// withHooks


import { EventTms_homeProperty } from 'esoftplay/cache/event/tms_home/import';
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 { LibPicture } from 'esoftplay/cache/lib/picture/import';
import { LibStatusbar } from 'esoftplay/cache/lib/statusbar/import';
import { LibStyle } from 'esoftplay/cache/lib/style/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 { Pressable, ScrollView, Text, View } from 'react-native';


export interface EventTms_out_failedArgs {

}
export interface EventTms_out_failedProps {

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

  useEffect(() => {
    EventTms_homeProperty.addCounterOutScanFailed(1, "0")
  }, [])

  function getBack() {
    LibNavigation.replace('component/scanner', {
      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
    })
  }

  function sendTelegram() {
    let data = {
      '#bbotmsexit': ":",
      reporter: {
        email: UserClass.state().get().email,
        name: UserClass.state().get().name
      },
      url: "event_tms_out",
      post: postGetData,
      error_msg: msgError,
    }

    let post = {
      text: String(JSON.stringify(data || {}, undefined, 2)).replace(/[\[\]\{\}\"]+/g, ''),
      chat_id: '-1001737180019',
      disable_web_page_preview: true
    }

    let _url = "https://api.telegram.org/bot923808407:AAEFBlllQNKCEn8E66fwEzCj5vs9qGwVGT4/sendMessage"
    new LibCurl().custom(_url, post, (res, msg) => { })
  }


  return (
    <View style={{ flex: 1 }}>
      <LibStatusbar style='light' />
      <View style={{ flex: 1 }}>
        <LibPicture source={esp.assets('failed.png')} style={{ flex: 1, height: LibStyle.height, width: LibStyle.width }} /* resizeMode={"stretch"} */ />
      </View>
      <View style={{ position: 'absolute', top: 100, left: 0, right: 0, bottom: 0, alignItems: 'center', alignContent: 'center' }} >
        <ScrollView>
          <View style={{ marginBottom: 20, alignSelf: 'center', borderRadius: LibStyle.width / 3, padding: 10, backgroundColor: '#fff' }}>
            <LibPicture
              source={esp.assets('failed_icon.png')}
              style={{ width: LibStyle.width / 3, height: LibStyle.width / 3 }}
            />
          </View>
          <Text allowFontScaling={false} style={{ fontWeight: 'bold', fontSize: 25, color: '#fff', marginHorizontal: 20, textAlign: 'center', letterSpacing: 1 }}>{msgError?.message}</Text>

          {
            msgError?.result?.length > 0 &&
            <View style={{ marginTop: 15, flexDirection: 'row', justifyContent: 'space-between', backgroundColor: '#37c2d0', borderTopLeftRadius: 10, borderTopRightRadius: 10 }}>
              <View style={{ flex: 1, justifyContent: 'center', borderRightWidth: 1, borderColor: LibStyle.colorBgGrey, paddingVertical: 5, padding: 5 }}>
                <Text allowFontScaling={false} style={{ fontWeight: 'bold', textAlign: 'center', fontSize: 14 }}>{esp.lang("event/tms_out_failed", "type")}</Text>
              </View>
              <View style={{ flex: 1, justifyContent: 'center', paddingVertical: 5 }}>
                <Text allowFontScaling={false} style={{ fontWeight: 'bold', textAlign: 'center', fontSize: 14 }}>{esp.lang("event/tms_out_failed", "time")}</Text>
              </View>
            </View>
          }
          {
            msgError?.result?.length > 0 && msgError?.result?.map((item: any, i: number) => {
              return (
                <View key={i} style={{ backgroundColor: i % 2 ? '#fff' : LibStyle.colorGrey, flexDirection: 'row', justifyContent: 'space-between', borderBottomWidth: 1, borderBottomColor: LibStyle.colorBgGrey }}>
                  <View style={{ flex: 1, justifyContent: 'center', borderRightWidth: 1, borderColor: LibStyle.colorBgGrey, paddingVertical: 5, padding: 5 }}>
                    <Text allowFontScaling={false} numberOfLines={2} ellipsizeMode="tail" style={{ letterSpacing: 1, textAlign: 'center', fontSize: 12 }}>{item?.price_name}</Text>
                  </View>
                  <View style={{ flex: 1, justifyContent: 'center', paddingVertical: 5 }}>
                    {
                      item.scanned != "0000-00-00 00:00:00" &&
                      <Text allowFontScaling={false} style={{ textAlign: 'center', fontSize: 12 }}>{LibUtils.moment(item.hall_scanned).localeFormat("DD MMM H:mm:ss ")}</Text>
                    }
                  </View>
                </View>
              )
            })
          }

        </ScrollView>
        <View style={{ flexDirection: 'row', width: LibStyle.width - 40, marginVertical: 10, justifyContent: 'space-between' }}>
          <Pressable onPress={() => {

            LibDialog.warningConfirm(esp.lang("event/tms_in_failed", "warn_title"), esp.lang("event/tms_in_failed", "warn_msg"), esp.lang("event/tms_in_failed", "warn_ok"), () => {
              sendTelegram()
            }, esp.lang("event/tms_in_failed", "warn_no"), () => { })

          }} style={{ width: (LibStyle.width - 45) * 0.5, height: 43, borderRadius: 5, backgroundColor: LibStyle.colorRed, borderWidth: 3, borderColor: "#fff", justifyContent: 'center', alignContent: 'center', alignItems: 'center', ...LibStyle.elevation(10) }}>
            <Text allowFontScaling={false} style={{ textAlign: 'center', fontSize: 14, fontWeight: 'bold', color: "#fff" }}>{esp.lang("event/tms_out_failed", "report_error")}</Text>
          </Pressable>
          <Pressable onPress={getBack} style={{ width: (LibStyle.width - 45) * 0.5, height: 43, borderRadius: 5, backgroundColor: LibStyle.colorGreen, justifyContent: 'center', alignContent: 'center', alignItems: 'center', ...LibStyle.elevation(10) }}>
            <Text allowFontScaling={false} style={{ fontSize: 14, fontWeight: 'bold', color: "#fff" }}>{esp.lang("event/tms_out_failed", "back")}</Text>
          </Pressable>

        </View>
      </View>

    </View>
  )
}