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


import { LibCurl } from 'esoftplay/cache/lib/curl/import';
import { LibDialog } from 'esoftplay/cache/lib/dialog/import';
import { LibLoading } from 'esoftplay/cache/lib/loading/import';
import { LibNavigation } from 'esoftplay/cache/lib/navigation/import';
import { LibProgress } from 'esoftplay/cache/lib/progress/import';
import { LibToastProperty } from 'esoftplay/cache/lib/toast/import';
import { LibUtils } from 'esoftplay/cache/lib/utils/import';
import { MarketButton } from 'esoftplay/cache/market/button/import';
import { MarketHeader } from 'esoftplay/cache/market/header/import';
import { MarketInput_container } from 'esoftplay/cache/market/input_container/import';
import { MarketLocationProperty } from 'esoftplay/cache/market/location/import';
import { MarketMerchant_input, MarketMerchant_inputForm, MarketMerchant_inputProperty } from 'esoftplay/cache/market/merchant_input/import';
import { MarketMerchant_reviewingProperty } from 'esoftplay/cache/market/merchant_reviewing/import';
import { MarketNotice } from 'esoftplay/cache/market/notice/import';
import { MarketReview } from 'esoftplay/cache/market/review/import';
import { UseCurl } from 'esoftplay/cache/use/curl/import';
import { UseForm } from 'esoftplay/cache/use/form/import';
import { UserClass } from 'esoftplay/cache/user/class/import';

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


export interface MarketMerchant_editProps {

}
export default function m(props: MarketMerchant_editProps): any {
  const [form, setForm, reForm, delForm, batchUpdate] = UseForm<MarketMerchant_inputForm>("market_merchant_input")
  const [review, setReview] = MarketMerchant_reviewingProperty.reviewState().useState()
  const [curl] = UseCurl(true)
  const [loading, setLoading] = useSafeState(false)
  const [detail, setDetail] = useSafeState<any>()
  const user = UserClass.state().useSelector(state => state)
  const reviewRef = useRef<MarketReview>(null)

  const location = MarketLocationProperty.state().useSelector(s => s)

  useEffect(() => {
    if (location)
      curl('shop/merchant_detail/' + user?.merchant_id, null, (res, msg) => {
        const lc = location.filter((lc: any) => lc.id == res.location_id)[0] || []
        batchUpdate({
          ...res,
          location: { ...lc },
          latlong: { latlong: res.location_latlong }
        })
        setDetail(res)
      }, 1)
  }, [])

  function doEdit() {
    reForm((form) => {
      setLoading(true)
      LibProgress.show(esp.lang("market/merchant_edit", "edit_wait"))
      new LibCurl("shop/merchant_edit", MarketMerchant_inputProperty.buildPost(form),
        (res, msg) => {
          //@ts-ignore
          setReview(null)
          LibProgress.hide()
          setLoading(false)
          LibNavigation.backToRoot()
          LibToastProperty.show(esp.lang("market/merchant_edit", "edit_ok"))
          LibNavigation.navigate("market/account_merchant")
        }, (msg) => {
          LibProgress.hide()
          LibDialog.warning(esp.lang("market/merchant_edit", "edit_failed"), msg?.message)
          // Alert.alert(JSON.stringify(msg))
        }, 1
      )
    })
  }


  return (
    <View style={{ flex: 1, backgroundColor: "white" }} >
      <MarketHeader onlyBack title={esp.lang("market/merchant_edit", "header_title")} />
      {
        !detail
          ? <LibLoading />
          :
          <MarketInput_container>
            {
              LibUtils.checkUndefined(detail, 'alert.0') && detail?.alert?.map((item: any, i: number) => (
                <MarketNotice key={i} mode={item.type} text={item.message} canPress={item.module != '' && item.module != "market/merchant_edit" && item.url != ''} onPress={() => {
                  if (item?.module != '') {
                    LibNavigation.navigate(item?.module, { url: item?.url })
                  }
                }} />
              ))
            }
            <MarketMerchant_input defaultData={detail} />
            <View style={{ paddingHorizontal: 17, marginTop: 4, marginBottom: 8 }} >
              <MarketButton
                text={esp.lang("market/merchant_edit", "save_change")}
                onPress={() => {
                  if (MarketMerchant_inputProperty.validateInputMerchant(form)) {
                    if (detail?.reviewing_resubmit) {
                      if (detail?.is_online == 1 && form?.is_online == 0) {
                        LibNavigation.navigateForResult('market/confirm_change_type').then((t) => {
                          if (t)
                            reviewRef.current?.show(detail?.reviewing_admin, detail?.reviewing_resubmit, doEdit)
                        })
                      } else {
                        reviewRef.current?.show(detail?.reviewing_admin, detail?.reviewing_resubmit, doEdit)
                      }
                    } else {
                      doEdit()
                    }
                  }
                }} />
            </View>
          </MarketInput_container>
      }
      <MarketReview ref={reviewRef} />
    </View>
  )
}