<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="合同名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入合同名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="合同类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择合同类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="客户公司id" prop="customerCompanyId">
        <el-input
          v-model="queryParams.customerCompanyId"
          placeholder="请输入客户公司id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="合同进展" prop="progress">
        <el-input
          v-model="queryParams.progress"
          placeholder="请输入合同进展"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="预计签订时间" prop="expectedTime">
        <el-date-picker
          v-model="queryParams.expectedTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="签订时间" prop="signingTime">
        <el-date-picker
          v-model="queryParams.signingTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="归档时间" prop="archiveTime">
        <el-date-picker
          v-model="queryParams.archiveTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONTRACT_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合同金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入合同金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="前期费用" prop="preAmount">
        <el-input
          v-model="queryParams.preAmount"
          placeholder="请输入前期费用"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="设计费" prop="designFee">
        <el-input
          v-model="queryParams.designFee"
          placeholder="请输入设计费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="勘测费" prop="surveyFees">
        <el-input
          v-model="queryParams.surveyFees"
          placeholder="请输入勘测费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="检测费" prop="testingFee">
        <el-input
          v-model="queryParams.testingFee"
          placeholder="请输入检测费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="其他费" prop="otherFee">
        <el-input
          v-model="queryParams.otherFee"
          placeholder="请输入其他费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="计费方式" prop="countType">
        <el-select
          v-model="queryParams.countType"
          placeholder="请选择计费方式"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONTRACT_BILLING_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="建安费" prop="constructionCost">
        <el-input
          v-model="queryParams.constructionCost"
          placeholder="请输入建安费"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="资金来源" prop="source">
        <el-select
          v-model="queryParams.source"
          placeholder="请选择资金来源"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.FUNDS_SOURCE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="收费标准" prop="chargingStandard">
        <el-select
          v-model="queryParams.chargingStandard"
          placeholder="请选择收费标准"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.CONTRACT_FEE_STANDARD)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="优惠" prop="discount">
        <el-input
          v-model="queryParams.discount"
          placeholder="请输入优惠"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否联合体" prop="consortium">
        <el-select
          v-model="queryParams.consortium"
          placeholder="请选择是否联合体"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="联合体单位" prop="consortiumCompany">
        <el-input
          v-model="queryParams.consortiumCompany"
          placeholder="请输入联合体单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="合同提示时间" prop="reminderTime">
        <el-date-picker
          v-model="queryParams.reminderTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审定金额" prop="approvedAmount">
        <el-input
          v-model="queryParams.approvedAmount"
          placeholder="请输入审定金额"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建者" prop="creator">
        <el-input
          v-model="queryParams.creator"
          placeholder="请输入创建者"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="更新者" prop="updater">
        <el-input
          v-model="queryParams.updater"
          placeholder="请输入更新者"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="更新时间" prop="updateTime">
        <el-date-picker
          v-model="queryParams.updateTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="合同id" prop="contractId">
        <el-input
          v-model="queryParams.contractId"
          placeholder="请输入合同id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['cms-ext:ext-contract:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['cms-ext:ext-contract:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="合同名称" align="center" prop="name" />
      <el-table-column label="合同类型" align="center" prop="type" />
      <el-table-column label="客户公司id" align="center" prop="customerCompanyId" />
      <el-table-column label="合同进展" align="center" prop="progress" />
      <el-table-column
        label="预计签订时间"
        align="center"
        prop="expectedTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="签订时间"
        align="center"
        prop="signingTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="归档时间"
        align="center"
        prop="archiveTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONTRACT_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="合同金额" align="center" prop="amount" />
      <el-table-column label="前期费用" align="center" prop="preAmount" />
      <el-table-column label="设计费" align="center" prop="designFee" />
      <el-table-column label="勘测费" align="center" prop="surveyFees" />
      <el-table-column label="检测费" align="center" prop="testingFee" />
      <el-table-column label="其他费" align="center" prop="otherFee" />
      <el-table-column label="计费方式" align="center" prop="countType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONTRACT_BILLING_TYPE" :value="scope.row.countType" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="合同附件url" align="center" prop="contractFileUrl" />
      <el-table-column label="建安费" align="center" prop="constructionCost" />
      <el-table-column label="资金来源" align="center" prop="source">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.FUNDS_SOURCE" :value="scope.row.source" />
        </template>
      </el-table-column>
      <el-table-column label="收费标准" align="center" prop="chargingStandard">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.CONTRACT_FEE_STANDARD" :value="scope.row.chargingStandard" />
        </template>
      </el-table-column>
      <el-table-column label="优惠" align="center" prop="discount" />
      <el-table-column label="是否联合体" align="center" prop="consortium" />
      <el-table-column label="联合体单位" align="center" prop="consortiumCompany" />
      <el-table-column
        label="合同提示时间"
        align="center"
        prop="reminderTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="审定金额" align="center" prop="approvedAmount" />
      <el-table-column label="审核文件url" align="center" prop="reviewFileUrl" />
      <el-table-column label="创建者" align="center" prop="creator" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="更新者" align="center" prop="updater" />
      <el-table-column
        label="更新时间"
        align="center"
        prop="updateTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="合同id" align="center" prop="contractId" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cms-ext:ext-contract:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cms-ext:ext-contract:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ExtContractForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ExtContractApi, ExtContractVO } from '@/api/cms-ext/extcontract'
import ExtContractForm from './ExtContractForm.vue'

/** 外部合同 列表 */
defineOptions({ name: 'ExtContract' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ExtContractVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  type: undefined,
  customerCompanyId: undefined,
  progress: undefined,
  expectedTime: [],
  signingTime: [],
  archiveTime: [],
  status: undefined,
  amount: undefined,
  preAmount: undefined,
  designFee: undefined,
  surveyFees: undefined,
  testingFee: undefined,
  otherFee: undefined,
  countType: undefined,
  remark: undefined,
  contractFileUrl: undefined,
  constructionCost: undefined,
  source: undefined,
  chargingStandard: undefined,
  discount: undefined,
  consortium: undefined,
  consortiumCompany: undefined,
  reminderTime: [],
  approvedAmount: undefined,
  reviewFileUrl: undefined,
  creator: undefined,
  createTime: [],
  updater: undefined,
  updateTime: [],
  contractId: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ExtContractApi.getExtContractPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

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

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

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ExtContractApi.deleteExtContract(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ExtContractApi.exportExtContract(queryParams)
    download.excel(data, '外部合同.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>