<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" row-key="id" :request-api="listCarryChecklistApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="['als:carryChecklist:add']" icon="CirclePlus" @click="openDialog(1, '任务携行新增')"> 新增 </el-button>
        <el-button type="primary" v-auth="['als:carryChecklist:import']" icon="Upload" plain @click="batchAdd"> 导入 </el-button>
        <el-button type="primary" v-auth="['als:carryChecklist:export']" icon="Download" plain @click="downloadFile"> 导出 </el-button>
        <el-button
          type="danger"
          v-auth="['als:carryChecklist:remove']"
          icon="Delete"
          plain
          :disabled="!scope.isSelected"
          @click="batchDelete(scope.selectedListIds)"
        >
          批量删除
        </el-button>
      </template>
      <!-- 表格操作 -->
      <template #operation="scope">
        <el-button type="primary" link icon="View" v-auth="['als:carryChecklist:query']" @click="openDialog(3, '任务携行查看', scope.row)">
          查看
        </el-button>
        <el-button type="primary" link icon="EditPen" v-auth="['als:carryChecklist:edit']" @click="openDialog(2, '任务携行编辑', scope.row)">
          编辑
        </el-button>
        <el-button type="primary" link icon="Delete" v-auth="['als:carryChecklist:remove']" @click="deleteCarryChecklist(scope.row)">
          删除
        </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" :items-options="itemsOptions" :model="model" />
    <ImportExcel ref="dialogRef" />
    <TaskDialog ref="taskDialogRef" />
  </div>
</template>

<script setup lang="tsx" name="CarryChecklist">
import { useHandleData } from '@/hooks/useHandleData'
import { ElMessageBox } from 'element-plus'
import ImportExcel from '@/components/ImportExcel/index.vue'
import TaskDialog from '@/components/TaskDialog/index.vue'
import FormDialog from '@/components/FormDialog/index.vue'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import {
  listCarryChecklistApi,
  delCarryChecklistApi,
  addCarryChecklistApi,
  updateCarryChecklistApi,
  importTemplateApi,
  importCarryChecklistDataApi,
  exportCarryChecklistApi,
  getCarryChecklistApi
} from '@/api/modules/als/carryChecklist'
// ProTable 实例
const proTable = ref<ProTableInstance>()
// 表单model
const model = ref({})
// 删除任务携行信息
const deleteCarryChecklist = async (params: any) => {
  await useHandleData(delCarryChecklistApi, params.id, '删除【' + params.id + '】任务携行')
  proTable.value?.getTableList()
}

// 批量删除任务携行信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(delCarryChecklistApi, ids, '删除所选任务携行信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

const taskDialogRef = ref<InstanceType<typeof TaskDialog> | null>(null)
// 导出任务携行列表
const downloadFile = async () => {
  ElMessageBox.confirm('确认导出任务携行数据?', '温馨提示', { type: 'warning' }).then(async () => {
    exportCarryChecklistApi(proTable.value?.searchParam)
    taskDialogRef.value?.openExportDialog()
  })
}

// 批量添加任务携行
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null)
const batchAdd = () => {
  const params = {
    title: '任务携行',
    tempApi: importTemplateApi,
    importApi: importCarryChecklistDataApi,
    getTableList: proTable.value?.getTableList
  }
  dialogRef.value?.acceptParams(params)
}

const formDialogRef = ref<InstanceType<typeof FormDialog> | null>(null)
// 打开弹框的功能
const openDialog = async (type: number, title: string, row?: any) => {
  let res = { data: {} }
  if (row?.id) {
    res = await getCarryChecklistApi(row?.id || null)
  }
  model.value = type == 1 ? {} : res.data
  // 重置表单
  setItemsOptions()
  const params = {
    title,
    width: 580,
    isEdit: type !== 3,
    api: type == 1 ? addCarryChecklistApi : updateCarryChecklistApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { prop: 'id', label: '编号' },
  {
    prop: 'aircraftModel',
    label: '机型',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'aircraftNo',
    label: '机号',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'name',
    label: '成品名称',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'model',
    label: '型号',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'factory',
    label: '厂家名称',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'quantity',
    label: '需求数量',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'remark',
    label: '备注',
    width: 120
  },
  {
    prop: 'createBy',
    label: '创建人',
    width: 120
  },
  {
    prop: 'createTime',
    label: '创建时间',
    width: 120
  },
  {
    prop: 'updateBy',
    label: '更新人',
    width: 120
  },
  {
    prop: 'updateTime',
    label: '更新时间',
    width: 120
  },
  { prop: 'operation', label: '操作', width: 230, fixed: 'right' }
])
// 表单配置项
let itemsOptions = reactive<ProForm.ItemsOptions[]>([])
const setItemsOptions = () => {
  itemsOptions = [
    {
      label: '机型',
      prop: 'aircraftModel',
      rules: [{ required: true, message: '机型不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入机型'
      }
    },
    {
      label: '机号',
      prop: 'aircraftNo',
      rules: [{ required: true, message: '机号不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入机号'
      }
    },
    {
      label: '成品名称',
      prop: 'name',
      rules: [{ required: true, message: '成品名称不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入成品名称'
      }
    },
    {
      label: '型号',
      prop: 'model',
      rules: [{ required: true, message: '型号不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入型号'
      }
    },
    {
      label: '厂家名称',
      prop: 'factory',
      rules: [{ required: true, message: '厂家名称不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入厂家名称'
      }
    },
    {
      label: '需求数量',
      prop: 'quantity',
      rules: [{ required: true, message: '需求数量不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入需求数量'
      }
    },
    {
      label: '备注',
      prop: 'remark',
      compOptions: {
        placeholder: '请输入备注'
      }
    }
  ]
}
</script>
