<template>
  <div class="view-dataSpecies">
    <div class="view-dataSpecies-left">
      <MenuTree node-key="aircaftTypeCode" :defaultcheCkedKeys="defaultcheCkedKeys" :currentNodeKey="currentNodeKey" :treedata="menuTreeData" @TreeNodeclick="treeNodeClick" v-bind="treeObj"> </MenuTree>
    </div>
    <div class="view-dataSpecies-right">
      <!-- 通用代码 左侧按钮 右侧输入框 下拉框 -->
      <div class="view-common-header">
        <div class="view-common-header-left">
          <el-button type="success" @click="openDialog()" :disabled="currentNodeKey === ''">新增</el-button>
          <el-button type="warning" @click="remove(removeArr)" :disabled="removeArr.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-common-header-right">
          <!-- width-137 在style/excel.js 定义宽度 -->
          <el-input @keyup.enter.native="fetchTableData('search')" placeholder="请输入机型名称/编号" v-model="searchValue" class="input-select">
            <el-button @click="fetchTableData('search')" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </div>
      </div>
      <div class="view-dataType-table">
        <LTable ref="table" @select-all="selectAll" @selection-change="selection" :defaultFetch="false" :columns="columns" :dataSource="tableData" :options="options" :fetch="fetchTableData" :pagination="tableRequset"></LTable>
      </div>
      <CrudDialog v-bind="modalConfig" ref="NewDialogRef" @crudDialogSubmit="crudDialogSubmit"></CrudDialog>
      <!-- 导入 -->
      <ImportDialog ref="ImportDialog" @submitCallBack="ImportDialogCallBack" />
    </div>
  </div>
</template>

<script>
import { defaultProps, columns, options, modalConfig } from './index'
import { getAircaftTypeTree, getAircaftModel, addAircaftModel, updateAircaftModel, removeAircaftModel, downLoadFile, exportExcel } from '@/api/basicData/dataSpecies'
import ImportDialog from './importDialog/index.vue'
export default {
  name: 'dataSpecies',
  components: { ImportDialog },
  data() {
    // 这里存放数据
    return {
      dialogTitle: '新增',
      dialogVisible: false,
      currentNodeKey: '',
      menuTreeData: [],
      currentNodeName: '',
      aircaftTypeId: '',
      defaultcheCkedKeys: [''],
      treeObj: {
        title: '所属机种',
        activityheight: '275px',
        searchIcon: false,
        configure: defaultProps
      },
      typeTree: {
        children: 'children',
        label: 'aircraftTypeName'
      },
      searchValue: '',
      columns: columns(this),
      modalConfig: modalConfig(this),
      options,
      tableData: [],
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 10
      },
      removeArr: []
    }
  },
  mounted() {
    this.getData()
  },
  methods: {
    async getData() {
      let params = {
        status: 1
      }
      const { data } = await getAircaftTypeTree(params)
      let obj = {
        aircaftTypeId: '',
        aircaftTypeCode: '',
        aircaftTypeName: '全部',
        children: data
      }
      this.menuTreeData = [obj]
      this.currentNodeKey = ''
      this.fetchTableData()
    },

    treeNodeClick(data) {
      this.$refs.table.clearSelection()
      // 点击树节点传值
      this.currentNodeKey = data.aircaftTypeCode
      this.currentNodeName = data.aircaftTypeName
      this.aircaftTypeId = data.aircaftTypeId
      this.searchValue = ''
      this.tableRequset.pageIndex = 1
      this.fetchTableData()
    },

    // 全选
    selectAll(data) {
      this.removeArr = []
      if (data.length > 0) {
        data.forEach((item) => {
          this.removeArr.push(item.aircaftModelId)
        })
      } else {
        this.removeArr = []
      }
    },
    // 单选
    selection(data) {
      this.removeArr = []
      data.forEach((item) => {
        this.removeArr.push(item.aircaftModelId)
      })
    },
    async fetchTableData(search) {
      if (search) {
        this.currentNodeKey = ''
        this.currentNodeName = '全部'
        this.tableRequset.pageIndex = 1
      }
      let params = {
        aircaftTypeCode: this.currentNodeKey,
        pageSize: this.tableRequset.pageSize,
        pageIndex: this.tableRequset.pageIndex,
        queryParam: this.searchValue,
        aircaftTypeId: this.aircaftTypeId
      }
      const { data } = await getAircaftModel(params)
      this.tableData = data.list
      this.tableRequset.total = data.totalCount
    },
    toggle(type, row) {
      this.$confirm(`是否${type}该机种`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let params = {
            ...row,
            status: type === '启用' ? 1 : 0
          }
          await updateAircaftModel(params)
          this.fetchTableData()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
        .catch(() => {})
    },
    remove(aircaftModelIdList) {
      this.$confirm('是否删除该机种', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await removeAircaftModel(aircaftModelIdList)
          this.fetchTableData()
          this.$refs.table.clearSelection()
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
        .catch(() => {})
    },
    openDialog() {
      let config = {
        type: 'add',
        dataShow: {
          aircaftTypeName: this.currentNodeName,
          aircaftTypeCode: this.currentNodeKey
        }, // 回填的值（字段对应上）
        title: '新增机型'
      }
      this.$refs.NewDialogRef.open(config)
    },
    handUpdate(row) {
      let config = {
        type: 'upDate',
        dataShow: row, // 回填的值（字段对应上）
        title: '编辑机型'
      }
      this.$refs.NewDialogRef.open(config)
    },

    async crudDialogSubmit(data) {
      const { dialogData, type } = data
      if (type == 'upDate') {
        const { code } = await updateAircaftModel({ aircaftTypeId: this.aircaftTypeId, ...dialogData })
        if (code == 200) {
          this.fetchTableData()
          this.$message({
            type: 'success',
            message: '编辑成功!'
          })
        }
      }
      if (type == 'add') {
        const { code } = await addAircaftModel({ aircaftTypeId: this.aircaftTypeId, ...dialogData })
        if (code == 200) {
          this.fetchTableData()
          this.$message({
            type: 'success',
            message: '新增成功!'
          })
        }
      }
    },
    //更多按钮
    handleExport(type) {
      this[type]()
    },
    //导入
    importOperation() {
      this.$refs.ImportDialog.triggerDialog()
    },
    //导入回写
    ImportDialogCallBack() {
      this.fetchTableData()
    },

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

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