<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-if="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" v-model="queryParams" :rules="rules" :inline="true" label-width="130px" label-position="left">
            <el-form-item label="管理机构" prop="manageCom">
              <el-select v-model="queryParams.manageCom" clearable placeholder="请输入管理机构">
                <template #header>
                  <el-input v-model="likeName" placeholder="可根据机构编码或名称模糊查询" @input="filterByManageCom"></el-input>
                </template>
                <el-option
                  v-for="dict in filteredManagecom"
                  :key="dict.dictValue"
                  :label="dict.dictValue + '-' + dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="投保人名称" prop="appntName">
              <el-input v-model="queryParams.appntName" placeholder="请输入投保人名称" clearable />
            </el-form-item>
            <el-form-item label="保单号" prop="policyNumber">
              <el-input v-model="queryParams.policyNumber" placeholder="请输入保单号" clearable />
            </el-form-item>
            <el-form-item label="系统来源" prop="fromSystem">
              <el-select v-model="queryParams.fromSystem" clearable placeholder="请输入系统来源">
                <el-option v-for="dict in sys_from_system" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="匹配日期">
              <el-date-picker
                v-model="dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 240px"
              />
            </el-form-item>
            <el-form-item label="匹配确认日期">
              <el-date-picker
                v-model="dateAuditRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 240px"
              />
            </el-form-item>
            <el-row>
              <el-form>
                <el-button type="primary" icon="Search" @click="handleQuery">查询</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </el-form>
            </el-row>
          </el-form>
        </el-card>
        <el-card shadow="hover">
          <el-table v-loading="loading" :data="matchList" border style="width: 100%">
            <el-table-column label="请求流水号" align="center" prop="matchReqId" width="180px" />
            <el-table-column label="发起系统" align="center" prop="fromSystem" width="150px" >
              <template #default="scope">
                <dict-tag :options="sys_from_system" :value="scope.row.fromSystem" />
              </template>
            </el-table-column>
            <el-table-column label="投保人名称" align="center" prop="appntName" width="150px" show-overflow-tooltip />
            <el-table-column label="申请机构" align="center" prop="manageCom" />
            <el-table-column label="匹配日期" align="center" prop="matchDate" width="150px" />
            <el-table-column label="审核时间" align="center" prop="auditDate" width="150px" show-overflow-tooltip />
            <el-table-column label="暂收费号码" align="center" prop="temporaryChargeNumber" width="180px" />
            <el-table-column label="保单号" align="center" prop="policyNumber" width="150px" show-overflow-tooltip />
            <el-table-column label="实收金额" align="center" prop="actualReceivedAmount" width="150px" />
            <el-table-column label="暂缴费总金额" align="center" prop="amount" width="150px" />
            <el-table-column label="银行核对码" align="center" prop="reconciliationCode" width="150px" />
            <el-table-column label="暂收费到账日期" align="center" prop="arrivalDate" width="150px" />
          </el-table>
          <pagination
            v-show="total > 0"
            v-model:limit="queryParams.pageSize"
            v-model:page="queryParams.pageNum"
            :total="total"
            @pagination="getList"
          />
        </el-card>
      </div>
    </transition>
    <el-card shadow="hover">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button v-hasPermi="['bls:matchquery:export']" type="warning" plain icon="Download" @click="handleExport">导出</el-button>
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script setup name="MatcheQury" lang="ts">
import { queryManageCom } from '@/api/bls/cache';
import { queryMatchList } from '@/api/bls/match';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const showSearch = ref(true);
const { sys_from_system } = toRefs<any>(proxy?.useDict('sys_from_system'));

const likeName = ref('');
const filteredManagecom = ref([]);
const sys_managecomCache = ref([]);
const dateRange = ref<[string, string]>(['', '']);
const dateAuditRange = ref<[string, string]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const loading = ref(false);
const total = ref(0);
const matchList = ref([]);


const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    manageCom: undefined,
    appntName: undefined,
    fromSystem: undefined,
    policyNumber: undefined
  },
  rules: {}
});
const { queryParams, rules } = toRefs(data);


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

const getList = async () => {
  loading.value = true;
  const res = await queryMatchList(addRange());
  matchList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 导出按钮操作 */
const handleExport = () => {

  proxy?.download(
    'bls/match/MatchQueryExport',
    {
      ...addRange()
    },
    `保费匹配清单_${new Date().getTime()}.xlsx`
  );
};

const addRange = () => {
  queryParams.value = proxy?.addDateRange(queryParams.value, dateAuditRange.value, 'Audit');
  queryParams.value = proxy?.addDateRange(queryParams.value, dateRange.value);
  return queryParams.value;
};

/** 模糊查询 */
const filterByManageCom = () => {
  if (likeName.value) {
    filteredManagecom.value = sys_managecomCache.value.filter((item) => {
      return item.dictLabel.includes(likeName.value) || item.dictValue.includes(likeName.value);
    });
  } else {
    filteredManagecom.value = sys_managecomCache.value;
  }
};

const getCache = async () => {
  const res = await queryManageCom();
  if (res.code == 200) {
    sys_managecomCache.value = res.data;
    filteredManagecom.value = sys_managecomCache.value;
  }
};

onMounted(() => {
  getCache();
});
</script>

<style scoped lang="scss"></style>
