<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" row-key="id" :request-api="listFaultCaseApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="primary" v-auth="['als:faultCase:add']" icon="CirclePlus" @click="openDialog(1, '故障案例新增')"> 新增 </el-button>
        <el-button type="primary" v-auth="['als:faultCase:import']" icon="Upload" plain @click="batchAdd"> 导入 </el-button>
        <el-button type="primary" v-auth="['als:faultCase:export']" icon="Download" plain @click="downloadFile"> 导出 </el-button>
        <el-button
          type="danger"
          v-auth="['als:faultCase: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:faultCase:query']" @click="openDialog(3, '故障案例查看', scope.row)">
          查看
        </el-button>
        <el-button type="primary" link icon="EditPen" v-auth="['als:faultCase:edit']" @click="openDialog(2, '故障案例编辑', scope.row)">
          编辑
        </el-button>
        <el-button type="primary" link icon="Delete" v-auth="['als:faultCase:remove']" @click="deleteFaultCase(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" :items-options="itemsOptions" :model="model" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="FaultCase">
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { ElMessageBox } from 'element-plus'
import ImportExcel from '@/components/ImportExcel/index.vue'
import FormDialog from '@/components/FormDialog/index.vue'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import {
  listFaultCaseApi,
  delFaultCaseApi,
  addFaultCaseApi,
  updateFaultCaseApi,
  importTemplateApi,
  importFaultCaseDataApi,
  exportFaultCaseApi,
  getFaultCaseApi
} from '@/api/modules/als/faultCase'

// ProTable 实例
const proTable = ref<ProTableInstance>()
// 表单model
const model = ref({})
// 删除故障案例信息
const deleteFaultCase = async (params: any) => {
  await useHandleData(delFaultCaseApi, params.id, '删除【' + params.id + '】故障案例')
  proTable.value?.getTableList()
}

// 批量删除故障案例信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(delFaultCaseApi, ids, '删除所选故障案例信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

// 导出故障案例列表
const downloadFile = async () => {
  ElMessageBox.confirm('确认导出故障案例数据?', '温馨提示', { type: 'warning' }).then(() =>
    useDownload(exportFaultCaseApi, '故障案例列表', proTable.value?.searchParam)
  )
}

// 批量添加故障案例
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null)
const batchAdd = () => {
  const params = {
    title: '故障案例',
    tempApi: importTemplateApi,
    importApi: importFaultCaseDataApi,
    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 getFaultCaseApi(row?.id || null)
  }
  model.value = type == 1 ? {} : res.data
  // 重置表单
  setItemsOptions()
  const params = {
    title,
    width: 580,
    isEdit: type !== 3,
    api: type == 1 ? addFaultCaseApi : updateFaultCaseApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  {
    prop: 'aircraftType',
    label: '机型',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'unitName',
    label: '单位',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'itemName',
    label: '故障件名称',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'specialty',
    label: '专业',
    width: 120
  },
  {
    prop: 'faultPhenomenon',
    label: '故障现象',
    width: 120
  },
  {
    prop: 'engineType',
    label: '发动机型别',
    width: 120
  },
  {
    prop: 'locationFault',
    label: '故障发生地点',
    width: 120
  },
  {
    prop: 'itemType',
    label: '故障件型别',
    width: 120
  },
  {
    prop: 'occurDate',
    label: '发生日期',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  },
  {
    prop: 'systemName',
    label: '系统',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'causeFault',
    label: '故障原因',
    width: 120
  },
  {
    prop: 'natureFault',
    label: '故障性质',
    width: 120
  },
  {
    prop: 'methodDetermination',
    label: '判明方法',
    width: 120
  },
  {
    prop: 'methodRectification',
    label: '排除方法',
    width: 120
  },
  {
    prop: 'remarks',
    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: 'aircraftType',
      rules: [{ required: true, message: '机型不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入机型'
      }
    },
    {
      label: '单位',
      prop: 'unitName',
      compOptions: {
        placeholder: '请输入单位'
      }
    },
    {
      label: '故障件名称',
      prop: 'itemName',
      compOptions: {
        placeholder: '请输入故障件名称'
      }
    },
    {
      label: '专业',
      prop: 'specialty',
      compOptions: {
        placeholder: '请输入专业'
      }
    },
    {
      label: '故障现象',
      prop: 'faultPhenomenon',
      compOptions: {
        placeholder: '请输入故障现象'
      }
    },
    {
      label: '发动机型别',
      prop: 'engineType',
      compOptions: {
        placeholder: '请输入发动机型别'
      }
    },
    {
      label: '故障发生地点',
      prop: 'locationFault',
      compOptions: {
        placeholder: '请输入故障发生地点'
      }
    },
    {
      label: '故障件型别',
      prop: 'itemType',
      compOptions: {
        placeholder: '请输入故障件型别'
      }
    },
    {
      label: '发生日期',
      prop: 'occurDate',
      compOptions: {
        elTagName: 'date-picker',
        type: 'date',
        placeholder: '请选择发生日期'
      }
    },
    {
      label: '系统',
      prop: 'systemName',
      compOptions: {
        placeholder: '请输入系统'
      }
    },
    {
      label: '故障原因',
      prop: 'causeFault',
      compOptions: {
        placeholder: '请输入故障原因'
      }
    },
    {
      label: '故障性质',
      prop: 'natureFault',
      compOptions: {
        placeholder: '请输入故障性质'
      }
    },
    {
      label: '判明方法',
      prop: 'methodDetermination',
      compOptions: {
        placeholder: '请输入判明方法'
      }
    },
    {
      label: '排除方法',
      prop: 'methodRectification',
      compOptions: {
        placeholder: '请输入排除方法'
      }
    },
    {
      label: '备注',
      prop: 'remarks',
      compOptions: {
        placeholder: '请输入备注'
      }
    }
  ]
}
</script>
