<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" v-loading="loading" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :rules="rules" :inline="true" label-width="80px">
            <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="dateRange">
              <el-date-picker
                v-model="queryParams.dateRange"
                value-format="YYYY-MM-DD"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                clearable
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="日志类型" prop="errType">
              <el-select v-model="queryParams.errType" placeholder="请选择日志类型" clearable>
                <el-option v-for="dict in bfs_error_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-row>
              <el-form-item>
                <el-button type="primary" icon="Search" @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">
      <el-table v-if="queryParams.errType === '1'" v-loading="loading" :data="businessDataList">
        <el-table-column label="错误描述" align="center" prop="errMsg" width="100px" show-overflow-tooltip />
        <el-table-column label="来源系统" align="center" prop="sourceCode" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="bfs_buss_system" :value="scope.row.sourceCode" />
          </template>
        </el-table-column>
        <el-table-column label="业务场景类别编码" align="center" prop="bussType" width="100px" show-overflow-tooltip/>
        <el-table-column label="业务场景类别名称" align="center" prop="bussName" width="120px"  show-overflow-tooltip/>
        <el-table-column label="团单号" align="center" prop="policyNo" width="200px" show-overflow-tooltip />
        <el-table-column label="保单号" align="center" prop="singlePolicyNo" width="200px" show-overflow-tooltip />
        <el-table-column label="险种代码" align="center" prop="productCode" show-overflow-tooltip />
        <el-table-column label="总金额" align="center" prop="amount" width="100px"  show-overflow-tooltip/>
        <el-table-column label="不含税金额" align="center" prop="income" width="100px" show-overflow-tooltip />
        <el-table-column label="税额" align="center" prop="tax" width="100px" show-overflow-tooltip />
        <el-table-column label="交易日期" align="center" prop="businessDate" width="150px" show-overflow-tooltip>
          <template #default="scope">
            <span>{{ parseTime(scope.row.businessDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="理赔回退标识" align="center" prop="isBack" show-overflow-tooltip />
        <el-table-column label="业务渠道" align="center" prop="channelCode" show-overflow-tooltip />
        <el-table-column label="销售渠道" align="center" prop="salesChannel" show-overflow-tooltip />
        <el-table-column label="收付机构" align="center" prop="subManageCom" width="200px" show-overflow-tooltip />
        <el-table-column label="银行账户代码" align="center" prop="bankAccount" width="200px" show-overflow-tooltip />
        <el-table-column label="银行账户名称" align="center" prop="bankAccountName" show-overflow-tooltip />
        <el-table-column label="保单业务交易号码" align="center" prop="transactionNo" width="200px" show-overflow-tooltip />
        <el-table-column label="保单管理机构" align="center" prop="policyOrgCode" width="200px" show-overflow-tooltip />
        <el-table-column label="缴别" align="center" prop="payIntervalType" show-overflow-tooltip />
        <el-table-column label="缴费期限" align="center" prop="payInterval" show-overflow-tooltip />
        <el-table-column label="缴费期限标志" align="center" prop="payIntervalFlag" show-overflow-tooltip />
        <el-table-column label="业务来源" align="center" prop="businessSource" show-overflow-tooltip />
        <el-table-column label="费用类型" align="center" prop="expenseType" show-overflow-tooltip />
        <el-table-column label="健康险业务标识" align="center" prop="healthLabel" show-overflow-tooltip />
        <el-table-column label="银行记账码" align="center" prop="matchCode" width="200px" show-overflow-tooltip />
        <el-table-column label="开户行" align="center" prop="hbkId" show-overflow-tooltip />
        <el-table-column label="账户标示" align="center" prop="hktId" show-overflow-tooltip />
        <el-table-column label="业务性质" align="center" prop="operSourceType" show-overflow-tooltip />
        <el-table-column label="税率" align="center" prop="taxRate" show-overflow-tooltip />
        <el-table-column label="统一社会信用代码" align="center" prop="taxregno" width="200px" show-overflow-tooltip />
        <el-table-column label="税码" align="center" prop="taxCode" show-overflow-tooltip />
        <el-table-column label="承诺项目" align="center" prop="budgetCode" show-overflow-tooltip />
      </el-table>

      <el-table v-if="queryParams.errType === '2'" v-loading="loading" :data="datatransResultList">
        <el-table-column label="错误信息" align="center" prop="errMsg" width="150px" show-overflow-tooltip />
        <el-table-column label="凭证类型" align="center" prop="dataType" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="bfs_certificate_type" :value="scope.row.dataType" />
          </template>
        </el-table-column>
        <el-table-column label="凭证大类编码" align="center" prop="certificateId" show-overflow-tooltip />
        <el-table-column label="场景编码" align="center" prop="costId" width="150px" show-overflow-tooltip />
        <el-table-column label="总账科目" align="center" prop="finItemCode" width="150px" show-overflow-tooltip/>
        <el-table-column label="明细科目" align="center" prop="subItemCode" width="150px" show-overflow-tooltip />
        <el-table-column label="借贷标记" align="center" prop="finItemType" show-overflow-tooltip>
          <template #default="scope">
            <dict-tag :options="bfs_debit_and_credit" :value="scope.row.finItemType" />
          </template>
        </el-table-column>
        <el-table-column label="金额" align="center" prop="amount" width="150px" show-overflow-tooltip />
        <el-table-column label="记账日期" align="center" prop="accountDate" width="150px">
          <template #default="scope">
            <span>{{ parseTime(scope.row.accountDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="业务号码" align="center" prop="businessNo" width="200px" show-overflow-tooltip />
        <el-table-column label="业务机构" align="center" prop="manageCom" show-overflow-tooltip />
        <el-table-column label="交费年期" align="center" prop="payendyear" show-overflow-tooltip />
        <el-table-column label="保单号" align="center" prop="policyNo" width="200px" show-overflow-tooltip />
        <el-table-column label="分单号" align="center" prop="singlePolicyNo" width="200px" show-overflow-tooltip  />
        <el-table-column label="手续费结算单位" align="center" prop="settlementUnit" width="100px" show-overflow-tooltip />
        <el-table-column label="银行对账码" align="center" prop="reconCode" width="200px" show-overflow-tooltip  />
        <el-table-column label="销售渠道" align="center" prop="saleChnl" show-overflow-tooltip />
        <el-table-column label="往来单位分类" align="center" prop="businessPartners" show-overflow-tooltip />
        <el-table-column label="供应商" align="center" prop="intraBranch" show-overflow-tooltip />
        <el-table-column label="业务性质" align="center" prop="businessNature" show-overflow-tooltip />
        <el-table-column label="原因代码" align="center" prop="cashFlow" show-overflow-tooltip />
        <el-table-column label="开户行" align="center" prop="openingBank" show-overflow-tooltip />
        <el-table-column label="成本中心" align="center" prop="costCenter" show-overflow-tooltip />
        <el-table-column label="税率" align="center" prop="taxRate" show-overflow-tooltip />
        <el-table-column label="税码" align="center" prop="taxCode" show-overflow-tooltip />
        <el-table-column label="税项" align="center" prop="taxClass" show-overflow-tooltip />
        <el-table-column label="承诺项目" align="center" prop="budget" show-overflow-tooltip />
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" @pagination="getList" />
    </el-card>
  </div>
</template>
<script setup lang="ts">
import { parseTime } from '@/utils/ruoyi';
import { getErrorMsgList } from '@/api/bfs/reconciliation';
import {queryManageCom} from "@/api/bfs/cache";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { bfs_buss_system, bfs_error_type } = toRefs<any>(proxy?.useDict('bfs_buss_system', 'bfs_error_type'));
const { bfs_certificate_type, bfs_debit_and_credit } = toRefs<any>(proxy?.useDict('bfs_certificate_type', 'bfs_debit_and_credit'));
const { sys_managecom } = toRefs<any>(proxy?.useDict('sys_managecom'));

const showSearch = ref(true);
const loading = ref(false);
const total = ref(0);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dateRange: undefined,
    errType: undefined,
    manageCom: undefined,
    params: {}
  },
  rules: {
    dateRange: [{ required: true, message: '日志日期不能为空', trigger: 'blur' }],
    errType: [{ required: true, message: '错误类型不能为空', trigger: 'blur' }]
  }
});
const { queryParams, rules } = toRefs(data);
const businessDataList = ref([]);
const datatransResultList = ref([]);
const queryFormRef = ref<ElFormInstance>();
const likeName = ref('');
const filteredManagecom = ref([]);
const sys_managecomCache = ref([]);

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

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

const getList = () => {
  queryFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true;
      const res = await getErrorMsgList(proxy?.addDateRange(queryParams.value, queryParams.value.dateRange), queryParams.value.errType);
      if (queryParams.value.errType === '1') {
        businessDataList.value = res.rows;
        total.value = res.total;
        loading.value = false;
      } else if (queryParams.value.errType === '2') {
        datatransResultList.value = res.rows;
        total.value = res.total;
        loading.value = false;
      }
    }
  });
};
/** 模糊查询 */
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 getCacheManageCom = async () => {
  const res = await queryManageCom();
  if (res.code == 200) {
    sys_managecomCache.value = res.data;
    filteredManagecom.value = sys_managecomCache.value;
  }
};
onMounted(() => {
  getCacheManageCom();
});
</script>

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