<template>
  <div class="view-carrierListExecutionRegistrationCompile-missionPlane">
    <div class="view-dataSpecies-right-registering">
      <div class="view-dataType-table">
        <div class="common-box">
          <div class="common-box-button">
            <el-button type="primary" @click="importOperation">导入</el-button>
            <el-button type="primary" :disabled="tableData.length == 0" @click="allExport">导出</el-button>
          </div>
        </div>
        <MissionDetail />

        <LTable ref="table" :columns="columns" :dataSource="tableData" :options="options" :fetch="fetchTableData" :pagination="pagination"></LTable>

        <!-- 导入 -->
        <ImportDialog ref="ImportDialog" @submitCallBack="ImportDialogCallBack" />
      </div>
    </div>
  </div>
</template>

<script>
import { getTaskExecondRegisterItem, register, exportExcel } from '@/api/carrierStandard/carrierListExecutionRegistration'
import { columns, options } from './index'
import { isNums } from '@/utils/verifyType'
import ImportDialog from '../importDialog/index.vue'

export default {
  name: 'registering',
  components: { ImportDialog },
  data() {
    // 这里存放数据
    return {
      currentNodeKey: '',
      pagination: {
        total: 0,
        pageIndex: 1,
        pageSize: 10,
        searchValue: ''
      },
      columns: columns(this),
      options,
      tableData: [],
      renderKey: Math.random(),
      activeRow: {}
    }
  },
  created() {},
  methods: {
    async fetchTableData() {
      const { pageIndex, pageSize } = this.pagination
      const { taskId } = this.$route.params
      const {
        data: { list, totalCount }
      } = await getTaskExecondRegisterItem({
        aircaftCatalogId: '',
        taskId,
        pageIndex: pageIndex,
        pageSize: pageSize
      })
      this.tableData = list
      this.pagination.total = totalCount
    },
    async updateRow(row, scope) {
      const { taskId } = this.$route.params
      const { taskExecondRegisterId, aircaftCatalogId, aircraftActualNum, lackMaterStopFlyNum, aircraftIntactNum } = row
      console.log(scope.column.property)
      const res = isNums([row[scope.column.property]])
      if (!res.every((e) => e)) {
        this.fetchTableData()
        this.$message({
          type: 'warning',
          message: '只能输入数字'
        })
        return
      }
      // 飞机完好架日 缺航材停飞架日 飞机实有架日只能为正整数
      if (['aircraftIntactNum', 'lackMaterStopFlyNum', 'aircraftActualNum'].includes(scope.column.property)) {
        const value = row[scope.column.property]
        if (!Number.isInteger(Number(value)) || Number(value) < 0) {
          this.fetchTableData()
          this.$message({
            type: 'warning',
            message: '只能输入正整数'
          })
          return
        }
      }
      try {
        const { success } = await register({
          taskId,
          taskExecondRegisterId,
          aircaftCatalogId,
          aircraftActualNum: getFuncValue(row, scope, 'aircraftActualNum'),
          lackMaterStopFlyNum: getFuncValue(row, scope, 'lackMaterStopFlyNum'),
          aircraftIntactNum: getFuncValue(row, scope, 'aircraftIntactNum'),
          flyDuration: getFuncValue(row, scope, 'flyDuration')
          // [scope.column.property]: row[scope.column.property]
        })
        if (success) {
          this.$message({
            type: 'success',
            message: '编辑成功!'
          })
        }
      } catch (error) {}
      this.fetchTableData()
    },
    edit(row) {
      this.activeRow = row
      this.dialogVisible = true
    },
    treeNodeClick(data) {
      this.$refs.table.clearSelection()
      // 点击树节点传值
      this.currentNodeKey = data.id
      this.currentNodeName = data.aircraftTypeName
      this.fetchTableData()
    },
    cellBlue(index, row, key) {
      // row[key] = row[key]
      // this.tableData[index] = row
    },
    editCell(index, row, key) {
      // let value = row[key]
      // row[key] = {}
      // vue.set(row[key], key, row[key])
      // this.tableData[index] = row
    },
    //导入
    importOperation() {
      const { taskId } = this.$route.params
      let params = {
        taskId
      }
      this.$refs.ImportDialog.triggerDialog(params)
    },
    //导入回写
    ImportDialogCallBack() {
      this.fetchTableData()
    },
    //导出全部
    async allExport() {
      const { taskId } = this.$route.params
      let params = {
        aircaftCatalogId: '',
        taskId
      }
      await exportExcel(params)
    }
  }
}

function getFuncValue(row, scope, value) {
  if (value == scope.column.property) {
    return row[scope.column.property]
  } else {
    return row[value]
  }
}
</script>

<style lang="scss">
@import './index.scss';
</style>
