<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="用户名"
                    prop="account">
        <el-input v-model="dataForm.account"
                  placeholder="登录帐号"></el-input>
      </el-form-item>
      <el-form-item label="密码"
                    prop="password"
                    :class="{ 'is-required': !dataForm.id }" v-if="!dataForm.id">
        <el-input v-model="dataForm.password"
                  type="password"
                  placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item label="姓名"
                    prop="name">
        <el-input v-model="dataForm.name"
                  placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="编号"
                    prop="code">
        <el-input v-model="dataForm.code"
                  placeholder="编号"></el-input>
      </el-form-item>
      <el-form-item label="年龄"
                    prop="code">
        <el-input v-model="dataForm.age"
                  placeholder="年龄"></el-input>
      </el-form-item>
      <el-form-item label="性别"
                    prop="sex">
        <el-select v-model="dataForm.sex"
                   placeholder="请选择">
          <el-option key=1
                     label="男"
                     value=1 />
          <el-option key=2
                     label="女"
                     value=2 />
        </el-select>
      </el-form-item>
      <el-form-item label="用户类型"
                    prop="type">
        <el-select v-model="dataForm.type"
                   placeholder="请选择">
          <el-option key=1
                     label="管理员"
                     value=1 />
          <el-option key=2
                     label="用户"
                     value=2 />
        </el-select>
      </el-form-item>
      <el-form-item label="邮箱"
                    prop="email">
        <el-input v-model="dataForm.email"
                  placeholder="邮箱"></el-input>
      </el-form-item>
      <el-form-item label="联系方式"
                    prop="phone">
        <el-input v-model="dataForm.mobile"
                  placeholder="联系方式"></el-input>
      </el-form-item>
      <el-form-item label="状态"
                    size="mini"
                    prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">禁用</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from '@/utils/validate'
export default {
  data () {
    var validatePassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    var validateComfirmPassword = (rule, value, callback) => {
      if (!this.dataForm.id && !/\S/.test(value)) {
        callback(new Error('确认密码不能为空'))
      } else if (this.dataForm.password !== value) {
        callback(new Error('确认密码与密码输入不一致'))
      } else {
        callback()
      }
    }
    var validateEmail = (rule, value, callback) => {
      if (!isEmail(value)) {
        callback(new Error('邮箱格式错误'))
      } else {
        callback()
      }
    }
    var validateMobile = (rule, value, callback) => {
      if (!isMobile(value)) {
        callback(new Error('手机号格式错误'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      roleList: [],
      dataForm: {
        id: 0,
        account: '',
        password: '',
        comfirmPassword: '',
        salt: '',
        name: '',
        code: '',
        sex: '',
        age: '',
        email: '',
        mobile: '',
        roleIdList: [],
        status: 1,
        type: '2'
      },
      dataRule: {
        account: [
          { required: true, message: '用户名不能为空', trigger: 'blur' }
        ],
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ],
        comfirmPassword: [
          { validator: validateComfirmPassword, trigger: 'blur' }
        ],
        email: [
          { required: true, message: '邮箱不能为空', trigger: 'blur' },
          { validator: validateEmail, trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        name: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      if (this.dataForm.id) {
        this.$http({
          url: this.$http.adornUrl(`/user/getuser/${this.dataForm.id}`),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({ data }) => {
          console.log(data)
          if (data && data.successful) {
            var form = data.resultValue
            this.dataForm.account = form.account
            this.dataForm.password = form.password
            this.dataForm.name = form.name
            this.dataForm.salt = form.salt
            this.dataForm.email = form.email
            this.dataForm.mobile = form.phone
            this.dataForm.status = form.status
            this.dataForm.type = form.type
            this.dataForm.sex = form.sex
            this.dataForm.code = form.code
            this.dataForm.age = form.age
          }
        })
      }
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        var changeData
        if (valid) {
          if (this.dataForm.id) {
            changeData = this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'account': this.dataForm.account,
              'password': this.dataForm.id ? undefined : this.dataForm.password,
              'email': this.dataForm.email,
              'phone': this.dataForm.mobile,
              'status': this.dataForm.status,
              'code': this.dataForm.code,
              'age': this.dataForm.age,
              'name': this.dataForm.name,
              'sex': this.dataForm.sex,
              'type': this.dataForm.type
            })
          } else {
            changeData = this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'account': this.dataForm.account,
              'password': this.dataForm.password,
              'email': this.dataForm.email,
              'phone': this.dataForm.mobile,
              'status': this.dataForm.status,
              'code': this.dataForm.code,
              'age': this.dataForm.age,
              'name': this.dataForm.name,
              'sex': this.dataForm.sex,
              'type': this.dataForm.type
            })
          }
          this.$http({
            url: this.$http.adornUrl('/user/saveorupdate'),
            method: 'post',
            data: changeData
          }).then(({ data }) => {
            if (data && data.successful) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.resultHint)
            }
          })
        }
      })
    },
    dataFormClose () {
      this.dataForm = {
        id: 0,
        account: '',
        password: '',
        comfirmPassword: '',
        salt: '',
        name: '',
        code: '',
        sex: '',
        age: '',
        email: '',
        mobile: '',
        status: 1,
        type: '2'
      }
      this.$refs['dataForm'].resetFields()
      console.log(this.dataForm)
    }
  }
}
</script>
