<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" row-key="id" :request-api="listMaintenanceRecordApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="['als:maintenanceRecord:add']" icon="CirclePlus" @click="openDialog(1, '维修记录新增')"> 新增 </el-button>
        <el-button type="primary" v-auth="['als:maintenanceRecord:import']" icon="Upload" plain @click="batchAdd"> 导入 </el-button>
        <el-button type="primary" v-auth="['als:maintenanceRecord:export']" icon="Download" plain @click="downloadFile"> 导出 </el-button>
        <el-button
          type="danger"
          v-auth="['als:maintenanceRecord: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:maintenanceRecord:query']" @click="openDialog(3, '维修记录查看', scope.row)">
          查看
        </el-button>
        <el-button type="primary" link icon="EditPen" v-auth="['als:maintenanceRecord:edit']" @click="openDialog(2, '维修记录编辑', scope.row)">
          编辑
        </el-button>
        <el-button type="primary" link icon="Delete" v-auth="['als:maintenanceRecord:remove']" @click="deleteMaintenanceRecord(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="MaintenanceRecord">
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 {
  listMaintenanceRecordApi,
  delMaintenanceRecordApi,
  addMaintenanceRecordApi,
  updateMaintenanceRecordApi,
  importTemplateApi,
  importMaintenanceRecordDataApi,
  exportMaintenanceRecordApi,
  getMaintenanceRecordApi
} from '@/api/modules/als/maintenanceRecord'
const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { sys_yes_no, sys_app_type } = toRefs<any>(proxy?.useDict('sys_yes_no', 'sys_app_type'))

// ProTable 实例
const proTable = ref<ProTableInstance>()
// 表单model
const model = ref({})
// 删除维修记录信息
const deleteMaintenanceRecord = async (params: any) => {
  await useHandleData(delMaintenanceRecordApi, params.id, '删除【' + params.id + '】维修记录')
  proTable.value?.getTableList()
}

// 批量删除维修记录信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(delMaintenanceRecordApi, ids, '删除所选维修记录信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

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

// 批量添加维修记录
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null)
const batchAdd = () => {
  const params = {
    title: '维修记录',
    tempApi: importTemplateApi,
    importApi: importMaintenanceRecordDataApi,
    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 getMaintenanceRecordApi(row?.id || null)
  }
  model.value = type == 1 ? {} : res.data
  // 重置表单
  setItemsOptions()
  const params = {
    title,
    width: 900,
    isEdit: type !== 3,
    api: type == 1 ? addMaintenanceRecordApi : updateMaintenanceRecordApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { prop: 'id', label: '编号' },
  {
    prop: 'aircraftType',
    label: '机型',
    tag: true,
    enum: sys_yes_no,
    search: {
      el: 'tree-select'
    }
  },
  {
    prop: 'aircraftNo',
    label: '机号',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'serialNumber',
    label: '出厂号码',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'specialty',
    label: '专业',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'systemName',
    label: '系统',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'engineType',
    label: '发动机型别',
    tag: true,
    enum: sys_app_type,
    search: {
      el: 'tree-select'
    }
  },
  {
    prop: 'engineSerialNumber',
    label: '发动机号码',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'unitName',
    label: '单位',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'faultPhenomenon',
    label: '故障现象',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'locationFault',
    label: '故障发生地点',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'discoveryTime',
    label: '发现时机',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'itemName',
    label: '故障件名称',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'itemType',
    label: '故障件型别',
    tag: true,
    enum: sys_app_type,
    search: {
      el: 'tree-select'
    }
  },
  {
    prop: 'partNumber',
    label: '故换件号码',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'partType',
    label: '故换件型别',
    tag: true,
    enum: sys_app_type,
    search: {
      el: 'tree-select'
    }
  },
  {
    prop: 'faultCode',
    label: '故障失常码',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'installationDate',
    label: '故障件装机日期',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  },
  {
    prop: 'causeFault',
    label: '故障原因',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'natureFault',
    label: '故障性质',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'liabilityFault',
    label: '故障责任',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'consequencesFault',
    label: '故障后果',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'occurDate',
    label: '发生日期',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  },
  {
    prop: 'methodDetermination',
    label: '判明方法',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'methodRectification',
    label: '排除方法',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'discoveredBy',
    label: '发现人',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'rectificationBy',
    label: '排故人',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'reviewerBy',
    label: '审核人',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'rectificationNumber',
    label: '排故人数',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'remarks',
    label: '备注',
    search: {
      el: 'input'
    },
    width: 120
  },
  { prop: 'operation', label: '操作', width: 230, fixed: 'right' }
])
// 表单配置项
let itemsOptions = reactive<ProForm.ItemsOptions[]>([])
const setItemsOptions = () => {
  itemsOptions = [
    {
      label: '机型',
      prop: 'aircraftType',
      rules: [{ required: true, message: '机型不能为空', trigger: 'change' }],
      span: 12,
      compOptions: {
        elTagName: 'select',
        enum: sys_yes_no.value,
        placeholder: '请选择机型'
      }
    },
    {
      label: '机号',
      prop: 'aircraftNo',
      rules: [{ required: true, message: '机号不能为空', trigger: 'blur' }],
      span: 12,
      compOptions: {
        placeholder: '请输入机号'
      }
    },
    {
      label: '出厂号码',
      prop: 'serialNumber',
      rules: [{ required: true, message: '出厂号码不能为空', trigger: 'blur' }],
      span: 12,
      compOptions: {
        placeholder: '请输入出厂号码'
      }
    },
    {
      label: '专业',
      prop: 'specialty',
      span: 12,
      compOptions: {
        placeholder: '请输入专业'
      }
    },
    {
      label: '系统',
      prop: 'systemName',
      span: 12,
      compOptions: {
        placeholder: '请输入系统'
      }
    },
    {
      label: '发动机型别',
      prop: 'engineType',
      span: 12,
      compOptions: {
        elTagName: 'select',
        enum: sys_app_type.value,
        placeholder: '请选择发动机型别'
      }
    },
    {
      label: '发动机号码',
      prop: 'engineSerialNumber',
      span: 12,
      compOptions: {
        placeholder: '请输入发动机号码'
      }
    },
    {
      label: '单位',
      prop: 'unitName',
      span: 12,
      compOptions: {
        placeholder: '请输入单位'
      }
    },
    {
      label: '故障现象',
      prop: 'faultPhenomenon',
      span: 12,
      compOptions: {
        placeholder: '请输入故障现象'
      }
    },
    {
      label: '故障发生地点',
      prop: 'locationFault',
      span: 12,
      compOptions: {
        placeholder: '请输入故障发生地点'
      }
    },
    {
      label: '发现时机',
      prop: 'discoveryTime',
      span: 12,
      compOptions: {
        placeholder: '请输入发现时机'
      }
    },
    {
      label: '故障件名称',
      prop: 'itemName',
      span: 12,
      compOptions: {
        placeholder: '请输入故障件名称'
      }
    },
    {
      label: '故障件型别',
      prop: 'itemType',
      span: 12,
      compOptions: {
        elTagName: 'select',
        enum: sys_app_type.value,
        placeholder: '请选择故障件型别'
      }
    },
    {
      label: '故换件号码',
      prop: 'partNumber',
      span: 12,
      compOptions: {
        placeholder: '请输入故换件号码'
      }
    },
    {
      label: '故换件型别',
      prop: 'partType',
      rules: [{ required: true, message: '故换件型别不能为空', trigger: 'change' }],
      span: 12,
      compOptions: {
        elTagName: 'select',
        enum: sys_app_type.value,
        placeholder: '请选择故换件型别'
      }
    },
    {
      label: '故障失常码',
      prop: 'faultCode',
      rules: [{ required: true, message: '故障失常码不能为空', trigger: 'blur' }],
      span: 12,
      compOptions: {
        placeholder: '请输入故障失常码'
      }
    },
    {
      label: '故障件装机日期',
      prop: 'installationDate',
      span: 12,
      compOptions: {
        elTagName: 'date-picker',
        type: 'date',
        placeholder: '请选择故障件装机日期'
      }
    },
    {
      label: '故障原因',
      prop: 'causeFault',
      span: 12,
      compOptions: {
        placeholder: '请输入故障原因'
      }
    },
    {
      label: '故障性质',
      prop: 'natureFault',
      span: 12,
      compOptions: {
        placeholder: '请输入故障性质'
      }
    },
    {
      label: '故障责任',
      prop: 'liabilityFault',
      span: 12,
      compOptions: {
        placeholder: '请输入故障责任'
      }
    },
    {
      label: '故障后果',
      prop: 'consequencesFault',
      span: 12,
      compOptions: {
        placeholder: '请输入故障后果'
      }
    },
    {
      label: '发生日期',
      prop: 'occurDate',
      span: 12,
      compOptions: {
        elTagName: 'date-picker',
        type: 'date',
        placeholder: '请选择发生日期'
      }
    },
    {
      label: '判明方法',
      prop: 'methodDetermination',
      span: 12,
      compOptions: {
        placeholder: '请输入判明方法'
      }
    },
    {
      label: '排除方法',
      prop: 'methodRectification',
      span: 12,
      compOptions: {
        placeholder: '请输入排除方法'
      }
    },
    {
      label: '发现人',
      prop: 'discoveredBy',
      span: 12,
      compOptions: {
        placeholder: '请输入发现人'
      }
    },
    {
      label: '排故人',
      prop: 'rectificationBy',
      span: 12,
      compOptions: {
        placeholder: '请输入排故人'
      }
    },
    {
      label: '审核人',
      prop: 'reviewerBy',
      span: 12,
      compOptions: {
        placeholder: '请输入审核人'
      }
    },
    {
      label: '排故人数',
      prop: 'rectificationNumber',
      span: 12,
      compOptions: {
        placeholder: '请输入排故人数'
      }
    },
    {
      label: '备注',
      prop: 'remarks',
      span: 12,
      compOptions: {
        placeholder: '请输入备注'
      }
    }
  ]
}
</script>
