<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="KEYWORDS" placeholder="搜索" class="filter-item" style="width: 200px;"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
        搜索
      </el-button>
    </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 prop="CHECKITEMNAME" label="检查项名称" />
      <el-table-column label="操作" align="center" width="500">
        <template slot-scope="{row}">
          <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(row.CHECKITEMBASE_ID)">编辑</el-button>
          <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="handleConfigDept(row.CHECKITEMBASE_ID)">配置部门</el-button>
          <el-button v-show="edit" type="primary" icon="el-icon-edit" size="mini" @click="handleConfigType(row.CHECKITEMBASE_ID)">配置企业类型</el-button>
          <el-button v-show="del" type="danger" icon="el-icon-delete" size="mini" @click="handleDelete(row.CHECKITEMBASE_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button v-show="add" type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="batchDel">删除</el-button>
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog :visible.sync="dialogFormEdit" :title="dialogType==='edit'?'修改':'新增'" width="600px">
      <el-form ref="form" :rules="rules" :model="form" label-width="110px" style="width: 500px;">
        <el-form-item label="检查项名称" prop="CHECKITEMNAME">
          <el-input id="CHECKITEMNAME" ref="CHECKITEMNAME" v-model="form.CHECKITEMNAME" maxlength="255" 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>

    <el-dialog :visible.sync="dialogFormDept" title="配置部门" width="600px">
      <el-form label-width="110px" style="width: 500px;">
        <el-tree
          v-loading="treeLoading"
          ref="deptTree"
          :data="deptTree"
          :props="deptProps"
          node-key="id"
          show-checkbox
          class="filter-tree"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormDept = false">取 消</el-button>
        <el-button type="primary" @click="configDept">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormType" title="配置类型" width="600px">
      <el-form label-width="110px" style="width: 500px;">
        <el-tree
          v-loading="treeLoading"
          ref="typeTree"
          :data="typeTree"
          :props="typeProps"
          node-key="id"
          show-checkbox
          class="filter-tree"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormType = false">取 消</el-button>
        <el-button type="primary" @click="configType">确 定</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 {
      treeLoading: false,
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      varList: [],
      pd: [],
      form: {
        CHECKITEMNAME: ''// 检查项名称
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      dialogFormEdit: false,
      dialogType: 'add',
      rules: {
        CHECKITEMNAME: [{ required: true, message: '检查项名称不能为空', trigger: 'blur' }]
      },
      dialogFormDept: false,
      CHECKITEMBASE_ID: '',
      deptTree: [],
      deptProps: {
        value: 'id',
        children: 'nodes',
        label: 'name'
      },
      dialogFormType: false,
      typeTree: [],
      typeProps: {
        value: 'id',
        children: 'nodes',
        label: 'name'
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getRowKey(row) {
      return row.CHECKITEMBASE_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/checkitembase/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取列表
    getDeptTree(ID) {
      requestFN(
        '/checkitembase/goAdd',
        {
          CHECKITEMBASE_ID: ID
        }
      ).then((data) => {
        this.deptTree = JSON.parse(data.zTreeNodes)
        this.$refs.deptTree.setCheckedKeys(data.checked)
      }).catch((e) => {
      })
    },
    // 获取列表
    getTypeTree(ID) {
      requestFN(
        '/checkitemtocorpinfo/goAdd',
        {
          CHECKITEMBASE_ID: ID
        }
      ).then((data) => {
        this.typeTree = JSON.parse(data.zTreeNodes)
        this.$refs.typeTree.setCheckedKeys(data.checked)
      }).catch((e) => {
      })
    },
    // 添加
    handleAdd() {
      this.dialogType = 'add'
      this.resetForm()
      this.dialogFormEdit = true
    },
    // 修改
    handleEdit(ID) {
      this.dialogType = 'edit'
      requestFN(
        '/checkitembase/goEdit',
        {
          CHECKITEMBASE_ID: ID
        }
      ).then((data) => {
        this.form = Object.assign({}, data.pd) // copy obj
      }).catch((e) => {
        this.listLoading = false
      })
      this.dialogFormEdit = true
    },
    handleConfigDept(ID) {
      this.dialogFormDept = true
      this.CHECKITEMBASE_ID = ID
      this.getDeptTree(ID)
    },
    configDept() {
      requestFN(
        '/checkitembase/config',
        {
          CHECKITEMBASE_ID: this.CHECKITEMBASE_ID,
          IDS: this.$refs.deptTree.getCheckedKeys().join(',')
        }
      ).then((data) => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.dialogFormDept = false
        this.getList()
      }).catch((e) => {
      })
    },
    handleConfigType(ID) {
      this.dialogFormType = true
      this.CHECKITEMBASE_ID = ID
      this.getTypeTree(ID)
    },
    configType() {
      requestFN(
        '/checkitemtocorpinfo/config',
        {
          CHECKITEMBASE_ID: this.CHECKITEMBASE_ID,
          IDS: this.$refs.typeTree.getCheckedKeys().join(',')
        }
      ).then((data) => {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.dialogFormType = false
        this.getList()
      }).catch((e) => {
      })
    },
    // 保存
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/checkitembase/' + this.dialogType,
            this.form
          ).then((data) => {
            this.$message({
              message: '操作成功',
              type: 'success'
            })
            this.listLoading = false
            this.dialogFormEdit = false
            this.getList()
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },

    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/checkitembase/delete',
          {
            CHECKITEMBASE_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.CHECKITEMBASE_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/checkitembase/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 = 'checkitembase:add,checkitembase:del,checkitembase:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.checkitembasefhadminadd		// 新增权限
        this.del = data.checkitembasefhadmindel		// 删除权限
        this.edit = data.checkitembasefhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    resetForm() {
      this.form = {
        CHECKITEMNAME: ''// 检查项名称
      }
    }
  }
}

</script>
