<template>
  <div class="view-dataAircraftCataloging">
    <div class="view-dataSpecies-left">
      <MenuTree :currentNodeKey="currentNodeKey" nodeKey="id" :treedata="menuTreeData" @TreeNodeclick="treeNodeClick" v-bind="treeObj"> </MenuTree>
    </div>
    <div class="view-dataSpecies-right">
      <div class="view-dataType-title">
        <div class="view-dataType-title-btn">
          <el-button type="success" @click="openDialog()" :disabled="currentNode && currentNode.type == 0">新增</el-button>
          <el-button type="warning" @click="remove(tableCheckItems)" :disabled="tableCheckItems.length == 0">删除</el-button>
          <el-dropdown split-button type="primary" style="margin-left: 10px" trigger="click" @command="handleExport">
            更多
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="importOperation">导入</el-dropdown-item>
              <el-dropdown-item command="allExport" :disabled="tableData.length == 0">导出</el-dropdown-item>
              <el-dropdown-item command="downloadTemplate">下载数据模板</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div class="view-dataType-title-search">
          <el-input placeholder="请输入飞机编号/厂家/出厂编号" v-model="keyWord" class="input1">
            <el-button slot="append" icon="el-icon-search" @click="searchClick"></el-button>
          </el-input>
        </div>
      </div>
      <div class="view-dataType-table">
        <LTable ref="table" @selection-change="selection" :defaultFetch="false" :fetch="fetch" :columns="columns" :dataSource="tableData" :options="options" :pagination="tableRequset"></LTable>
      </div>
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px" :before-close="handleClose">
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="飞机编号">
            <el-input v-model="form.aircaftCatalogCode"></el-input>
          </el-form-item>
          <el-form-item label="机械师">
            <el-input v-model="form.mechanician"></el-input>
          </el-form-item>
          <el-form-item label="所属机型">
            <el-input v-model="form.aircaftModelName" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="所属部队">
            <el-input v-model="form.army"></el-input>
          </el-form-item>
          <el-form-item label="所属战区">
            <el-input v-model="form.warZone"></el-input>
          </el-form-item>
          <el-form-item label="驻地">
            <el-input v-model="form.station"></el-input>
          </el-form-item>
          <el-form-item label="装列时间">
            <el-date-picker v-model="form.assemblyTime" value-format="yyyy-MM-dd" type="date" placeholder="选择日期"> </el-date-picker>
          </el-form-item>
          <el-form-item label="生产厂家">
            <el-input v-model="form.manufacturer"></el-input>
          </el-form-item>
          <el-form-item label="生厂编号">
            <el-input v-model="form.factoryNumber"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio label="0">服役</el-radio>
              <el-radio label="1">退役</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 导入 -->
      <ImportDialog ref="ImportDialog" @submitCallBack="ImportDialogCallBack" />
    </div>
  </div>
</template>

<script>
import { getAircaftCatalog, getAircaftTypeAndModelTree, addAircaftCatalog, updateAircaftCatalog, removeAircaftCatalog, downLoadFile, exportExcel } from '@/api/basicData/dataAircraftCataloging'
import { defaultProps, columns, options } from './index'
import { deepClone, debounce } from '@/utils/index'
import ImportDialog from './importDialog/index.vue'
export default {
  name: 'dataAircraftCataloging',
  components: { ImportDialog },
  data() {
    // 这里存放数据
    return {
      dialogTitle: '新增',
      dialogVisible: false,
      keyWord: '',
      aircaftModelIdList: [],
      currentNodeKey: null,
      currentNode: null,
      menuTreeData: [],
      treeObj: {
        title: '所属机种',
        activityheight: '275px',
        searchIcon: false,
        configure: defaultProps
      },
      typeTree: {
        children: 'children',
        label: 'label'
      },
      searchValue: '',
      columns: columns(this),
      options,
      tableCheckItems: [],
      tableData: [],
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 10,
        searchValue: ''
      },
      form: {
        aircaftCatalogCode: '',
        mechanician: '',
        aircaftModelId: '',
        aircaftModelName: '',
        army: '',
        warZone: '',
        station: '',
        assemblyTime: '',
        manufacturer: '',
        factoryNumber: '',
        status: ''
      },
      debounceFn: debounce(this.fetch, 500)
    }
  },
  watch: {
    keyWord() {
      this.tableRequset.pageIndex = 1
      this.debounceFn()
    }
  },
  mounted() {
    this.getAircaftTypeAndModelTreeAPI()
  },
  methods: {
    async getAircaftTypeAndModelTreeAPI(params) {
      const { data } = await getAircaftTypeAndModelTree(params)
      this.menuTreeData = data
      if (data.length) {
        this.currentNodeKey = data[0].value
        this.currentNode = data[0]
        this.aircaftModelIdList = this.getTreeLeafData(data[0]?.children).map((e) => e.id)
        this.getAircaftCatalogAPI({ aircaftModelIdList: this.aircaftModelIdList })
      }
    },

    async removeAircaftCatalogAPI(params) {
      try {
        const { success } = await removeAircaftCatalog(params)
        if (success) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          await this.getAircaftCatalogAPI({ aircaftModelIdList: this.aircaftModelIdList })
          this.handleClose()
        }
      } catch (error) {}
    },

    getTreeLeafData(list) {
      const newArr = []
      function getLeaf(data, arr) {
        data.forEach((e) => {
          if (e.type === 1) {
            arr.push(e)
          }
          if (e.children.length) {
            getLeaf(e.children, arr)
          }
        })
      }
      getLeaf(list, newArr)
      return newArr
    },

    async getAircaftCatalogAPI(params) {
      if (this.$refs.table) this.$refs.table.clearSelection()
      const { keyWord } = this
      const { pageSize, pageIndex } = this.tableRequset
      const {
        data: { list, totalCount }
      } = await getAircaftCatalog({ pageSize, pageIndex, keyWord, ...params })
      this.tableData = list
      this.tableRequset.total = totalCount
    },

    fetch() {
      this.getAircaftCatalogAPI({ aircaftModelIdList: this.aircaftModelIdList })
    },

    searchClick() {
      this.getAircaftCatalogAPI({ aircaftModelIdList: this.aircaftModelIdList })
    },

    async addAircaftCatalogAPI() {
      try {
        delete this.form.aircaftModelName
        const { success } = await addAircaftCatalog({ ...this.form })
        if (success) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.getAircaftCatalogAPI({ aircaftModelIdList: this.aircaftModelIdList })
        }
      } catch (error) {}
    },

    async updateAircaftCatalogAPI() {
      try {
        const { aircaftCatalogId, aircaftCatalogCode, mechanician, aircaftModelId, army, warZone, station, assemblyTime, manufacturer, factoryNumber, status } = this.form
        const { success } = await updateAircaftCatalog({ aircaftCatalogId, aircaftCatalogCode, mechanician, aircaftModelId, army, warZone, station, assemblyTime, manufacturer, factoryNumber, status })
        if (success) {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
          this.getAircaftCatalogAPI({ aircaftModelIdList: this.aircaftModelIdList })
        }
      } catch (error) {}
    },

    treeNodeClick(data) {
      this.$refs.table.clearSelection()
      this.currentNodeKey = data.id
      this.currentNode = data
      this.aircaftModelIdList = this.getTreeLeafData(data.children.length ? data.children : [data]).map((e) => e.id)
      this.getAircaftCatalogAPI({ aircaftModelIdList: this.aircaftModelIdList })
    },

    openDialog() {
      this.dialogTitle = '新增'
      this.dialogVisible = true
      this.form.aircaftModelName = this.currentNode.label
      this.form.aircaftModelId = this.currentNode.id
    },

    handleClose() {
      this.dialogVisible = false
      this.form = {
        aircaftCatalogCode: '',
        mechanician: '',
        aircaftModelId: '',
        army: '',
        warZone: '',
        station: '',
        assemblyTime: '',
        manufacturer: '',
        factoryNumber: '',
        status: ''
      }
    },

    handUpdate(row) {
      this.dialogTitle = '编辑'
      row.status = row.status.toString()
      this.form = deepClone(row)
      this.dialogVisible = true
    },

    submit() {
      switch (this.dialogTitle) {
        case '编辑':
          this.updateAircaftCatalogAPI()
          this.handleClose()
          break
        case '新增':
          this.addAircaftCatalogAPI()
          this.handleClose()
          break
      }
    },

    selection(val) {
      this.tableCheckItems = val
    },

    remove(row) {
      this.$confirm('是否删除该飞机', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.removeAircaftCatalogAPI(row.map((e) => e.aircaftCatalogId))
        })
        .catch(() => {})
    }, //更多按钮
    handleExport(type) {
      this[type]()
    },
    //导入
    importOperation() {
      this.$refs.ImportDialog.triggerDialog()
    },
    //导入回写
    ImportDialogCallBack() {
      this.getAircaftCatalogAPI({ aircaftModelIdList: this.aircaftModelIdList })
    },

    //导出全部
    async allExport() {
      const params = {
        queryParam: this.searchValue,
        aircaftModelIdList: this.aircaftModelIdList
      }
      await exportExcel(params)
    },
    //下载模板
    async downloadTemplate() {
      await downLoadFile()
    }
  }
}
</script>

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