<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" :inline="true" label-width="80px">
            <el-row>
              <el-form-item label="凭证大类" prop="certificateId">
                <el-select v-model="queryParams.certificateId" clearable placeholder="请选择凭证大类">
                  <el-option v-for="dict in certificateList" :key="dict.certificateId" :label="dict.certificateName" :value="dict.certificateId" />
                </el-select>
              </el-form-item>
              <el-form-item label="费用类型" prop="costId">
                <el-select v-model="queryParams.costId" clearable placeholder="请选择费用类型">
                  <el-option v-for="dict in costList" :key="dict.costId" :label="dict.costName" :value="dict.costId" />
                </el-select>
              </el-form-item>
              <el-form-item label="保单号码" prop="policyNo">
                <el-input v-model="queryParams.policyNo" placeholder="请输入保单号码" clearable />
              </el-form-item>
              <el-form-item label="业务日期" prop="accountDate">
                <el-date-picker
                  v-model="queryParams.accountDate"
                  clearable
                  type="date"
                  value-format="YYYY-MM-DD"
                  placeholder="请选择过期时间"
                  style="width: 240px"
                  :disabled-date="pickerOptions"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="业务号码" prop="businessNo">
                <el-input v-model="queryParams.businessNo" placeholder="请输入业务号码" clearable />
              </el-form-item>
              <el-form-item label="凭证类型" prop="dataType">
                <el-select v-model="queryParams.dataType" clearable placeholder="请选择凭证类型">
                  <el-option v-for="dict in bfs_certificate_type" :key="dict.value" :label="dict.label" :value="dict.value" />
                </el-select>
              </el-form-item>
            </el-row>
            <el-form-item>
              <el-button v-hasPermi="['bfs:redivoapply:list']" type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>

        <el-card shadow="hover">
          <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="costResultList">
            <el-table-column label="凭证大类编码" align="center" prop="certificateId" />
            <el-table-column label="场景编码" align="center" prop="costId" width="150px" />
            <el-table-column label="总账科目" align="center" prop="finItemCode" width="150px"/>
            <el-table-column label="明细科目" align="center" prop="subItemCode" width="150px"/>
            <el-table-column label="借贷标记" align="center" prop="finItemType" >
              <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" />
            <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"/>
            <el-table-column label="业务机构" align="center" prop="manageCom" />
            <el-table-column label="交费年期" align="center" prop="payendyear" />
            <el-table-column label="保单号" align="center" prop="policyNo" width="200px" />
            <el-table-column label="分单号" align="center" prop="singlePolicyNo" width="200px" />
            <el-table-column label="手续费结算单位" align="center" prop="settlementUnit" width="100px"/>
            <el-table-column label="银行对账码" align="center" prop="reconCode" width="200px"/>
            <el-table-column label="销售渠道" align="center" prop="saleChnl" />
            <el-table-column label="往来单位分类" align="center" prop="businessPartners" />
            <el-table-column label="供应商" align="center" prop="intraBranch" />
            <el-table-column label="业务性质" align="center" prop="businessNature" />
            <el-table-column label="原因代码" align="center" prop="cashFlow" />
            <el-table-column label="开户行" align="center" prop="openingBank" />
            <el-table-column label="成本中心" align="center" prop="costCenter" />
            <el-table-column label="税率" align="center" prop="taxRate" />
            <el-table-column label="税码" align="center" prop="taxCode" />
            <el-table-column label="税项" align="center" prop="taxClass" />
            <el-table-column label="承诺项目" align="center" prop="budget" />
          </el-table>
          <pagination
            v-show="total > 0"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            :total="total"
            @pagination="getList"
          />
        </el-card>
      </div>
    </transition>
  </div>

  <el-dialog v-model="dialog.visible" :title="dialog.title" width="800px" append-to-body>
    <el-form ref="redFormRef" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="业务号码" prop="businessNo">
            <el-input v-model="form.businessNo" placeholder="请输入业务号码" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="红冲类型" prop="invoicingType">
            <el-select v-model="form.invoicingType" @change="invoicingTypeChange">
              <el-option v-for="item in bfs_invoicing_type" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col v-if="showCertificateId" :span="12">
          <el-form-item v-if="showCertificateId" label="凭证类型编号" prop="certificateId">
            <el-select v-model="form.certificateId">
              <el-option v-for="item in busCertificateList" :key="item.certificateId" :label="item.certificateName" :value="item.certificateId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="红冲原因" prop="invoicingReason">
            <el-select v-model="form.invoicingReason">
              <el-option v-for="item in bfs_invoicing_reason" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="原因描述" prop="invoicingDetail">
          <el-input v-model="form.invoicingDetail" type="textarea" style="width: 290px" :rows="4" placeholder="请输入原因描述"></el-input>
        </el-form-item>
      </el-row>
      <!-- 隐藏信息 -->
      <el-col v-if="false" :span="12">
        <el-form-item v-if="false" label="红冲数据id" prop="businessDataId">
          <el-input v-model="form.businessDataId" placeholder="红冲数据id"></el-input>
        </el-form-item>
      </el-col>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup name="RedInvoicingApply" lang="ts">
import { getCacheList, getCertificateByBusinessNo, getInvoicingList, redInvoicingApply } from '@/api/bfs/busnessQury/redInvoicing';
import { pickerOptions } from '@/utils/date';

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

const showSearch = ref(true);
const initFormData = {
  id: undefined,
  businessNo: '',
  certificateId: '',
  policySource: '',
  businessDataId: '',
  invoicingReason: '',
  invoicingDetail: '',
  invoicingType: '',
  status: '0',
  applyBy: ''
};
const data = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    certificateId: undefined,
    costId: undefined,
    policyNo: undefined,
    manageCom: undefined,
    businessNo: undefined,
    accountDate: undefined,
    dataType: undefined,
    params: {}
  },
  rules: {}
});
const { form, queryParams, rules } = toRefs(data);
const certificateList = ref([]);
const costList = ref([]);
const dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const loading = ref(false);
const costResultList = ref([]);
const total = ref(0);
const redFormRef = ref<ElFormInstance>();
const queryFormRef = ref<ElFormInstance>();
const showCertificateId = ref(false);
const busCertificateList = ref([]);

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

/** 查询字典下拉信息 */
const getCache = async () => {
  const res = await getCacheList();
  certificateList.value = res.data.certificateTypeCache;
  costList.value = res.data.costTypeCache;
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  queryParams.value.pageNum = 1;
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 查询科目明细 */
const getList = async () => {
  loading.value = true;
  const res = await getInvoicingList(queryParams.value);
  costResultList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
const getCertificateNameById = (id) => {
  const item = certificateList.value.find((item) => item.certificateId === id);
  return item ? item.certificateName : '';
};

const getCostNameById = (id) => {
  const item = costList.value.find((item) => item.costId === id);
  return item ? item.costName : '';
};

const handleRedInvoic = async (row) => {
  reset();
  dialog.visible = true;
  dialog.title = '红冲申请';
  form.value.businessNo = row.businessNo;
  form.value.businessDataId = row.businessDataId;
  const res = await getCertificateByBusinessNo(form.value);
  if (res.code === 200) {
    busCertificateList.value = res.data;
  }
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  redFormRef.value?.resetFields();
};

/** 取消按钮 */
const cancel = () => {
  dialog.visible = false;
  reset();
};

/** 红冲类型选择 **/
const invoicingTypeChange = (value) => {
  if (value === '1') {
    showCertificateId.value = false;
  } else {
    showCertificateId.value = true;
  }
};

/** 提交按钮 */
const submitForm = async () => {
  redFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      const res = await redInvoicingApply(form.value);
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

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

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