<template>
  <div class="index">
    <el-dialog :title="dialogTitle" :visible.sync="regularitiesChooseVisible" width="600px" :before-close="closeModal">
      <el-form ref="form" :model="activeRow" :rules="rules" label-width="80px">
        <el-form-item label="航材名称">
          <el-input v-model="activeRow.avmatCatalogName" disabled>
            <el-button type="primary" @click="openFittingsModal" slot="append" :disabled="dialogType !== 'add'">选择</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.avmatCategoriesName" disabled></el-input>
        </el-form-item>
        <el-form-item label="使用环境">
          <el-input v-model="activeRow.envFactorName" disabled></el-input>
        </el-form-item>
        <el-form-item label="因子" prop="factor">
          <el-input v-model="activeRow.factor"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeModal">取 消</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'
export default {
  name: 'RegularitiesChoose',
  props: {
    regularitiesActiveRow: {}
  },
  data() {
    const validatePass = (rule, value, callback) => {
      const pattern = /^(?!0\d)(\d+|\d+\.\d{1,2})$/
      if (value === '') {
        callback(new Error('请输入因子'))
      } else if (!pattern.test(String(value))) {
        callback(new Error('请输入正整数或保留两位的小数!'))
      } else {
        callback()
      }
    }
    // 这里存放数据
    return {
      regularitiesChooseVisible: false,
      dialogVisible: false,
      activeRow: {
        avmatCatalogName: '',
        avmatCatalogCode: '',
        avmatCategoriesCode: '',
        avmatCategoriesName: '',
        envFactorName: this.$route.params.name,
        envFactorId: this.$route.params.id,
        factor: this.$route.params.value
      },
      rules: {
        factor: [{ validator: validatePass, trigger: 'blur' }]
      },
      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 initData() {
      const { data } = await getAvmatCategoriesTree()
      this.menuTreeData = [{ avmatCategoriesCode: '', avmatCategoriesName: '全部', children: data }]
      this.fetchTableData({ pageIndex: 1, pageSize: 20, avmatCategoriesCode: this.currentNodeKey, avmatCategoriesId: this.avmatCategoriesId })
    },

    handleRegularities() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$emit('handleRegularities', { type: this.dialogType, ...this.activeRow })
        } else {
          return false
        }
      })
    },

    openModal(params) {
      const { title, type } = params
      this.dialogTitle = title
      this.dialogType = type
      this.activeRow.envFactorName = this.$route.params.name
      this.activeRow.envFactorId = this.$route.params.id
      this.activeRow.factor = this.$route.params.value
      this.regularitiesChooseVisible = true
    },

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

    closeModal() {
      this.regularitiesChooseVisible = false
      this.activeRow = {
        avmatCatalogName: '',
        avmatCatalogCode: '',
        avmatCategoriesCode: '',
        avmatCategoriesName: '',
        factor: '',
        avmatCatalogId: ''
      }
      this.closefittingsVisible()
    },

    openFittingsModal() {
      this.initData()
      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 })
    },

    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
    },

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

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