<template>
  <div class="view-carrierSailingMaterialConsumeRegularities">
    <div class="view-carrierSailingMaterialConsumeRegularities-left">
      <MenuTree node-key="avmatCategoriesCode" :currentNodeKey="currentNodeKey" :treedata="menuTreeData" @TreeNodeclick="treeNodeClick" v-bind="treeObj"> </MenuTree>
    </div>
    <div class="view-carrierSailingMaterialConsumeRegularities-right">
      <div class="common-box">
        <div class="common-box-button">
          <el-button type="success" @click="openDialog">新增</el-button>
          <el-button type="warning" @click="handleDelete(removeArr)" :disabled="removeArr.length == 0">删除</el-button>
          <!-- <el-button type="primary" @click="handleExport('导入')">导入模板</el-button>
          <el-button type="primary" @click="handleExport('导出')">导出模板</el-button> -->
        </div>
        <div class="common-box-search">
          <el-input placeholder="航材名称" v-model="avmatCategoriesName" class="input-with-select">
            <el-button slot="append" icon="el-icon-search" @click="fetchTableData"></el-button>
          </el-input>
        </div>
      </div>
      <LTable ref="table" @selection-change="selection" :defaultFetch="false" :columns="columns" :dataSource="tableData" :dictData="dictData" :options="options" :fetch="fetchTableData" :pagination="tableRequset"></LTable>
    </div>
    <RegularitiesChoose ref="RegularitiesChoose" :monadOptions="dictData.life_unit" @handleRegularities="handleRegularities" />
  </div>
</template>

<script>
import RegularitiesChoose from './components/index.vue'
import { defaultProps, columns, options } from './index'
import { compareObj, debounce } from '@/utils/index'
import { getDict } from '@/api/dict'
import { getAvmatCategoriesTree } from '@/api/basicData/dataSpecies'
import { getAvmatConsumeRule, addAvmatConsumeRuleVo, updateAvmatConsumeRule, removeAvmatConsumeRule } from '@/api/carrierStandard/carrierSailingMaterialConsumeRegularities'
export default {
  name: 'carrierSailingMaterialConsumeRegularities',
  components: { RegularitiesChoose },
  data() {
    // 这里存放数据
    return {
      currentNodeKey: '0',
      menuTreeData: [],
      treeObj: {
        title: '所属分类',
        activityheight: '275px',
        searchIcon: false,
        configure: defaultProps
      },
      options,
      columns: columns(this),
      tableData: [],
      avmatCategoriesName: '',
      tableRequset: {
        total: 0,
        pageIndex: 1,
        pageSize: 10
      },
      regularitiesChooseVisible: false,
      activeRow: {},
      dictData: {},
      removeArr: [],
      avmatCategoriesId: '',
      debounceFn: debounce(this.fetchTableData, 500)
    }
  },
  created() {
    this.getTreeData()
    this.getDict()
  },
  watch: {
    avmatCategoriesName() {
      this.tableRequset.pageIndex = 1
      this.debounceFn()
    }
  },
  methods: {
    async getDict() {
      const { data } = await getDict('life_unit')
      this.dictData.life_unit = data
    },

    async fetchTableData() {
      const { pageIndex, pageSize } = this.tableRequset
      const {
        data: { list, totalCount }
      } = await getAvmatConsumeRule({
        avmatCategoriesId: this.avmatCategoriesId,
        avmatCategoriesName: this.avmatCategoriesName,
        avmatCategoriesCode: this.currentNodeKey,
        pageIndex,
        pageSize
      })
      this.tableData = list
      this.tableRequset.total = totalCount
    },

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

    selection(data) {
      this.removeArr = data
    },

    openDialog() {
      this.$refs.RegularitiesChoose.initData()
      this.$refs.RegularitiesChoose.openDialog({
        type: 'add',
        title: '新增'
      })
    },

    treeNodeClick(node) {
      this.currentNodeKey = node.avmatCategoriesCode
      this.avmatCategoriesId = node.avmatCategoriesId
      this.fetchTableData()
    },

    handUpdate(row) {
      this.$refs.RegularitiesChoose.initData()
      this.$refs.RegularitiesChoose.handUpdateRow(row)
      this.$refs.RegularitiesChoose.openDialog({
        type: 'edit',
        title: '编辑'
      })
    },

    async handleRegularities(params) {
      const Add_Submit = 'add'
      const edit_Submit = 'edit'
      const {
        type,
        params: { consumeRuleId, avmatCatalogId, mtbf, LifeUnit: lifeUnit }
      } = params
      switch (type) {
        case Add_Submit:
          await this.addTableRow({ avmatCatalogId, mtbf, lifeUnit })
          break
        case edit_Submit:
          await this.editTableRow({ consumeRuleId, avmatCatalogId, mtbf, lifeUnit })
          break
      }
      this.$refs.RegularitiesChoose.closeDialog()
      this.fetchTableData()
    },

    async addTableRow(params) {
      try {
        const { data } = await addAvmatConsumeRuleVo(params)
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
      } catch (error) {}
    },

    async editTableRow(params) {
      try {
        const { data } = await updateAvmatConsumeRule(params)
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
      } catch (error) {}
    },

    async deleteTableRow(params) {
      try {
        const { data } = await removeAvmatConsumeRule(params)
        this.$message({
          type: 'success',
          message: '操作成功!'
        })
      } catch (error) {}
      this.$refs.table.clearSelection()
      this.fetchTableData()
    },

    handleDelete(params) {
      this.$confirm('是否确认删除选中数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await this.deleteTableRow(params.map((e) => e.consumeRuleId))
        })
        .catch(() => {})
    },

    handleExport(type) {
      let msg = ''
      type == '下载模板' ? (msg = '是否确认下载该模板') : (msg = `是否${type}该航材`)
      this.$confirm(msg, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
        .catch(() => {})
    }
  }
}
</script>

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