<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :inline="true" :model="queryParams" label-position="left" label-width="130px">
            <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.value" :label="dict.value + '-' + dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="付款人名称" prop="appntName">
              <el-input v-model="queryParams.appntName" clearable placeholder="请输入付款人名称" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="付款人银行账号" prop="bankAccount">
              <el-input v-model="queryParams.bankAccount" clearable placeholder="请输入收款人银行账号" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="保单号" prop="policyNumber">
              <el-input v-model="queryParams.policyNumber" clearable placeholder="请输入保单号" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="业务号" prop="businessNumber">
              <el-input v-model="queryParams.businessNumber" clearable placeholder="请输入业务号" @keyup.enter="handleQuery" />
            </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="dateRangeRece"
                end-placeholder="结束日期"
                range-separator="-"
                start-placeholder="开始日期"
                type="daterange"
                value-format="YYYY-MM-DD"
                style="width: 240px"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="金额" prop="amount">
              <el-input v-model="amountBigen" clearable style="width: 40%" />
              <div style="margin: 0 10px">-</div>
              <el-input v-model="amountEnd" clearable style="width: 40%" />
            </el-form-item>
            <el-row>
              <el-form-item>
                <el-button icon="Search" type="primary" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-row>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="recList">
        <el-table-column label="机构" type="manageCom" width="150" />
        <el-table-column label="日期" type="recDate" width="150" />
        <el-table-column label="批次号" type="batchNo" width="150" />
        <el-table-column label="暂缴费号" type="businessReceiptNumber" width="150" />
        <el-table-column label="保单号" type="policyNumber" width="150" />
        <el-table-column label="业务号码" type="businessNumber" width="150" />
        <el-table-column label="付款人名称" type="appntName" width="150" />
        <el-table-column label="付款人银行" type="bankCode" width="150" />
        <el-table-column label="付款人银行账号" type="bankAccount" width="150" />
        <el-table-column label="收费方式" type="paymentMethod" width="150" />
        <el-table-column label="金额" type="amount" width="150" />
        <el-table-column label="收入户" type="receivingBankAccount" width="150" />
        <el-table-column label="凭证号" type="belnrSap" width="150" />
        <el-table-column label="银行记账码" type="reconciliationCode" width="150" />
        <el-table-column label="备注（虚拟户）" type="virtualAccount" width="150" />
      </el-table>
      <pagination v-show="total > 0" v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageNum"
                  :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>
<script setup lang="ts">

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const dateRangeRece = ref<[DateModelType, DateModelType]>(['', '']);
const dateRangeChar = ref<[DateModelType, DateModelType]>(['', '']);
const amountBigen = ref<number | null>(null);
const amountEnd = ref<number | null>(null);
const amountRange = ref<[number | null, number | null]>([null, null]);
const queryFormRef = ref<ElFormInstance>();
const recList = ref([]);

const likeName = ref('');
const filteredManagecom = ref([]);
const { sys_managecom } = toRefs<any>(proxy?.useDict('sys_managecom'));
const { sys_bank_code } = toRefs<any>(proxy?.useDict('sys_bank_code'));
const { sys_rec_method } = toRefs<any>(proxy?.useDict('sys_rec_method'));
const { sys_from_system } = toRefs<any>(proxy?.useDict('sys_from_system'));

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    manageCom: undefined,
    appntName: undefined,
    bankAccount: undefined,
    businessReceiptNumber: undefined,
    policyNumber: undefined,
    businessNumber: undefined,
    fromSystem: undefined,
    chargeType: undefined,
    chargeStatus: undefined,
    paymentMethod: undefined,
    params: {}
  },
  rules: {}
});

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

/** 重置按钮操作 */
const resetQuery = () => {
  dateRangeRece.value = ['', ''];
  dateRangeChar.value = ['', ''];
  amountEnd.value = null;
  amountBigen.value = null;
  queryFormRef.value?.resetFields();
  handleQuery();
};
const getList = async () => {

}

const checkAmount = () => {
  const regex = /^\d+(\.\d{1,2})?$/;
  const fields = [
    { name: 'amountBigen', value: amountBigen.value },
    { name: 'amountEnd', value: amountEnd.value }
  ];
  for (const field of fields) {
    if (field.value && !regex.test(String(field.value))) {
      proxy?.$modal.msgError(`${field.name} 金额只能为数字且最多2位小数`);
      return false;
    }
  }
  if (amountBigen.value && amountEnd.value && amountBigen.value > amountEnd.value) {
    proxy?.$modal.msgError('开始金额不能大于终止金额');
    return false;
  }
  return true;
};
/** 模糊查询 */
const filterByManageCom = () => {
  if (likeName.value) {
    filteredManagecom.value = sys_managecom.value.filter((item) => {
      return item.label.includes(likeName.value) || item.value.includes(likeName.value);
    });
  } else {
    filteredManagecom.value = sys_managecom.value;
  }
};

const initManageCom = () => {
  filteredManagecom.value = sys_managecom.value;
};

onMounted(() => {
  getList();
  initManageCom();
});
</script>
<style scoped lang="scss"></style>
