<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="600px">
    <el-form
      :rules="dataRule" 
      :model="dataForm" 
      ref="dataForm" 
      @keyup.enter.native="dataFormSubmit()" 
      label-width="80px" 
      size="mini"
      style="margin-left: 20px;width: 500px">
      <el-form-item label="岗位名称" prop="jobName">
        <el-input v-model="dataForm.jobName"></el-input>
      </el-form-item>
      <el-form-item label="岗位代码" prop="jobCode">
        <el-input v-model="dataForm.jobCode"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="visible = false">关闭</el-button>
      <el-button size="mini" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: undefined,
          jobName: '',
          jobCode: ''
        },
        dataRule: {
          jobName: [
            { required: true, message: '岗位名称不能为空', trigger: 'blur' },
            { min: 1, max: 10, message: '长度在 1 到 10 个字符之间', trigger: ['blur', 'change'] }
          ],
          jobCode: [
            { required: true, message: '岗位代码不能为空', trigger: 'blur' },
            { type: 'string' , message: '以GW开头，后接三位数字', pattern: /^GW[0-9]{3}$/ }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        this.dataForm.id = id || undefined
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/sys/job/info/${this.dataForm.id}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            console.log(data);
            if (data && data.code === 0) {
              this.dataForm.jobName = data.job.jobName
              this.dataForm.jobCode = data.job.jobCode
            }
          })
        }
        // this.$http({
        //   url: this.$http.adornUrl('/sys/job/select'),
        //   method: 'get',
        //   params: this.$http.adornParams()
        // }).then(({data}) => {
        //   this.roleList = data && data.code === 0 ? data.list : []
        // }).then(() => {
        //   this.visible = true
        //   this.$nextTick(() => {
        //     this.$refs['dataForm'].resetFields()
        //   })
        // }).then(() => {
        //   if (this.dataForm.id) {
        //     this.$http({
        //       url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.id}`),
        //       method: 'get',
        //       params: this.$http.adornParams()
        //     }).then(({data}) => {
        //       if (data && data.code === 0) {
        //         this.dataForm.userName = data.user.username
        //         this.dataForm.salt = data.user.salt
        //         this.dataForm.email = data.user.email
        //         this.dataForm.mobile = data.user.mobile
        //         this.dataForm.roleIdList = data.user.roleIdList
        //         this.dataForm.status = data.user.status
        //       }
        //     })
        //   }
        // })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/sys/job/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'jobCode': this.dataForm.jobCode,
                'jobName': this.dataForm.jobName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.visible = false
                this.$emit('refreshDataList')
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  // onClose: () => {
                  //   this.visible = false
                  //   this.$emit('refreshDataList')
                  // }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
