<template>
  <div
    id="review_Email"
    v-loading="pictLoading"
    class="view"
    element-loading-background="rgba(43, 48, 67, 0.65)"
    element-loading-spinner="el-icon-loading"
  >
    <div class="crm_search">
      <el-form ref="formRef" label-position="" :model="search" label-width="">
        <el-row>
          <el-col :span="24" :md="24" :lg="24">
            <el-form-item>
              <el-select
                v-model="search.tag"
                class="crm_search_down crm-border-radius-no"
                :placeholder="$t('Placeholder.Choose')"
              >
                <el-option
                  v-for="option in searchTagOptions"
                  :key="option.value"
                  :label="$t(option.label)"
                  :value="option.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="margin-right: 10px">
              <el-input
                v-if="search.tag == 1"
                v-model.trim="search.cId"
                class="crm-border-left-no crm-border-radius-no"
                clearable
                :placeholder="$t('Placeholder.Input')"
                @keyup.enter="toSearch"
              ></el-input>
              <el-input
                v-if="search.tag == 2"
                v-model.trim="search.mobile"
                class="crm-border-left-no crm-border-radius-no"
                clearable
                :placeholder="$t('Placeholder.Input')"
                @keyup.enter="toSearch"
              ></el-input>
              <el-input
                v-if="search.tag == 3"
                v-model.trim="search.email"
                class="crm-border-left-no crm-border-radius-no"
                clearable
                :placeholder="$t('Placeholder.Input')"
                @keyup.enter="toSearch"
              ></el-input>
            </el-form-item>
            <el-form-item style="margin-right: 10px">
              <el-select
                v-model="search.type"
                filterable
                clearable
                class="crm-border-radius-no select_down"
                :placeholder="$t('card.Form.f52')"
                @change="toSearch"
              >
                <el-option :label="$t('card.Status.t22')" :value="null"></el-option>
                <el-option
                  v-for="(label, key) in options"
                  :key="key"
                  :label="$t(label)"
                  :value="key"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item style="margin-right: 10px">
              <el-select
                v-model="search.status"
                filterable
                clearable
                class="crm-border-radius-no select_down"
                :placeholder="$t('Label.State')"
                @change="toSearch"
              >
                <el-option
                  v-for="option in statusOptions"
                  :key="option.value"
                  :label="$t(option.label)"
                  :value="option.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-date-picker
                v-model="search.date"
                class="crm_date_pick crm-border-radius-no"
                type="daterange"
                unlink-panels
                value-format="yyyy-MM-dd"
                range-separator="-"
                :start-placeholder="$t('Placeholder.Start')"
                :end-placeholder="$t('Placeholder.End')"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button class="crm-border-radius-no crm-border-left-no" @click="toSearch">
                <el-icon><Search /></el-icon>
              </el-button>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item>
          <el-button
            v-if="display['R-UserOrder-Export'] && display['R-UserOrder-Export'].show"
            type="primary"
            style="margin-left: 8px"
            @click="exportAgents"
            >{{ $t('Btn.Export') }}</el-button
          >
        </el-form-item>
      </el-form>

      <div class="card-mock-demo" style="margin: 30px 0">
        <el-table :data="mock_tableData" stripe style="margin-top: 20px; width: 100%">
          <el-table-column prop="cId" align="left" :label="$t('Ucard.UserOrder.item1')">
            <template #default="scope">
              <span
                v-if="scope.row.cId"
                class="crm-text-underline"
                style="cursor: pointer"
                @click="accountOpen(scope.row.cId)"
                >{{ scope.row.cId }}</span
              >
              <span v-else>--</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('Ucard.UserOrder.item14')">
            <template #default="scope">
              {{ scope.row.lastName }}{{ scope.row.firstName }}
            </template>
          </el-table-column>
          <el-table-column prop="email" align="left" :label="$t('Ucard.UserOrder.item15')" />
          <el-table-column prop="mobile" align="left" :label="$t('Ucard.UserOrder.item16')">
            <template #default="scope"> {{ scope.row.areaCode }} {{ scope.row.mobile }} </template>
          </el-table-column>
          <el-table-column prop="type" align="left" :label="$t('card.Form.f52')">
            <template #default="scope">
              <span v-if="scope.row.type && options[scope.row.type]">
                {{ $t(options[scope.row.type]) }}
              </span>
              <span v-else>--</span>
            </template>
          </el-table-column>
          <el-table-column prop="amount" align="left" :label="$t('card.Form.f55')" />
          <el-table-column prop="fee" align="left" :label="$t('card.Form.f30')" />
          <el-table-column prop="addTime" align="left" :label="$t('card.Form.f51')" />
          <el-table-column prop="status" align="left" :label="$t('Label.State')">
            <template #default="scope">
              <span v-if="scope.row.status == '2'" class="state crm_state_blue">{{
                $t('card.Status.t1')
              }}</span>
              <span v-else-if="scope.row.status == '3'" class="state crm_state_gray">{{
                $t('card.Status.t2')
              }}</span>
              <span v-else class="state crm_state_orange">
                {{ $t('card.Status.t3') }}
              </span>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <trading-info-add
      :dialog-info-trading-add="dialogInfoTradingAdd"
      :editor="editor"
      :add-type="addType"
      :my-info="myInfo"
      :form-list="formList"
      @confirm-to-reload="confirmToReload"
      @close-add="closeAdd"
    ></trading-info-add>

    <PagePagination
      :pager-info="pagerInfo"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <detailed-info-cid
      :dialog-info-cid="dialogInfoCid"
      :form-info="formInfo"
      :is-trading="true"
      @close="closeCidDialog"
    />
  </div>
</template>

<script setup>
  import { ref, reactive, computed, watch, onMounted, inject } from 'vue'
  import { useRouter } from 'vue-router'
  import { ElMessage } from 'element-plus'
  import Service from '@/service/ucard'
  import PagePagination from '@/components/pagePagination'
  import Config from '@/config/index'
  import TradingInfoAdd from '@/views/components/PaymentTransfer'
  import DetailedInfoCid from '@/views/components/DetailedInfoCid'
  import { exportExcel } from '@/utils/export'
  import { options, searchTagOptions, statusOptions } from './const'
  import { useI18n } from 'vue-i18n'

  const { Code } = Config
  const router = useRouter()
  const Session = inject('session')
  const pigeon = inject('pigeon')
  const { t } = useI18n()

  // 响应式数据
  const pictLoading = ref(false)
  const dialogInfoTradingAdd = ref(false)
  const dialogInfoCid = ref(false)
  const formInfo = ref({})
  const editor = ref('')
  const addType = ref('')
  const formList = ref({})
  const myInfo = ref({})
  const mock_tableData = ref([])
  const pagerInfo = reactive({ row: 10, current: 1, pageTotal: 0, rowTotal: 0 })
  const banksData = ref([])
  const formRef = ref()

  // 搜索数据
  const search = reactive({
    tag: 1,
    cId: '',
    mobile: '',
    email: '',
    currency: '',
    amount: '',
    type: '',
    startDate: '',
    endDate: '',
    date: null,
  })

  // 计算属性
  const display = computed(() => {
    return JSON.parse(Session.Get('display', true))
  })

  // 方法
  // 导出
  const exportAgents = async () => {
    if (search.date == null) {
      search.startDate = ''
      search.endDate = ''
    } else if (search.date.length == 0) {
      search.startDate = ''
      search.endDate = ''
    } else {
      search.startDate = search.date[0]
      search.endDate = search.date[1]
    }

    exportExcel(pigeon, '/wasabi/card/wallet/record/export', { ...search }, 'Wallet_Balance_Record')
  }

  const closeAdd = (val) => {
    dialogInfoTradingAdd.value = val
  }

  const closeDiaAdd = () => {
    dialogInfoTradingAdd.value = false
  }

  const confirmToReload = () => {
    closeDiaAdd()
    searchFunc()
  }

  const toSearch = () => {
    pagerInfo.current = 1
    searchFunc()
  }

  // 列表
  const searchFunc = async () => {
    if (!display.value['R-UserOrder-Search']?.show) {
      ElMessage.warning(t('Msg.NotDisplay'))
      return
    }

    if (search.date == null) {
      search.startDate = ''
      search.endDate = ''
    } else if (search.date.length == 0) {
      search.startDate = ''
      search.endDate = ''
    } else {
      search.startDate = search.date[0]
      search.endDate = search.date[1]
    }

    pictLoading.value = true
    try {
      const res = await Service.getRecordPage({
        ...search,
        page: {
          current: pagerInfo.current,
          row: pagerInfo.row,
        },
      })

      if (res.code === Code.StatusOK) {
        mock_tableData.value = res.data || []
        pagerInfo.rowTotal = res.page?.rowTotal || 0
        pagerInfo.pageTotal = res.page?.pageTotal || 0
        ElMessage.success(t('Msg.SearchSuccess'))
      } else {
        ElMessage.error(res.msg)
      }
    } catch (error) {
      ElMessage.error(t('Msg.SearchFailed'))
    } finally {
      pictLoading.value = false
    }
  }

  const handleSizeChange = (val) => {
    pagerInfo.row = val
    searchFunc()
  }

  const handleCurrentChange = (val) => {
    pagerInfo.current = val
    searchFunc()
  }

  // 详细信息cid
  const accountOpen = (cId) => {
    router.push({ name: 'R-CustomerDetail', params: { cId: cId } })
  }

  const closeCidDialog = (val) => {
    dialogInfoCid.value = val
  }

  // 监听器
  watch(
    () => search.tag,
    () => {
      search.cId = ''
      search.mobile = ''
      search.email = ''
      search.currency = ''
      search.amount = ''
      search.type = ''
    }
  )

  // 生命周期
  onMounted(() => {
    searchFunc()
  })
</script>
<style scoped lang="scss">
  #review_Email {
    .crm_search {
      .search_action_btn {
        .delete {
          background-color: #a1a1a1;
        }

        .delete.active {
          background-color: #368fec;
        }
      }
    }

    .el-table .state {
      display: inline-block;
      min-width: 80px;
      max-width: 150px;
      box-sizing: border-box;
      line-height: 1.5;
      border-radius: 2px;
      padding: 2px 10px;
      color: #ffffff;
    }
  }
</style>
<style lang="scss">
  #review_Email {
    .dialog_header_w {
      .crm_search_down {
        width: 400px;
      }
    }
  }
</style>
