<script setup name="delayApply">
import { ref, reactive, onMounted } from 'vue'
import { declareExport } from '@/http/apis/declareMange'
import { useRouter } from 'vue-router'
import useExportExc from '@/utils/useExportExc'
import { list } from '@/http/apis/declareMange/delayApply'
import ApplyDialog from './components/applyDialog.vue'
import store from '@/store'
const { projectTypeOptions } = store.dictStore.globalDicts || {}
const router = useRouter()
// 搜索栏表单数据
const searchForm = reactive({
    projectType: undefined,
    status: undefined,
    projectYear: undefined,
    projectName: undefined,
    createOnMin: undefined,
    createOnMax: undefined,
    times: []
  }),
  column = reactive([
    {
      label: '序号',
      type: 'index',
      width: '80'
    },
    {
      label: '项目名称',
      key: 'projectName',
      prop: 'projectName',
      minWidth: '200',
      showOverflowTooltip: true
    },
    {
      label: '项目类型',
      key: 'projectTypeName',
      prop: 'projectTypeName',
      width: '80'
    },
    {
      label: '批复金额（万元）',
      key: 'approvedAmount',
      prop: 'approvedAmount',
      width: '150'
    },
    {
      label: '预算年度',
      key: 'projectYear',
      prop: 'projectYear',
      width: 80
    },
    {
      label: '计划验收时间',
      key: 'planAcceptanceTime',
      prop: 'planAcceptanceTime',
      width: '200'
    },
    {
      label: '操作',
      slot: 'action',
      width: '180',
      fixed: 'right'
    }
  ]),
  data = ref([]),
  tableListRef = ref(),
  getTableData = async (pageParams = tableListRef.value.pageParams) => {
    const res = await list({
      ...pageParams,
      ...searchForm,
      createOnMin: searchForm.times?.[0],
      createOnMax: searchForm.times?.[1],
      projectYear: searchForm.projectYear * 1 || undefined,
      times: undefined
    })
    data.value = res.data.records
    total.value = res.data.total
  },
  // 数据总数
  total = ref(2),
  // 提交查询
  search = () => {
    getTableData()
  },
  // 重置
  formReset = () => {
    searchForm.projectYear = undefined
    searchForm.projectName = undefined
    searchForm.projectType = undefined
    searchForm.createOnMin = undefined
    searchForm.createOnMax = undefined
    searchForm.times = undefined
    tableListRef.value.pageParams.pageNumber = 1
    tableListRef.value.pageParams.pageSize = 10
    getTableData()
  },
  // 导出excel文件
  { exportLoading, exportData } = useExportExc(),
  handleExcel = () => {
    exportData(() => declareExport(10, {
      ...searchForm,
      createOnMin: searchForm.times?.[0],
      createOnMax: searchForm.times?.[1],
      projectYear: searchForm.projectYear * 1 || undefined,
      times: undefined
    }))
  },
  // 申请延期
  applyDelay = (data) => {
    dialogData.visible = true
    dialogData.data = data
  },
  dialogData = reactive({
    visible: undefined,
    data: undefined
  }),
  close = (flag) => {
    dialogData.visible = false
    flag && getTableData()
  }
onMounted(async () => {
  getTableData()
})
</script>
<template>
  <el-card class="w-full search">
    <el-form :model="searchForm" size="small" label-suffix=":">
      <el-row :gutter="16" class="mb-16">
        <el-col :span="8">
          <el-form-item label="项目名称">
            <el-input
              v-model="searchForm.projectName"
              maxlength="50"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目类型">
            <el-select
              v-model="searchForm.projectType"
              placeholder="全部"
              class="w-full"
            >
              <el-option
                v-for="(v,k) in projectTypeOptions"
                :key="k"
                :label="v"
                :value="k"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="计划验收时间">
            <el-date-picker
              v-model="searchForm.times"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD HH:mm"
              value-format="YYYY-MM-DD HH:mm"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item label="创建时间">
            <el-date-picker
              v-model="searchForm.times"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              format="YYYY-MM-DD HH:mm"
              value-format="YYYY-MM-DD HH:mm"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item class="btn">
            <div class="flex">
              <el-button type="primary" @click="search">查询</el-button>
              <el-button @click="formReset">重置</el-button>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </el-card>
  <el-card class="w-full mt-8">
    <template #header>
      <div class="flex justify-between">
        <span>列表</span>
        <div>
          <el-button
            type="primary"
            size="small"
            plain
            :loading="exportLoading"
            @click="handleExcel"
          >
            导出
          </el-button>
        </div>
      </div>
    </template>
    <table-list
      ref="tableListRef"
      :column="column"
      :data="data"
      :total="total"
      @get-table-data="getTableData"
    >
      <template #action="{ scope }">
        <a v-if="scope.row.canDelayApply" @click="applyDelay(scope.row)">申请延期</a>
        <a @click="router.push({name:'delayApplyDetail',query:{id:scope.row.id}})">详情</a>
      </template>
    </table-list>
  </el-card>
  <apply-dialog :visible="dialogData.visible" :data="dialogData.data" @close="close" />
</template>
<style lang="less" scoped></style>
