<template>
  <div class="view-dataSailingMaterialClassification">
    <div class="common-box">
      <div class="common-box-button">
        <el-button type="success" @click="handleAdd">新增</el-button>
        <el-button type="warning" @click="remove(removeArr)">删除</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="common-box-search">
        <el-input placeholder="请输入分类名称/编号" v-model="keyword" class="input-with-select" @keyup.enter.native="fetchTableData">
          <el-button slot="append" icon="el-icon-search" @click="fetchTableData(1)"></el-button>
        </el-input>
      </div>
    </div>
    <LTable ref="table" @select-all="selectAll" @selection-change="selection" :defaultFetch="true" :columns="columns" :dataSource="tableData" :options="options" :fetch="fetchTableData" />
    <el-dialog :title="title" :visible.sync="dialogVisible" width="600px" :before-close="handleClose">
      <el-form :model="activeRow" :rules="rules" ref="form" label-width="80px">
        <el-form-item label="上级分类" prop="parentCategoriesId">
          <el-select ref="onSelect" v-model="activeRow.parentCategoriesId" placeholder="请选择" class="department-select">
            <el-option hidden :value="activeRow.parentCategoriesId" :label="activeRow.parentAvmatCategoriesName"></el-option>
            <el-tree default-expand-all :data="tableData" :props="defaultProps" @node-click="treeNodeClick"></el-tree>
          </el-select>
        </el-form-item>
        <el-form-item label="分类名称" prop="avmatCategoriesName">
          <el-input placeholder="请输入分类名称" v-model="activeRow.avmatCategoriesName"></el-input>
        </el-form-item>
        <el-form-item label="编号" prop="avmatCategoriesCode">
          <el-input placeholder="请输入编号" v-model="activeRow.avmatCategoriesCode"></el-input>
        </el-form-item>
        <el-form-item label="顺序" prop="number">
          <el-input placeholder="请输入顺序" v-model="activeRow.number"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="crudDialogSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 导入 -->
    <ImportDialog ref="ImportDialog" @submitCallBack="ImportDialogCallBack" />
  </div>
</template>

<script>
import { columns, options, rules } from './index'
import { getAvmatCategoriesTree, addAvmatCategorie, updateAvmatCategorie, removeAvmatCategories, downLoadFile, exportExcel } from '@/api/basicData/dataSailingMaterialClassification'
import ImportDialog from './importDialog/index.vue'
import { deepClone, debounce } from '@/utils/index'

export default {
  name: 'dataSailingMaterialClassification',
  components: { ImportDialog },
  data() {
    // 这里存放数据
    return {
      keyword: '',
      removeArr: [],
      columns: columns(this),
      options,
      tableData: [],
      dialogVisible: false,
      title: '新增',
      treeData: [],
      defaultProps: {
        label: 'avmatCategoriesName',
        children: 'children'
      },
      activeRow: {
        number: null,
        avmatCategoriesName: null,
        parentCategoriesId: null,
        avmatCategoriesCode: null
      },
      rules,
      debounceFn: debounce(this.fetchTableData, 500)
    }
  },
  watch: {
    keyword() {
      this.debounceFn()
    }
  },
  methods: {
    async fetchTableData() {
      const { data } = await getAvmatCategoriesTree({
        keyword: this.keyword
      })
      this.tableData = data
    },

    treeNodeClick(data) {
      const { avmatCategoriesName, avmatCategoriesId } = data
      this.activeRow.parentAvmatCategoriesName = avmatCategoriesName
      this.activeRow.parentCategoriesId = avmatCategoriesId
      this.$refs.onSelect.blur()
    },

    reset() {
      this.activeRow = {
        number: null,
        avmatCategoriesName: null,
        parentCategoriesId: null,
        avmatCategoriesCode: null
      }
    },
    handleClose() {
      this.$refs.form.resetFields()
      this.activeRow = {
        number: null,
        avmatCategoriesName: null,
        parentCategoriesId: null,
        avmatCategoriesCode: null
      }
      this.dialogVisible = false
    },
    handleAdd() {
      this.title = '新增'
      this.activeRow = {
        number: null,
        avmatCategoriesName: null,
        parentCategoriesId: null,
        avmatCategoriesCode: null
      }
      this.dialogVisible = true
    },
    handUpdate(row) {
      this.title = '编辑'
      this.activeRow = deepClone(row)
      this.dialogVisible = true
    },
    handRemove(row) {
      this.$confirm('是否确认删除该数据', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const { code } = await removeAvmatCategories([row.avmatCategoriesId])
          if (code == 200) {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
          }
        })
        .catch(() => {})
    },
    // 全选
    selectAll(data) {
      this.removeArr = []
      if (data.length > 0) {
        data.forEach((item) => {
          this.removeArr.push(item)
        })
      } else {
        this.removeArr = []
      }
    },
    // 单选
    selection(data) {
      this.removeArr = []
      data.forEach((item) => {
        this.removeArr.push(item)
      })
    },
    // handleExport(type) {
    //   let msg = ''
    //   type == '下载模板' ? (msg = '是否确认下载该模板') : (msg = `是否${type}该航材`)
    //   this.$confirm(msg, '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   })
    //     .then(() => {
    //       this.$message({
    //         type: 'success',
    //         message: '操作成功!'
    //       })
    //     })
    //     .catch(() => {})
    // },
    crudDialogSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.title == '新增') {
            const { code } = await addAvmatCategorie(this.activeRow)
            if (code == 200) {
              this.$message({
                type: 'success',
                message: '新增成功!'
              })
              this.dialogVisible = false
              this.fetchTableData()
              this.reset()
            }
          }
          if (this.title == '编辑') {
            const { code } = await updateAvmatCategorie(this.activeRow)
            if (code == 200) {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.dialogVisible = false
              this.fetchTableData()
              this.reset()
            }
          }
        } else {
          return false
        }
      })
    },
    operation(row, statu) {
      this.$confirm(`是否${statu == 0 ? '禁用' : '启用'}该分类`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          row.status = statu
          const { code } = await updateAvmatCategorie(row)
          if (code == 200) {
            this.$message({
              type: 'success',
              message: '修改成功!'
            })
            this.dialogVisible = false
            this.fetchTableData()
            this.reset()
          }
        })
        .catch(() => {})
    },
    recursion(data, row, statu) {
      for (let i = 0; i < data.length; i++) {
        const item = data[i]
        if (item.id == row.id) {
          row.status = statu
          return
        }
        if (item.children && item.children.length) {
          this.recursion(item.children, row, statu)
        }
      }
    },
    //更多按钮
    handleExport(type) {
      this[type]()
    },
    //导入
    importOperation() {
      this.$refs.ImportDialog.triggerDialog()
    },
    //导入回写
    ImportDialogCallBack() {
      this.fetchTableData()
    },

    //导出全部
    async allExport() {
      await exportExcel()
    },
    //下载模板
    async downloadTemplate() {
      await downLoadFile()
    }
  }
}
</script>

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