// 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 { MarketAddressProperty } from 'esoftplay/cache/market/address/import';
import { MarketAddress_input, MarketAddress_inputForm } from 'esoftplay/cache/market/address_input/import';
import { MarketHeader } from 'esoftplay/cache/market/header/import';
import { MarketStyle } from 'esoftplay/cache/market/style/import';
import { MarketUser_profileProperty } from 'esoftplay/cache/market/user_profile/import';
import { UseForm } from 'esoftplay/cache/use/form/import';

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


export interface MarketAddress_addProps {

}
export default function m(props: MarketAddress_addProps): any {
  // from ini antara dari mart/account atau market/account
  const { defaultAddress, from } = LibNavigation.getArgsAll<any>(props)
  const [form, setForm, reForm, delForm, batchForm] = UseForm<MarketAddress_inputForm>('market/address')

  function doEdit() {
    Keyboard.dismiss()
    LibDialog.confirm(esp.lang("market/address_edit", "address_title"), esp.lang("market/address_edit", "address_msg"), esp.lang("market/address_edit", "address_confirm"), () => {
      reForm((x) => {
        if (x) {
          if (!x.name) {
            LibToastProperty.show(esp.lang("market/address_edit", "empty_location_name"))
            return
          }
          if (!x.recipient) {
            LibToastProperty.show(esp.lang("market/address_edit", "empty_name"))
            return
          }
          if (!x.phone) {
            LibToastProperty.show(esp.lang("market/address_edit", "empty_phone"))
            return
          }
          if (!x.location_name) {
            LibToastProperty.show(esp.lang("market/address_edit", "empty_disctrict"))
            return
          }
          if (!x.zipcode) {
            LibToastProperty.show(esp.lang("market/address_edit", "empty_pos_code"))
            return
          }
          if (!x.address) {
            LibToastProperty.show(esp.lang("market/address_edit", "empty_address"))
            return
          }
          if (!x.location_latlong) {
            LibToastProperty.show(esp.lang("market/address_edit", "empty_location_address"))
            return
          }
          LibProgress.show(esp.lang("market/address_edit", "waiting_edit"))
          new LibCurl('shop/member_location_edit', x,
            (res, msg) => {
              delForm()
              MarketAddressProperty.subscribe().trigger()
              MarketUser_profileProperty.update()
              LibProgress.hide()
              LibToastProperty.show(res)
              LibNavigation.back()
            },
            (msg) => {
              LibProgress.hide()
              LibToastProperty.show(msg?.message, 5000)
            }, 1
          )
        }

      })
    }, esp.lang("market/address_edit", "address_cancel"), () => { })
  }

  return (
    <View style={{ flex: 1 }} >
      <MarketHeader
        onlyBack
        title={esp.lang("market/address_edit", "header_title")}
        rightView={() => (
          <TouchableOpacity onPress={doEdit} style={styleId_ZnTxdz} >
            <LibTextstyle textStyle="callout" text={esp.lang("market/address_edit", "btn_save")} style={styleId_ZwGcPJ} />
          </TouchableOpacity>
        )} />
      <MarketAddress_input defaultAddress={defaultAddress} from={from} />
    </View>
  )
}
const styleId_ZnTxdz: any = { height: 50, justifyContent: 'center', paddingRight: 17 }
const styleId_ZwGcPJ: any = { color: MarketStyle.colorPrimaryMarket }