<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="合同进展" 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="printingTime">
        <el-date-picker
          v-model="queryParams.printingTime"
          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 label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="计费方式" prop="countType">
        <el-select
          v-model="queryParams.countType"
          placeholder="请选择计费方式"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" 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="合同url" prop="contractFileUrl">
        <el-input
          v-model="queryParams.contractFileUrl"
          placeholder="请输入合同url"
          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-input
          v-model="queryParams.source"
          placeholder="请输入资金来源"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </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="extProportion">
        <el-input
          v-model="queryParams.extProportion"
          placeholder="请输入占主合同比例"
          clearable
          @keyup.enter="handleQuery"
          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="是否删除" prop="deleted">
        <el-select
          v-model="queryParams.deleted"
          placeholder="请选择是否删除"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="租户编号" prop="tenantId">
        <el-input
          v-model="queryParams.tenantId"
          placeholder="请输入租户编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </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="designAmount">
        <el-input
          v-model="queryParams.designAmount"
          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="measurementFee">
        <el-input
          v-model="queryParams.measurementFee"
          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>
        <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:contract:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['cms: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="合同进展" align="center" prop="progress" />
      <el-table-column
        label="合同拟定时间"
        align="center"
        prop="expectedTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="合同用印时间"
        align="center"
        prop="printingTime"
        :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" />
      <el-table-column label="计费方式" align="center" prop="countType" />
      <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" />
      <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="extProportion" />
      <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="是否删除" align="center" prop="deleted" />
      <el-table-column label="租户编号" align="center" prop="tenantId" />
      <el-table-column label="签订合同总额" align="center" prop="amount" />
      <el-table-column label="前期费" align="center" prop="preAmount" />
      <el-table-column label="设计费" align="center" prop="designAmount" />
      <el-table-column label="勘测费" align="center" prop="surveyFees" />
      <el-table-column label="测量费" align="center" prop="measurementFee" />
      <el-table-column label="其他费" align="center" prop="otherFee" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['cms:contract:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['cms: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>

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

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { ContractApi, ContractVO } from '@/api/cms/contract'
import ContractForm from './ContractForm.vue'

/** 合同 列表 */
defineOptions({ name: 'Contract' })

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

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

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await ContractApi.getContractPage(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 ContractApi.deleteContract(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

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

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