// withHooks
import { EventTms_homeProperty } from 'esoftplay/cache/event/tms_home/import';
import { LibCurl } from 'esoftplay/cache/lib/curl/import';
import { LibLoading } from 'esoftplay/cache/lib/loading/import';
import { LibNavigation } from 'esoftplay/cache/lib/navigation/import';

import { LibUtils } from 'esoftplay/cache/lib/utils/import';
import { UserClass } from 'esoftplay/cache/user/class/import';
import esp from 'esoftplay/esp';
import React, { useEffect } from 'react';
import { View } from 'react-native';


export interface EventTms_outArgs {

}
export interface EventTms_outProps {

}
export default function m(props: EventTms_outProps): any {
  const { scan_type, url_ticket_update, url_ticket_detail, price_type, gate_type, qr_code, event_id, email, selectGate, typeScanner, event_title, qty, configPriority }: any = LibNavigation.getArgsAll<any>(props)

  let args = {
    selectGate: selectGate,
    fromPage: 'event/tms_home',
    event_id: event_id,
    event_title: event_title,
    typeScanner: typeScanner,
    gate_type: gate_type,
    price_type: price_type,
    url_ticket_detail: url_ticket_detail,
    url_ticket_update: url_ticket_update,
    configPriority: configPriority,
    scan_type: scan_type
  }

  useEffect(() => {
    loadData()
  }, [])

  function loadData() {
    EventTms_homeProperty.addCounterOutScan(1, "0")

    let post = {
      event_id: event_id,
      gate_id: selectGate?.id,
      user_qr: encodeURIComponent(qr_code),
      email: email ? email : "",
    }

    new LibCurl('event_tms_out', post, (res) => {
      // setResult(res)
      let modifyList = res.list.length > 0 && res.list.map((item: any, i: number) => {
        return ({
          ...item,
          checked: i == 0 ? 1 : 0
        })
      })
      let finalData = {
        ...res,
        list: modifyList
      }

      esp.log(res);

      if (modifyList.length == 1) {
        let a = modifyList?.filter((item: any) => item.checked == 1).map((it: any) => it.id)
        let postOut = {
          event_id: event_id,
          ids: JSON.stringify(a),
          gate_id: selectGate?.id,
          trx_id: new Date().getTime() + "" + UserClass.state().get()?.id,
          price_id: modifyList?.filter((item: any) => item.checked == 1).map((it: any) => it.price_id),
          scanned_out: LibUtils.moment().localeFormat('YYYY-MM-DD HH:mm:ss'),
        }
        let urlOut = "event_tms_out_confirm"
        LibNavigation.replace('event/tms_out_success', { data: finalData, ...args, url: urlOut, post: postOut })
      } else {
        LibNavigation.replace('event/tms_out_list', { data: finalData, ...args })
      }
    }, (error) => {
      LibNavigation.replace('event/tms_out_failed', { msgError: error, ...args, postGetData: post })
    }, 1)
  }

  return (
    <View style={{ flex: 1, backgroundColor: '#fff', alignContent: 'center', alignItems: 'center', justifyContent: 'center' }}>
      <LibLoading />
    </View>
  )
}