<template>
  <div class="icons-container">
    <el-container>
      <el-aside width="300px" style="background-color:#fff;height:calc(100vh - 104px) ;">
        <el-input v-model="filterText" clearable placeholder="输入关键字进行过滤" style="margin:10px 0" />
        <el-tree
          v-loading="treeLoading"
          ref="tree"
          :data="treeData"
          :props="defaultProps"
          :filter-node-method="filterNode"
          class="filter-tree"
          accordion
          @node-click="handleNodeClick" />
      </el-aside>
      <el-main>
        <div class="app-ture">
          <div class="filter-btn-group">
            <div>
              <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
              <el-button type="danger" icon="el-icon-delete" plain @click="batchDel">删除</el-button>
              <el-button v-show="INDUSTRYTYPE_ID != '0'" icon="el-icon-arrow-left" @click="getList(PARENT_ID)">返回</el-button>
            </div>
          </div>
          <el-table v-loading="listLoading" ref="multipleTable" :data="varList" :row-key="getRowKey" :header-cell-style="{'font-weight': 'bold','color': '#000'}" tooltip-effect="dark" border fit highlight-current-row>
            <el-table-column :reserve-selection="true" type="selection" width="55" align="center" />
            <el-table-column type="index" label="序号" width="50" align="center" />
            <el-table-column label="名称">
              <template slot-scope="{row}">
                <div class="link-type" @click="getList(row.INDUSTRYTYPE_ID)">
                  <svg-icon :icon-class="row.NAME" class-name="disabled" /> {{ row.NAME }} <i class="el-icon-arrow-right" />
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="SORT" label="排序" />
            <el-table-column label="操作" align="center" width="200">
              <template slot-scope="{row}">
                <el-button type="text" @click="handleEdit(row.INDUSTRYTYPE_ID)">编辑</el-button>
                <span class="app-divider"/>
                <el-button type="text" class="color-red" @click="handleDelete(row.INDUSTRYTYPE_ID)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="page-btn-group">
            <div/>
            <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList()" />
          </div>
        </div>
      </el-main>
    </el-container>

    <el-dialog :visible.sync="dialogFormEdit" :title="dialogType==='edit'?'修改':'新增'" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" style="width: 500px;">
        <el-form-item label="名称" prop="NAME">
          <el-input v-model="form.NAME" placeholder="这里输入名称..." />
        </el-form-item>
        <el-form-item label="排序" prop="SORT">
          <el-input id="SORT" ref="SORT" v-model.number="form.SORT" maxlength="11" placeholder="这里输入排序..." title="排序"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormEdit = false">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      filterText: '',
      treeData: [],
      defaultProps: {
        // value: 'INDUSTRYTYPE_ID',
        children: 'nodes',
        label: 'NAME'
      },
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      treeLoading: false,
      total: 0,
      INDUSTRYTYPE_ID: '0',
      PARENT_ID: '0',
      KEYWORDS: '',
      varList: [],
      pd: [],
      form: {
        NAME: '', // 名称
        PARENT_ID: '', // 上级ID
        SORT: '' // 排序
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      dialogFormEdit: false,
      dialogType: 'add',
      rules: {
        NAME: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
        PARENT_ID: [{ required: true, message: '上级ID不能为空', trigger: 'blur' }],
        SORT: [
          { required: true, message: '排序不能为空', trigger: 'change' },
          { type: 'number', message: '排序必须为数字' }
        ]
      }
    }
  },
  watch: {
    filterText(val) {
      console.log(val)
      this.$refs.tree.filter(val)
    }
  },
  created() {
    this.getList('0')
    this.getTreeList()
  },
  methods: {
    // 左侧树
    getTreeList() {
      this.treeLoading = true
      requestFN(
        '/industrytype/getIndustryTree',
        {
        }
      ).then((data) => {
        console.info(data.zTreeNodes)
        this.treeLoading = false
        this.treeData = data.zTreeNodes
        console.info(this.treeData)
      }).catch((e) => {
        this.treeLoading = false
      })
    },

    // 增加和编辑时的下拉框
    handleNodeClick(node, data, value) {
      this.getList(node.INDUSTRYTYPE_ID)
    },

    filterNode(value, data) {
      if (!value) return true
      return data.NAME.indexOf(value) !== -1
    },

    getRowKey(row) {
      return row.INDUSTRYTYPE_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 获取列表
    getList(INDUSTRYTYPE_ID) {
      this.listLoading = true
      this.varList = []
      console.info(INDUSTRYTYPE_ID)
      if (INDUSTRYTYPE_ID) {
        this.INDUSTRYTYPE_ID = INDUSTRYTYPE_ID
      }
      // if(PARENT_ID){
      //   this.PARENT_ID = PARENT_ID
      // }
      requestFN(
        '/industrytype/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          PARENT_ID: this.PARENT_ID,
          INDUSTRYTYPE_ID: this.INDUSTRYTYPE_ID
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.PARENT_ID = data.PARENT_ID
        this.total = data.page.totalResult
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.dialogType = 'add'
      this.dialogFormEdit = true
      this.resetForm()
    },
    // 修改
    handleEdit(ID) {
      this.dialogType = 'edit'
      requestFN(
        '/industrytype/goEdit',
        {
          INDUSTRYTYPE_ID: ID

        }
      ).then((data) => {
        this.form = Object.assign({}, data.pd) // copy obj
      }).catch((e) => {
        this.listLoading = false
      })
      this.dialogFormEdit = true
    },
    // 保存
    confirm() {
      this.form.PARENT_ID = this.INDUSTRYTYPE_ID
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/industrytype/' + this.dialogType,
            this.form
          ).then((data) => {
            this.listLoading = false
            this.dialogFormEdit = false
            this.getList(this.INDUSTRYTYPE_ID)
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/industrytype/delete',
          {
            INDUSTRYTYPE_ID: id
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 批量删除
    batchDel() {
      const _selectData = this.$refs.multipleTable.selection
      if (_selectData == null || _selectData.length == 0) {
        this.$message({
          message: '请选中要删除的项...',
          type: 'error'
        })
        return false
      }
      const ids = _selectData.map((item, index) => {
        return item.INDUSTRYTYPE_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/industrytype/deleteAll',
          {
            DATA_IDS: ids
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.$refs.multipleTable.clearSelection()
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'industrytype:add,industrytype:del,industrytype:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.industrytypefhadminadd		// 新增权限
        this.del = data.industrytypefhadmindel		// 删除权限
        this.edit = data.industrytypefhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    resetForm() {
      this.form = {
        NAME: '', // 名称
        PARENT_ID: '', // 上级ID
        SORT: '', // 排序
        POSTTYPE_ID: ''// 岗位类型ID
      }
    }
  }
}

</script>
