<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="自增字段，唯一" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入自增字段，唯一"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="计划单号" prop="planNo">
        <el-input
          v-model="queryParams.planNo"
          placeholder="请输入计划单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="项目id" prop="projectId">
        <el-input
          v-model="queryParams.projectId"
          placeholder="请输入项目id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="项目负责人" prop="projectOwner">
        <el-input
          v-model="queryParams.projectOwner"
          placeholder="请输入项目负责人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否需要工艺:1表示需要，0表示不需要" prop="hasCraft">
        <el-select
          v-model="queryParams.hasCraft"
          placeholder="请选择是否需要工艺:1表示需要，0表示不需要"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.HELI_COMMON_IS_OR_NOT)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工艺负责人" prop="craftOwner">
        <el-input
          v-model="queryParams.craftOwner"
          placeholder="请输入工艺负责人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="工艺开始日期" prop="craftStartDate">
        <el-date-picker
          v-model="queryParams.craftStartDate"
          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="craftEndDate">
        <el-date-picker
          v-model="queryParams.craftEndDate"
          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="editor">
        <el-input
          v-model="queryParams.editor"
          placeholder="请输入编辑人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="编辑日期" prop="editorDate">
        <el-date-picker
          v-model="queryParams.editorDate"
          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="auditor">
        <el-input
          v-model="queryParams.auditor"
          placeholder="请输入审核人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审核日期" prop="auditDate">
        <el-date-picker
          v-model="queryParams.auditDate"
          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="approver">
        <el-input
          v-model="queryParams.approver"
          placeholder="请输入批准人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="批准日期" prop="approveDate">
        <el-date-picker
          v-model="queryParams.approveDate"
          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="状态,1未开始，2生产中，3已完成，4已终止 ，默认是1" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态,1未开始，2生产中，3已完成，4已终止 ，默认是1"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="项目变更次数" prop="changeNum">
        <el-input
          v-model="queryParams.changeNum"
          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>
        <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="['heli:plan:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['heli:plan: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="id" />
      <el-table-column label="计划单号" align="center" prop="planNo" />
      <el-table-column label="项目id" align="center" prop="projectId" />
      <el-table-column label="项目负责人" align="center" prop="projectOwner" />
      <el-table-column label="是否需要工艺:1表示需要，0表示不需要" align="center" prop="hasCraft">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.HELI_COMMON_IS_OR_NOT" :value="scope.row.hasCraft" />
        </template>
      </el-table-column>
      <el-table-column label="工艺负责人" align="center" prop="craftOwner" />
      <el-table-column
        label="工艺开始日期"
        align="center"
        prop="craftStartDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="工艺结束日期"
        align="center"
        prop="craftEndDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="工艺流程数组信息[{'id':1,'sdate':'','edate':'','owner':'','description':''}]" align="center" prop="craftContent" />
      <el-table-column label="编辑人" align="center" prop="editor" />
      <el-table-column
        label="编辑日期"
        align="center"
        prop="editorDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="审核人" align="center" prop="auditor" />
      <el-table-column
        label="审核日期"
        align="center"
        prop="auditDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="批准人" align="center" prop="approver" />
      <el-table-column
        label="批准日期"
        align="center"
        prop="approveDate"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="备注" align="center" prop="description" />
      <el-table-column label="状态,1未开始，2生产中，3已完成，4已终止 ，默认是1" align="center" prop="status" />
      <el-table-column label="项目变更次数" align="center" prop="changeNum" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['heli:plan:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['heli:plan: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>

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

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as PlanApi from '@/api/heli/plan'
import PlanForm from './PlanForm.vue'

defineOptions({ name: 'Plan' })

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

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  id: undefined,
  planNo: undefined,
  projectId: undefined,
  projectOwner: undefined,
  hasCraft: undefined,
  craftOwner: undefined,
  craftStartDate: [],
  craftEndDate: [],
  editor: undefined,
  editorDate: [],
  auditor: undefined,
  auditDate: [],
  approver: undefined,
  approveDate: [],
  description: undefined,
  status: undefined,
  changeNum: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

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

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

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