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

<script setup lang="tsx" name="Sortie">
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 {
  listSortieApi,
  delSortieApi,
  addSortieApi,
  updateSortieApi,
  importTemplateApi,
  importSortieDataApi,
  exportSortieApi,
  getSortieApi
} from '@/api/modules/als/sortie'

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

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

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

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

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  {
    prop: 'sortieNo',
    label: '架次号',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'aircraftNo',
    label: '机号',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'flightDate',
    label: '飞行日期',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  },
  {
    prop: 'startTime',
    label: '开始时间',
    width: 120
  },
  {
    prop: 'endTime',
    label: '结束时间',
    width: 120
  },
  {
    prop: 'duration',
    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: 'sortieNo',
      rules: [{ required: true, message: '架次号不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入架次号'
      }
    },
    {
      label: '机号',
      prop: 'aircraftNo',
      rules: [{ required: true, message: '机号不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入机号'
      }
    },
    {
      label: '飞行日期',
      prop: 'flightDate',
      rules: [{ required: true, message: '飞行日期不能为空', trigger: 'change' }],
      compOptions: {
        elTagName: 'date-picker',
        type: 'date',
        placeholder: '请选择飞行日期'
      }
    },
    {
      label: '开始时间',
      prop: 'startTime',
      rules: [{ required: true, message: '开始时间不能为空', trigger: 'change' }],
      compOptions: {
        elTagName: 'date-picker',
        type: 'date',
        placeholder: '请选择开始时间'
      }
    },
    {
      label: '结束时间',
      prop: 'endTime',
      rules: [{ required: true, message: '结束时间不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入结束时间'
      }
    },
    {
      label: '飞行时长',
      prop: 'duration',
      rules: [{ required: true, message: '飞行时长不能为空', trigger: 'blur' }],
      compOptions: {
        placeholder: '请输入飞行时长'
      }
    }
  ]
}
</script>
