<!--
 * @Description: create
 * @Version: 1.0
 * @Autor: XuTongZhang
 * @Date: 2020-07-28 17:19:43
 * @LastEditors: XuTongZhang
 * @LastEditTime: 2020-08-15 11:18:19
-->
<template>
<div class="indexPage">
    <v-input :btn="btn" :list="list" @add="add"></v-input>
    <v-table :table="table" :tableList="tableList" :sortType="true" :queryData="queryData" @details="details" @editor="editor" @del="del"></v-table>
    <v-pager @page="callPage" :total="totalrecords"></v-pager>
    <el-dialog width="600px" :visible.sync="dialogFormVisible" :before-close="close" :close-on-click-modal="false">
        <el-form :model="form" ref="form" label-width="140px" :rules="rules" label-position="left">
            <el-form-item label="职位名称" prop="positionName">
                <el-input v-if="state!==2" v-model="form.positionName" placeholder="请输入职位名称" autocomplete="off" maxlength="32" show-word-limit></el-input>
                <div v-else>{{form.positionName}}</div>
            </el-form-item>
            <el-form-item label="职位描述" prop="positionIntroduce">
                <el-input v-if="state!==2" type="textarea" :rows="4" resize="none" placeholder="请输入内容" v-model="form.positionIntroduce" maxlength="1024" show-word-limit></el-input>
                <div v-else>{{form.positionIntroduce}}</div>
            </el-form-item>
            <el-form-item label="职位状态" prop="isDisable">
                <el-radio-group v-if="state!==2" v-model="form.isDisable">
                    <el-radio :label="0">启用</el-radio>
                    <el-radio :label="1">禁用</el-radio>
                </el-radio-group>
                <div v-else>{{['启用', '禁用'][form.isDisable]}}</div>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" v-if="state!==2" @click="determine">确 定</el-button>
        </div>
    </el-dialog>
</div>
</template>

<script>
export default {
  data () {
    return {
      tableList: [],
      dialogFormVisible: false,
      page: 1,
      form: {
        positionName: '',
        positionIntroduce: '',
        isDisable: 0
      },
      totalrecords: 0,
      state: 0,
      pickList: [],
      rules: {
        positionName: [{
          required: true,
          message: '请填写职位名称',
          trigger: 'change'
        }],
        positionIntroduce: [{
          required: true,
          message: '请填写职位描述',
          trigger: 'change'
        }],
        isDisable: [{
          required: true,
          message: '请选择职位状态',
          trigger: 'change'
        }]
      },
      list: [],
      btn: [{
        name: '添加',
        type: 'success',
        method: 'add'
      }],
      table: {
        column: [{
          label: '序号',
          props: 'serialNumber'
        },
        {
          label: '职位名称',
          props: 'positionName'
        },
        {
          label: '职位描述',
          props: 'positionIntroduceX',
          width: 700
        },
        {
          label: '职位状态',
          props: 'isDisable',
          options: ['有效', '禁用']
        }
        ],
        width: 300,
        handle: [{
          title: '查看',
          method: 'details',
          type: 'info'
        },
        {
          title: '编辑',
          method: 'editor',
          type: 'warning'
        },
        {
          title: '删除',
          method: 'del',
          type: 'danger'
        }
        ]
      }
    }
  },
  created () {
    this.queryData()
  },
  methods: {
    queryData (form = {}) {
      let page = this.page
      let reqdata = {}
      this.$api
        .post('/position/queryPositionList', {
          reqdata,
          page
        })
        .then((res) => {
          this.totalrecords = res.totalrecords
          this.tableList = res.list.map((item, index) => {
            item.serialNumber = (this.page - 1) * 10 + index + 1
            item.positionIntroduceX = item.positionIntroduce.length > 100 ? item.positionIntroduce.slice(0, 100) + '...' : item.positionIntroduce
            return item
          })
        })
    },
    add () {
      this.open()
      this.info = {}
      this.state = 0
    },
    getDetails (row) {
      let reqdata = {
        id: row.id
      }
      this.$api
        .post('/position/queryPositionDetail', {
          reqdata
        })
        .then((res) => {
          this.open()
          this.form = res.object
        })
    },
    details (row) {
      this.getDetails(row)
      this.state = 2
    },
    editor (row) {
      this.getDetails(row)
      this.state = 1
    },
    del (row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api
          .post('/position/deletePosition', {
            reqdata: {
              id: row.id
            }
          })
          .then((res) => {
            this.queryData()
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    determine () {
      let a
      this.$refs['form'].validate((valid) => {
        a = valid
      })
      if (!a) return
      let url = this.state ? '/position/updatePosition' : '/position/savePosition'
      let reqdata = this.form
      this.$api
        .post(url, {
          reqdata
        })
        .then((res) => {
          this.close()
          this.queryData()
        })
    },
    open () {
      this.dialogFormVisible = true
    },
    close () {
      this.dialogFormVisible = false
      this.form = {
        positionName: '',
        positionIntroduce: '',
        isDisable: 0
      }
    },
    callPage (val) {
      this.page = val
      this.queryData()
    }
  },
  watch: {
    dialogFormVisible: function (val, oldVla) {
      if (this.state === 2) {
        if (this.$refs['form'] !== undefined) {
          this.$refs['form'].resetFields()
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
    display: flex;
    justify-content: center;
    align-items: center;
}
</style>
