// withHooks
import esp from 'esoftplay/esp';

import { LibCurl } from 'esoftplay/cache/lib/curl/import';
import { LibDialog } from 'esoftplay/cache/lib/dialog/import';
import { LibNavigation } from 'esoftplay/cache/lib/navigation/import';
import { LibProgress } from 'esoftplay/cache/lib/progress/import';
import { LibTextstyle } from 'esoftplay/cache/lib/textstyle/import';
import { LibToastProperty } from 'esoftplay/cache/lib/toast/import';
import { MarketHeader } from 'esoftplay/cache/market/header/import';
import { MarketProduct_input, MarketProduct_inputForm, MarketProduct_inputProperty } from 'esoftplay/cache/market/product_input/import';
import { MarketReview } from 'esoftplay/cache/market/review/import';
import { MarketStyle } from 'esoftplay/cache/market/style/import';
import { UseForm } from 'esoftplay/cache/use/form/import';

import React, { useRef } from 'react';
import { TouchableOpacity, View } from 'react-native';


export interface MarketProduct_editArgs {
  id: string,
  url: string,
  reviewing_admin?: string
  reviewing_resubmit?: string
}
export interface MarketProduct_editProps {

}
export default function m(props: MarketProduct_editProps): any {
  const { id, url, reviewing_admin, reviewing_resubmit } = LibNavigation.getArgsAll<any>(props)
  const [form, setForm, reForm] = UseForm<MarketProduct_inputForm>("market_product_input")
  const reviewRef = useRef<MarketReview>(null)

  function doEdit() {
    if (form) {
      LibProgress.show(esp.lang("market/product_edit", "edit_wait"))
      new LibCurl(url, MarketProduct_inputProperty.buildPost(form),
        (res, msg) => {
          LibProgress.hide()
          LibNavigation.back()
          LibToastProperty.show(res)
        },
        (msg) => {
          LibToastProperty.show(msg?.message)
          LibProgress.hide()
        }, 1
      )
    }
  }

  return (
    <View style={{ flex: 1, backgroundColor: "white" }} >
      <MarketHeader
        leftIcon={{
          name: "arrow-left", onPress: () => {
            LibDialog.warningConfirm(esp.lang("market/product_edit", "exit_title"), esp.lang("market/product_edit", "exit_msg"), esp.lang("market/product_edit", "exit_ok"), () => {
              LibNavigation.back()
            }, esp.lang("market/product_edit", "exit_cancel"), () => { })
          }
        }}
        title={esp.lang("market/product_edit", "header_title")}
        subtitle={esp.lang("market/product_edit", "header_sub")} rightView={() => (
          <TouchableOpacity
            onPress={() => {
              if (MarketProduct_inputProperty.validateInput(form)) {
                if (reviewing_resubmit) {
                  reviewRef.current!.show(reviewing_admin, reviewing_resubmit, doEdit)
                } else {
                  doEdit()
                }
              }
            }}
            style={{ paddingRight: 17 }} >
            <LibTextstyle text={esp.lang("market/product_edit", "btn_save")} textStyle="footnote" style={{ color: MarketStyle.colorPrimaryMarket, fontWeight: "bold" }} />
          </TouchableOpacity>)} />
      <MarketProduct_input id={id} />
      <MarketReview ref={reviewRef} />
    </View>
  )
}