<template>
  <div class="index">
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="600px" :before-close="closeDialog">
      <el-form ref="form" :model="activeRow" label-width="80px">
        <el-form-item label="航材名称">
          <el-input v-model="activeRow.avmatCatalogName" disabled>
            <el-button type="primary" @click="openFittingsModal" slot="append">选择</el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="航材编码">
          <el-input v-model="activeRow.avmatCatalogCode" disabled></el-input>
        </el-form-item>
        <el-form-item label="所属分类">
          <el-input v-model="activeRow.avmatCategoriesCodeName" disabled></el-input>
        </el-form-item>
        <el-form-item label="MTBF">
          <el-input v-model="activeRow.mtbf"></el-input>
        </el-form-item>
        <el-form-item label="寿命单位">
          <el-select v-model="activeRow.LifeUnit" placeholder="">
            <el-option v-for="item in monadOptions" :key="item.dictValue" :label="item.dictLabel" :value="item.dictValue"> </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="handleRegularities">确 定</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'
import { deepClone } from '@/utils'
export default {
  name: 'Index',
  props: ['monadOptions'],
  data() {
    // 这里存放数据
    return {
      dialogVisible: false,
      activeRow: {
        avmatCatalogName: '',
        avmatCatalogCode: '',
        avmatCategoriesCodeName: '',
        mtbf: '',
        LifeUnit: ''
      },
      dialogTitle: '新增',
      dialogType: '',
      fittingsVisible: false,
      currentNodeKey: '',
      menuTreeData: [],
      treeObj: {
        title: '所属分类',
        activityheight: '500px',
        searchIcon: false,
        configure: defaultProps
      },
      tableData: [],
      columns: columns(this),
      options,
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 20,
        searchValue: ''
      },
      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 })
    },

    openDialog(data) {
      const { title, type } = data
      this.dialogTitle = title
      this.dialogType = type
      this.dialogVisible = true
    },

    closeDialog() {
      this.dialogVisible = false
      this.activeRow = {
        avmatCatalogName: '',
        avmatCatalogCode: '',
        avmatCategoriesCodeName: '',
        mtbf: '',
        LifeUnit: ''
      }
    },

    handUpdateRow(params) {
      const { consumeRuleId, avmatCatalogName, avmatCatalogCode, avmatCategoriesCode, avmatCategoriesName, avmatCatalogId, lifeUnit, mtbf } = params
      this.activeRow.avmatCatalogName = avmatCatalogName
      this.activeRow.avmatCatalogCode = avmatCatalogCode
      this.activeRow.avmatCategoriesCode = avmatCategoriesCode
      this.activeRow.avmatCategoriesCodeName = avmatCategoriesName
      this.activeRow.avmatCatalogId = avmatCatalogId
      this.activeRow.consumeRuleId = consumeRuleId
      this.activeRow.LifeUnit = String(lifeUnit)
      this.activeRow.mtbf = mtbf
    },

    handleRegularities() {
      this.$emit('handleRegularities', { type: this.dialogType, params: deepClone(this.activeRow) })
    },

    openFittingsModal() {
      this.fittingsVisible = true
    },

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

    treeNodeClick(data) {
      this.currentNodeKey = data.avmatCategoriesCode
      this.avmatCategoriesId = data.avmatCategoriesId || ''
      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
      this.fittingsVisible = false
    }
  }
}
</script>

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