<template>
  <div class="main-box">
    <TreeFilter title="机型机号" :is-all="false" :data="treeFilterData" :default-value="'0'" @change="changeTreeFilter" />
    <div class="table-box">
      <ProTable ref="proTable" :columns="columns" row-key="id" :request-api="listDataImportApi" :init-param="initParam">
        <!-- 表格 header 按钮 -->
        <template #tableHeader="scope">
          <el-button type="primary" v-auth="['als:dataImport:add']" @click="openDialog(1, '数据导入信息新增')"> 导入 </el-button>
          <el-button
            type="danger"
            v-auth="['als:dataImport: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:dataImport:query']" @click="openDialog(3, '数据导入信息查看', scope.row)">
            查看
          </el-button>
          <el-button type="primary" link icon="EditPen" v-auth="['als:dataImport:edit']" @click="openDialog(2, '数据导入信息编辑', scope.row)">
            编辑
          </el-button>
          <el-button type="primary" link icon="Delete" v-auth="['als:dataImport:remove']" @click="deleteDataImport(scope.row)"> 删除 </el-button>
        </template>
      </ProTable>
      <FormDialog ref="formDialogRef" :items-options="itemsOptions" :model="model" />
    </div>
  </div>
</template>

<script setup lang="tsx" name="DataImport">
import { useHandleData } from '@/hooks/useHandleData'
import FormDialog from '@/components/FormDialog/index.vue'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import { listDataImportApi, delDataImportApi, addDataImportApi, updateDataImportApi, getDataImportApi } from '@/api/modules/als/dataImport'
import { treeSelectApi } from '@/api/modules/als/aircraft'
import { selectSortieApi } from '@/api/modules/als/sortie'
const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { sys_common_status, common_type } = toRefs<any>(proxy?.useDict('sys_common_status', 'common_type'))
onMounted(() => {
  getTreeFilter()
})
// ProTable 实例
const proTable = ref<ProTableInstance>()
// 表单model
const model = ref({})
let initParam = reactive({ aircraftId: '' })
let treeFilterData = ref<any>([])
let treeOptions = ref<any>([])
const getTreeFilter = async () => {
  const { data } = await treeSelectApi()
  treeOptions.value = data
  treeFilterData.value.push({
    id: '0',
    label: '所有机型',
    parentId: '',
    children: data
  })
}

// 树形筛选切换
const changeTreeFilter = (val: string) => {
  proTable.value!.pageable.pageNum = 1
  initParam.aircraftId = val === '0' ? '' : val
}

// 删除数据导入信息信息
const deleteDataImport = async (params: any) => {
  await useHandleData(delDataImportApi, params.id, '删除【' + params.id + '】数据导入信息')
  proTable.value?.getTableList()
}

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

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 getDataImportApi(row?.id || null)
  }
  model.value = type == 1 ? {} : res.data
  // 重置表单
  setItemsOptions()
  const params = {
    title,
    width: 580,
    isEdit: type !== 3,
    api: type == 1 ? addDataImportApi : updateDataImportApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  {
    prop: 'ossId',
    label: '文件编号',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'source',
    label: '数据源',
    tag: true,
    enum: common_type,
    search: {
      el: 'select'
    }
  },
  {
    prop: 'sortieNo',
    label: '架次号',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'aircraftNo',
    label: '机号',
    width: 120
  },
  {
    prop: 'flightDate',
    label: '飞行日期',
    search: {
      el: 'date-picker',
      props: { type: 'date', valueFormat: 'YYYY-MM-DD' }
    },
    width: 120
  },
  {
    prop: 'status',
    label: '状态',
    tag: true,
    enum: sys_common_status,
    search: {
      el: 'tree-select'
    }
  },
  {
    prop: 'createByName',
    label: '创建人',
    width: 120
  },
  {
    prop: 'createTime',
    label: '创建时间',
    width: 120
  },
  {
    prop: 'updateByName',
    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: 'source',
      rules: [{ required: true, message: '数据源不能为空', trigger: 'change' }],
      compOptions: {
        elTagName: 'select',
        enum: common_type.value,
        placeholder: '请选择数据源'
      }
    },
    {
      label: '机号',
      prop: 'aircraftId',
      rules: [{ required: true, message: '机号不能为空', trigger: 'change' }],
      compOptions: {
        elTagName: 'tree-select',
        enum: treeOptions.value,
        valueKey: 'id',
        placeholder: '请选择机号'
      }
    },
    {
      label: '架次号',
      prop: 'sortieNo',
      rules: [{ required: true, message: '架次号不能为空', trigger: 'blur' }],
      compOptions: {
        elTagName: 'select',
        enum: selectSortieApi,
        valueKey: 'id',
        labelKey: 'sortieNo',
        placeholder: '请输入架次号'
      }
    },
    {
      label: '数据导入',
      prop: 'ossId',
      rules: [{ required: true, message: '机号不能为空', trigger: 'blur' }],
      compOptions: {
        elTagName: 'file-upload-s3',
        placeholder: '请输入机号'
      }
    }
  ]
}
</script>
