// withHooks
import { MarketUser_profileProperty } from 'esoftplay/cache/market/user_profile/import';
import esp from 'esoftplay/esp';

import { LibCurl } from 'esoftplay/cache/lib/curl/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 { 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 { UseForm } from 'esoftplay/cache/use/form/import';

import { MarketAddressProperty } from 'esoftplay/cache/market/address/import';
import React from 'react';
import { 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 { from } = LibNavigation.getArgsAll<any>(props)
  const [form, setForm, reForm, delForm, batchForm] = UseForm<MarketAddress_inputForm>('market/address')

  function doAdd() {
    reForm((x) => {
      if (x) {
        if (!x.name) {
          LibToastProperty.show(esp.lang("market/address_add", "empty_location"))
          return
        }
        if (!x.recipient) {
          LibToastProperty.show(esp.lang("market/address_add", "empty_name"))
          return
        }
        if (!x.phone) {
          LibToastProperty.show(esp.lang("market/address_add", "empty_phone"))
          return
        }
        if (!x.location_name) {
          LibToastProperty.show(esp.lang("market/address_add", "empty_district"))
          return
        }
        if (!x.zipcode) {
          LibToastProperty.show(esp.lang("market/address_add", "empty_pos_code"))
          return
        }
        if (!x.address) {
          LibToastProperty.show(esp.lang("market/address_add", "empty_address"))
          return
        }
        if (!x.location_latlong) {
          LibToastProperty.show(esp.lang("market/address_add", "empty_location_address"))
          return
        }
        LibProgress.show(esp.lang("market/address_add", "waiting_add"))
        new LibCurl('shop/member_location_add', x,
          (res, msg) => {
            MarketAddressProperty.subscribe().trigger()
            delForm()
            MarketUser_profileProperty.update()
            LibProgress.hide()
            LibToastProperty.show(res)
            LibNavigation.back()
          },
          (msg) => {
            LibProgress.hide()
            LibToastProperty.show(msg?.message)
          }, 1
        )
      }
    })
  }


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