<template>
  <div class="app-warp">
    <div class="app-structure">
      <div class="app-tools">
        <div class="app-search">
          <el-input v-model="KEYWORDS" placeholder="搜索" class="filter-item" style="width: 200px;"/>
          <el-button v-waves class="ml5" type="primary" icon="el-icon-search" @click="getQuery">
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" type="primary" @click="goKeyReset">
            重置
          </el-button>
        </div>
        <div class="app-minbtn">
          <el-button type="text" icon="el-icon-refresh-left"/>
          <el-button type="text" icon="el-icon-full-screen"/>
        </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 prop="NAME" label="名称" />
        <el-table-column prop="SORT" label="排序" />
        <el-table-column prop="TRAINING_TYPE_NAME" label="培训类型" />
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="{row}">
            <el-button type="text" @click="handleEdit(row.POSTTYPE_ID)">编辑</el-button>
            <span class="app-divider"/>
            <el-button type="text" class="color-red" @click="handleDelete(row.POSTTYPE_ID)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="page-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>
        </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="NAME">
            <el-input id="NAME" ref="NAME" v-model="form.NAME" maxlength="255" placeholder="这里输入名称..." title="名称"/>
          </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-item label="培训类型" prop="TRAININGTYPE_ID">
            <el-select v-model="form.TRAININGTYPE_ID" maxlength="255" placeholder="请选择" clearable style="width:100%">
              <el-option v-for="item in trainingList" :key="item.TRAININGTYPE_ID" :label="item.NAME" :value="item.TRAININGTYPE_ID"/>
            </el-select>
          </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>
  </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 {
      trainingList: [],
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      PARENT_ID: '0',
      KEYWORDS: '',
      varList: [],
      pd: [],
      form: {
        NAME: '', // 名称
        PARENT_ID: '', // 上级ID
        SORT: '' // 排序
        // TRAININGTYPE_ID: ''// 所属培训类型ID
      },
      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: '排序必须为数字' }
        ],
        TRAININGTYPE_ID: [{ required: true, message: '所属培训类型不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList(this.ROLE_ID)
    this.getDict()
  },
  methods: {
    // 下拉
    getDict() {
      requestFN(
        'trainingtype/listAll',
        {
        }
      ).then((data) => {
        this.trainingList = data.varList
        console.info(this.trainingList)
      })
    },
    getRowKey(row) {
      return row.POSTTYPE_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 获取列表
    getList(PARENT_ID) {
      this.listLoading = true
      this.PARENT_ID = PARENT_ID
      requestFN(
        '/posttype/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          TRAININGTYPE_ID: this.TRAININGTYPE_ID || null,
          NAME: this.NAME,
          PARENT_ID: 0,
          SORT: this.SORT,
          POSTTYPE_ID: this.POSTTYPE_ID

        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.dialogType = 'add'
      this.resetForm()
      this.getDict()
      this.dialogFormEdit = true
    },
    // 修改
    handleEdit(ID) {
      this.getDict()
      this.dialogType = 'edit'
      requestFN(
        '/posttype/goEdit',
        {
          POSTTYPE_ID: ID
        }
      ).then((data) => {
        this.form = Object.assign({}, data.pd) // copy obj
      }).catch((e) => {
        this.listLoading = false
      })
      this.dialogFormEdit = true
    },
    // 保存
    confirm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/posttype/' + this.dialogType,
            this.form
          ).then((data) => {
            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(
          '/posttype/delete',
          {
            POSTTYPE_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.POSTTYPE_ID
      }).join(',')

      this.$confirm('确定要删除选中的数据吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/posttype/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 = 'posttype:add,posttype:del,posttype:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.posttypefhadminadd		// 新增权限
        this.del = data.posttypefhadmindel		// 删除权限
        this.edit = data.posttypefhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },

    resetForm() {
      this.form = {
        NAME: '', // 名称
        PARENT_ID: 0, // 上级ID
        SORT: '', // 排序
        TRAININGTYPE_ID: ''// 所属培训类型ID
      }
    },
    goKeyReset() {
      this.KEYWORDS = ''
      this.listQuery = {
        page: 1,
        limit: 10
      }
      this.getList()
    }
  }
}

</script>
