<template>
  <div>
    <en-table-layout :tableData="tableData.data" :loading="loading" @selection-change="handleSelectionChange">
      <div slot="toolbar" class="inner-toolbar">
        <div class="toolbar-btns">
          <el-button size="mini" v-if="checkPermission(['station:add'])" type="primary"
            @click="handleAddStation">新增</el-button>
          <el-button size="mini" v-if="checkPermission(['station:del'])" type="danger"
            @click="handleDeleteStationAll()">删除</el-button>
        </div>
        <div class="toolbar-search">
          <en-table-search @search="searchEvent" placeholder="请输入关键字搜索" />
        </div>
      </div>

      <template slot="table-columns">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="职位名称" />
        <el-table-column prop="sn" label="岗位编码" />
        <el-table-column prop="sort" label="显示顺序" />
        <el-table-column prop="description" label="职位描述" />
        <el-table-column prop="members" label="职位成员" />
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" v-if="checkPermission(['station:edit'])"
              @click="handleEditStation(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" v-if="checkPermission(['station:del'])" type="danger"
              @click="handleDeleteStation(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </template>

      <el-pagination v-if="tableData" slot="pagination" @size-change="handlePageSizeChange"
        @current-change="handlePageCurrentChange" :current-page="tableData.page_no" :page-sizes="[10, 20, 50, 100]"
        :page-size="tableData.page_size" layout="total, sizes, prev, pager, next, jumper" :total="tableData.data_total">
      </el-pagination>
    </en-table-layout>
    <el-dialog :title="stationForm.id ? '编辑岗位' : '添加岗位'" :visible.sync="dialogVisible" width="500px"
      @open="handleDialogOpen" :modal-append-to-body="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form :model="stationForm" :rules="stockRules" ref="stationForm" label-width="120px">
        <el-form-item label="职位编码" prop="sn">
          <el-input v-model="stationForm.sn" :minlength="2" :maxlength="20" clearable placeholder="请输入职位编号"></el-input>
        </el-form-item>
        <el-form-item label="职位名称" prop="name">
          <el-input v-model="stationForm.name" :minlength="2" :maxlength="20" clearable placeholder="请输入职位名称"></el-input>
        </el-form-item>
        <el-form-item label="显示顺序" prop="sort">
          <el-input v-model="stationForm.sort" :maxlength="2" clearable placeholder="请输入显示顺序"></el-input>
        </el-form-item>
        <el-form-item label="职位描述" prop="description">
          <el-input v-model="stationForm.description" type="textarea" :autosize="{ minRows: 3, maxRows: 4 }"
            placeholder="请输入职位描述" :maxlength="100"></el-input>
        </el-form-item>
        <el-form-item label="职位成员" prop="members" v-if="stationForm.id">
          <el-input v-model="stationForm.members" :disabled="true" type="textarea" :autosize="{ minRows: 3, maxRows: 4 }"
            placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitStationForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as API_Setting from '@/api/setting'

export default {
  name: 'station',
  data() {
    return {
      // 列表loading状态
      loading: false,
      // 列表参数
      params: {
        page_no: 1,
        page_size: 10
      },
      // 列表数据
      tableData: '',
      // 岗位表单
      stationForm: {},
      // 岗位表单 规则
      stockRules: {
        username: [
          this.MixinRequired('请输入管理员名称！'),
          { min: 2, max: 20, message: '长度在 2 到 20 个字符' },
          {
            validator: (rule, value, callback) => {
              if (value && !RegExp.userName.test(value)) {
                callback(new Error('只支持汉字、字母、数字、“-”、“_”的组合！'))
              } else {
                callback()
              }
            }
          }],
        password: [
          { required: true, message: '请输入管理员密码！', trigger: 'bulr' },
          {
            validator: (rule, value, callback) => {
              if (value && !RegExp.password.test(value)) {
                callback(new Error('密码应为6-20位数字、英文字母，或者特殊字符！'))
              } else {
                callback()
              }
            }
          }
        ],
        department: [this.MixinRequired('请输入所属部门！')],
        role_id: [{ required: true, message: '请选择所属角色！', trigger: 'change' }],
        face: [this.MixinRequired('请上传管理员头像！')]
      },
      // 岗位表单 dialog
      dialogVisible: false,
      multipleSelection: []
    }
  },
  mounted() {
    this.GET_StationList()
  },
  methods: {
    // 单选或者全选
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    /** 分页大小发生改变 */
    handlePageSizeChange(size) {
      this.params.page_size = size
      this.GET_StationList()
    },

    /** 分页页数发生改变 */
    handlePageCurrentChange(page) {
      this.params.page_no = page
      this.GET_StationList()
    },

    /** 添加岗位 */
    handleAddStation() {
      this.stationForm = {}
      this.dialogVisible = true
    },

    /** 编辑岗位 */
    handleEditStation(index, row) {
      this.stationForm = this.MixinClone(row)
      this.dialogVisible = true
    },
    handleDeleteStationAll() {
      this.$confirm('确定要删除这些岗位吗？', '提示', { type: 'warning' }).then(() => {
        const ids = this.multipleSelection.map(item => item.id)
        API_Setting.deletesStation(ids).then(() => {
          this.$message.success('删除成功！')
          this.GET_StationList()
        })
      }).catch(() => { })
    },
    /** 删除岗位 */
    handleDeleteStation(index, row) {
      this.$confirm('确定要删除这个岗位吗？', '提示', { type: 'warning' }).then(() => {
        API_Setting.deleteStation(row.id).then(() => {
          this.$message.success('删除成功！')
          this.GET_StationList()
        })
      }).catch(() => { })
    },
    /** dialog打开后重置form表单校验结果 */
    handleDialogOpen() {
      setTimeout(() => { this.$refs['stationForm'].clearValidate() })
    },

    /** 提交岗位表单 */
    submitStationForm() {
      this.$refs['stationForm'].validate((valid) => {
        if (valid) {
          const { id } = this.stationForm
          const params = this.MixinClone(this.stationForm)
          if (id) {
            API_Setting.editStation(id, params).then(response => {
              this.dialogVisible = false
              this.$message.success('修改成功！')
              this.GET_StationList()
            })
          } else {
            API_Setting.addStation(params).then(() => {
              this.dialogVisible = false
              this.$message.success('添加成功！')
              this.GET_StationList()
            })
          }
        } else {
          this.$message.error('表单填写有误，请核对！')
          return false
        }
      })
    },

    /** 搜索事件触发 */
    searchEvent(data) {
      this.params = {
        ...this.params,
        keyword: data
      }
      this.params.page_no = 1
      this.GET_StationList()
    },

    /** 获取岗位列表 */
    GET_StationList() {
      this.loading = true
      API_Setting.getStationList(this.params).then(response => {
        this.loading = false
        this.tableData = response
      }).catch(() => { this.loading = false })
    }
  }
}
</script>

<style type="text/scss" lang="scss" scoped>
.face-image {
  display: block;
  width: 50px;
  height: 50px;
  margin: 0 auto;
}

/deep/ .el-dialog__body {
  padding: 10px 20px;
}
</style>
