<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-input
                v-if="search.tag == 5"
                v-model.trim="search.currency"
                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-input
                v-model.trim="search.cardNumber"
                class="crm-border-radius-no"
                clearable
                :placeholder="$t('Placeholder.Input') + $t('Ucard.Transactions.s1')"
                @keyup.enter="toSearch"
              ></el-input>
            </el-form-item>
            <el-form-item style="margin-right: 10px">
              <el-select
                v-model="search.type"
                class="crm-border-radius-no"
                clearable
                :placeholder="$t('Ucard.Transactions.s3')"
                @change="toSearch"
              >
                <el-option
                  v-for="option in transactionTypeOptions"
                  :key="option.value"
                  :label="$t(option.label)"
                  :value="option.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="search.status"
                class="crm-border-radius-no"
                clearable
                :placeholder="$t('Ucard.Transactions.s5')"
                @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-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-Transactions-Export'] && display['R-Transactions-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="" align="left" :label="$t('Label.CidAccount')">
            <template #default="scope">
              <span
                v-if="scope.row.cId && display['R-Transactions-Btn1'].show"
                class="crm-text-underline"
                @click="accountOpen(scope.row.cId)"
                >{{ scope.row.cId || '--' }}</span
              >
              <span v-else>{{ scope.row.cId || '--' }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="" align="left" :label="$t('Label.Name')">
            <template #default="scope">
              <span v-if="scope.row.firstName">{{ scope.row.firstName + ' ' }}</span>
              <span v-if="scope.row.middle">{{ scope.row.middle + ' ' }}</span>
              <span v-if="scope.row.lastName">{{ scope.row.lastName }}</span>
              <span v-if="!scope.row.firstName && !scope.row.lastName && !scope.row.middle">{{
                '--'
              }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="" align="left" :label="$t('Label.Email')">
            <template #default="scope">
              {{ scope.row.email || '--' }}
            </template>
          </el-table-column>
          <el-table-column prop="mobile" align="left" :label="$t('Ucard.KycAuth.item2')">
            <template #default="scope"> {{ scope.row.areaCode }} {{ scope.row.mobile }} </template>
          </el-table-column>
          <el-table-column prop="cardNumber" align="left" :label="$t('Ucard.Transactions.item2')">
            <template #default="scope">
              <span
                v-if="scope.row.cardNumber"
                class="crm-text-underline"
                @click="cardOpen(scope.row.cardNumber)"
                >{{ scope.row.cardNumber || '--' }}</span
              >
              <span v-else>{{ scope.row.cardNumber || '--' }}</span>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="cardTypeId" align="left" :label="$t('Ucard.Transactions.item1')" /> -->
          <el-table-column prop="currency" align="left" :label="$t('Ucard.Transactions.item3')" />
          <el-table-column prop="amount" align="left" :label="$t('Ucard.Transactions.item4')" />
          <el-table-column prop="fee" align="left" :label="$t('Ucard.Transactions.item5')" />
          <el-table-column prop="tradeNo" align="left" :label="$t('Ucard.Transactions.item8')" />
          <el-table-column prop="type" align="left" :label="$t('Ucard.Transactions.item9')">
            <template #default="scope">
              <span>
                {{ $t(transactionTypeMap[scope.row.type]) }}
              </span>
            </template>
          </el-table-column>
          <el-table-column prop="status" align="left" :label="$t('Ucard.Transactions.item11')">
            <template #default="scope">
              <span
                v-if="scope.row.status === 'succeed' || scope.row.status === 'success'"
                class="state crm_state_blue"
                >{{ $t('Ucard.Transactions.t18') }}</span
              >
              <span
                v-else-if="scope.row.status === 'failed' || scope.row.status === 'fail'"
                class="state crm_state_gray"
                >{{ $t('Ucard.Transactions.t25') }}</span
              >
              <span v-else-if="scope.row.status === 'processing'" class="state crm_state_orange">{{
                $t('card.Status.t3')
              }}</span>
              <span
                v-else-if="scope.row.status === 'wait_process'"
                class="state crm_state_yellow"
                >{{ $t('card.Status.t5') }}</span
              >
              <span v-else class="state crm_state_green">{{ $t('Ucard.Transactions.t24') }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="" align="center" :label="$t('Label.Action')">
            <template #default="scope">
              <el-dropdown trigger="click" @command="handleCommand">
                <span class="el-dropdown-link crm-cursor">
                  <i style="font-weight: bold; font-size: 20px" class="iconfont iconcaidan"></i>
                </span>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item
                      v-if="display['R-Transactions-Btn1'].show"
                      :command="{ type: 1, row: scope.row }"
                    >
                      <el-icon><Operation /></el-icon>
                      {{ $t('R-Hire-Check') }}
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <PagePagination
      :pager-info="pagerInfo"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <detailed-info-cid
      :dialog-info-cid="dialogInfoCid"
      :form-info="formInfo"
      :is-trading="true"
      @close="close"
    >
    </detailed-info-cid>
    <div v-if="dialogInfoCid" class="crm_verified_info_mask" @click="closeDia"></div>
    <ViewCardSingle
      :dialog-info-trading-single="dialogInfoTradingSingle"
      :form-list="formSingle"
      :editor-type="editorType"
      @close-single="closeSingle"
    >
    </ViewCardSingle>
    <div v-if="dialogInfoTradingSingle" class="crm_verified_info_mask" @click="closeSingle"></div>
  </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 Config from '@/config'
  import Service1 from '@/service/customer'
  import PagePagination from '@/components/pagePagination/index.vue'
  import DetailedInfoCid from '@/views/components/DetailedInfoCid/index.vue'
  import ViewCardSingle from '@/views/components/ViewCardSingle/index.vue'
  import { exportExcel } from '@/utils/export'
  import {
    searchTagOptions,
    statusOptions,
    transactionTypeMap,
    transactionTypeOptions,
  } from '@/views/card/CardTransactions/const'
  import { Operation } from '@element-plus/icons-vue'
  import { useI18n } from 'vue-i18n'

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

  // 响应式数据
  const pictLoading = ref(false)
  const dialogInfoTradingSingle = ref(false)
  const formSingle = ref({})
  const editorType = ref(4)
  const search = reactive({
    tag: 1,
    mobile: '',
    cId: '',
    email: '',
    cardNumber: '',
    currency: '',
    type: '',
    dataType: '',
    status: '',
  })
  const mock_tableData = ref([])
  const pagerInfo = reactive({ row: 10, current: 1, pageTotal: 0, rowTotal: 0 })
  const dialogInfoCid = ref(false)
  const formInfo = ref({})

  const formRef = ref()

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

  // 方法
  // 导出
  const exportAgents = async () => {
    exportExcel('/wasabi/card/transac/record/export', { ...search }, 'Bank_Card_Transactions')
  }

  // 银行卡详情
  const cardOpen = (cardNumber) => {
    router.push({ name: 'R-CardDetail', params: { cardNumber: cardNumber } })
  }

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

  const searchSingleCid = async (cId) => {
    const res = await Service1.cidRealSingle({
      id: cId,
    })
    if (res.code == Code.StatusOK) {
      // 注意：这里原代码有 editor 变量，但组件中未定义，保持原逻辑
      // if (this.editor) {
      //   this.formList = res.data;
      //   this.dialogInfoAdd = true;
      // } else {
      formInfo.value = res.data
      searchRealAll(cId)
      // }
    } else {
      ElMessage.error(res.msg)
    }
  }

  // 获取cid下的真实账户信息
  const searchRealAll = async (cId) => {
    const res = await Service1.realCustomerListAll({
      cId: cId,
      page: {
        current: 1,
        row: 1,
      },
    })
    if (res.code == Code.StatusOK) {
      formInfo.value.realList = res.data
      dialogInfoCid.value = true
    } else {
      ElMessage.error(res.msg)
    }
  }

  // 详细信息关闭cid/pibno
  const closeDia = () => {
    dialogInfoCid.value = false
  }

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

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

  // 更新
  const updateCardTypes = async () => {
    const res = await Service.updateCardTypes({})
    if (res.code == Code.StatusOK) {
      ElMessage.success(t('Msg.SearchSuccess'))
      searchFunc()
    } else {
      ElMessage.error(res.msg)
    }
  }

  // 查看
  const closeSingle = () => {
    dialogInfoTradingSingle.value = false
  }

  const handleCommand = (command) => {
    switch (command.type) {
      case 1:
        dialogInfoTradingSingle.value = true
        formSingle.value = command.row
        break
    }
  }

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

    try {
      const res = await Service.transactionsList({
        ...search,
        page: {
          current: pagerInfo.current,
          row: pagerInfo.row,
        },
      })
      if (res.code == Code.StatusOK) {
        mock_tableData.value = res.data
        if (res.page != null) {
          pagerInfo.rowTotal = res.page.rowTotal
          pagerInfo.pageTotal = res.page.pageTotal
        } else {
          pagerInfo.rowTotal = 0
        }
        ElMessage.success(t('Msg.SearchSuccess'))
      } else {
        ElMessage.error(res.msg)
      }
    } catch (error) {
      ElMessage.error(res.msg)
    }
    pictLoading.value = false
  }

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

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

  // 生命周期
  onMounted(() => {
    searchFunc()
  })

  // 监听器
  watch(
    () => search.tag,
    () => {
      search.mobile = ''
      search.email = ''
      search.cId = ''
      search.cardNumber = ''
      search.currency = ''
      search.type = ''
      search.dataType = ''
      search.status = ''
    }
  )
</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>
