<template>
  <div>
    <el-dialog class="structureEquipping-modal" title="航材" :visible="structureEquippingVisible" width="600px" :before-close="closeModal">
      <div class="structureEquipping-list">
        <label>航材名称</label>
        <el-input v-model="activeRow.avmatCatalogName" placeholder="请选择装备" disabled></el-input>
        <el-button type="primary" @click="openFittingsModal">选择装备</el-button>
      </div>
      <div class="structureEquipping-list">
        <label>航材编码</label>
        <el-input v-model="activeRow.avmatCatalogCode" placeholder="请输入航材编码" disabled></el-input>
      </div>
      <div class="structureEquipping-list">
        <label>所属机型</label>
        <el-input v-model="activeRow.aircaftModelCodeName" placeholder="请输入所属机型" disabled></el-input>
      </div>
      <div class="structureEquipping-list">
        <label>装机数量</label>
        <el-input v-model="activeRow.installedCount" placeholder="请输入装机数量"></el-input>
      </div>
      <div class="structureEquipping-list">
        <label>装机部位</label>
        <el-input v-model="activeRow.installedPosition" placeholder="请输入装机部位"></el-input>
      </div>
      <div class="structureEquipping-list">
        <label>航材分类</label>
        <el-input v-model="activeRow.avmatCategoriesCodeName" placeholder="请输入航材分类" disabled></el-input>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeModal">取 消</el-button>
        <el-button type="primary" @click="handleStructureEquipping">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog class="fittings-modal" title="选择航材" :visible="fittingsVisible" width="1200px" :before-close="closefittingsVisible">
      <div class="fittings-modal-box">
        <div class="fittings-modal-box-left">
          <MenuTree nodeKey="avmatCategoriesCode" :currentNodeKey="currentNodeKey" :treedata="menuTreeData" @TreeNodeclick="treeNodeClick" v-bind="treeObj"> </MenuTree>
        </div>
        <div class="fittings-modal-box-right">
          <LTable ref="table" :defaultFetch="false" :columns="columns" :dataSource="tableData" :options="options" :fetch="fetchTableData" :pagination="tableRequset"></LTable>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAvmatCategoriesTree, getAvmatCatalog } from '@/api/basicData/dataEnvironment'
import { defaultProps, columns, options } from './index'
export default {
  name: 'StructureEquipping',
  props: ['treeNode'],
  data() {
    // 这里存放数据
    return {
      structureEquippingVisible: false,
      fittingsVisible: false,
      currentNodeKey: '',
      menuTreeData: [],
      treeObj: {
        title: '所属分类',
        activityheight: '500px',
        searchIcon: false,
        configure: defaultProps
      },
      tableData: [],
      columns: columns(this),
      options,
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 10,
        searchValue: ''
      },
      activeRow: {
        avmatCatalogName: '',
        avmatCatalogCode: '',
        avmatCategoriesCode: '',
        avmatCategoriesCodeName: '',
        installedCount: '',
        installedPosition: '',
        aircaftModelCodeName: '',
        aircaftModelCode: ''
      },
      avmatCategoriesId: ''
    }
  },
  methods: {
    async fetchTableData(params) {
      if (params == undefined) {
        params = { pageIndex: this.tableRequset.pageIndex, pageSize: this.tableRequset.pageSize, avmatCategoriesCode: this.currentNodeKey, avmatCategoriesId: this.avmatCategoriesId }
      }
      const {
        data: { list, totalCount }
      } = await getAvmatCatalog({ ...params, avmatType: '0' })
      this.tableRequset.total = totalCount
      this.tableData = list
    },

    async initData() {
      const { data } = await getAvmatCategoriesTree()
      this.currentNodeKey = ''
      this.menuTreeData = [{ avmatCategoriesCode: '', avmatCategoriesName: '全部', children: data }]
      this.fetchTableData({ pageIndex: 1, pageSize: 20, avmatCategoriesCode: this.currentNodeKey, avmatCategoriesId: this.avmatCategoriesId })
    },

    closefittingsVisible() {
      this.fittingsVisible = false
      this.currentNodeKey = ''
    },

    handleFittingsVisible() {
      this.fittingsVisible = false
    },

    handleStructureEquipping() {
      delete this.activeRow.avmatCategoriesCodeName
      this.$emit('handleStructureEquipping', { type: this.dialogType, ...this.activeRow })
    },

    openModal(params) {
      const { title, type } = params
      this.dialogTitle = title
      this.dialogType = type
      this.structureEquippingVisible = true
      //
      this.activeRow.aircaftModelCodeName = this.treeNode.label
      this.activeRow.aircaftModelCode = this.treeNode.value
    },

    setRowData(row) {
      const { avmatCatalogName, avmatCatalogCode, avmatCategoriesCode, avmatCategoriesName } = row
      this.activeRow.avmatCatalogName = avmatCatalogName
      this.activeRow.avmatCatalogCode = avmatCatalogCode
      this.activeRow.avmatCategoriesCode = avmatCategoriesCode
      this.activeRow.avmatCategoriesCodeName = avmatCategoriesName
    },

    closeModal() {
      this.structureEquippingVisible = false
      this.activeRow = {
        avmatCatalogName: '',
        avmatCatalogCode: '',
        avmatCategoriesCode: '',
        avmatCategoriesCodeName: '',
        installedCount: '',
        installedPosition: '',
        aircaftModelCodeName: '',
        aircaftModelCode: '',
        avmatCatalogId: ''
      }
      this.closefittingsVisible()
    },

    openFittingsModal() {
      this.initData()
      this.fittingsVisible = true
    },

    treeNodeClick(data) {
      this.avmatCategoriesId = data.avmatCategoriesId || ''
      this.currentNodeKey = data.avmatCategoriesCode
      this.$refs.table.clearSelection()
      this.fetchTableData({ pageIndex: 1, pageSize: 20, avmatCategoriesCode: this.currentNodeKey, avmatCategoriesId: this.avmatCategoriesId })
    },

    select(row) {
      const { avmatCatalogName, avmatCatalogCode, avmatCategoriesCode, avmatCategoriesName, avmatCatalogId } = row
      this.activeRow.avmatCatalogName = avmatCatalogName
      this.activeRow.avmatCatalogCode = avmatCatalogCode
      this.activeRow.avmatCategoriesCode = avmatCategoriesCode
      this.activeRow.avmatCategoriesCodeName = avmatCategoriesName
      this.activeRow.avmatCatalogId = avmatCatalogId // 航材ID
      this.fittingsVisible = false
    }
  }
}
</script>

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