

<template>
  <el-dialog class="client-data-dialog" title="用户新增" :visible.sync="visible" :before-close="handleClose">
    <el-form :model="ruleForm"  :rules="rules" ref="ruleForm"  label-width="80px" class="demo-ruleForm">
      <el-form-item label="登录名" prop="loginName">
        <el-col :span="20"><el-input v-model="ruleForm.loginName" placeholder="请输入英文或者英文后加数字"></el-input></el-col>
      </el-form-item>
      <el-form-item label="用户名" prop="userName">
        <el-col :span="20"><el-input v-model="ruleForm.userName" placeholder="请输入中文或者中文后加数字"></el-input></el-col>
      </el-form-item>
      <el-form-item label="手机号码" prop="userCell">
        <el-col :span="20"><el-input v-model.number="ruleForm.userCell"></el-input></el-col>
      </el-form-item>
    </el-form>

    <div slot="footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm('ruleForm')">新 增</el-button>
    </div>
  </el-dialog>
</template>

<script type="text/ecmascript-6">


  export default {
    name: 'AddUserManagementDialog',
    props: {
      dialogVisible: {
        type: Boolean,
        required: true
      },
    },
    data() {
      /*验证登录名*/
      var validateLoginName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入登录名'));
        }
        setTimeout(() => {
          const reg2=/^[a-zA-Z][a-zA-Z0-9]*$/g;
          if (!reg2.test(value)) {
            callback(new Error('登录名只支持英文或者英文后加数字'));
          } else {
            callback();
          }
        }, 1000);
      };
      /*验证用户名*/
      var validateUserName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入用户名'));
        }
        setTimeout(() => {
          const reg2=/^[\u2E80-\u9FFF][\u2E80-\u9FFF0-9]+$/;
          if (!reg2.test(value)) {
            callback(new Error('用户名只支持中文或者中文后加数字'));
          } else {
            callback();
          }
        }, 1000);
      };
      /*验证手机号码*/
      var validateUserCell = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('请输入手机号码'));
        }
        setTimeout(() => {
          const reg=/^1[3|4|5|6|7|8|9][0-9]\d{8}$/;
          if (!reg.test(value)) {
            callback(new Error('手机格式不正确'));
          } else {
            callback();
          }
        }, 1000);
      };
      return {
        ui: {
          submitLoading: false
        },
        visible: this.$props.dialogVisible,

        ruleForm: {
          loginName: '',
          userName: '',
          userCell:'',
        },
        rules: {
          loginName: [
            { validator: validateLoginName, trigger: 'blur' }
          ],
          userName: [
            { validator: validateUserName, trigger: 'blur' }
          ],
          userCell: [
            { validator: validateUserCell, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      /** 提交点击 */
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$$post('/user/insert', {
              loginName: this.ruleForm.loginName,
              userName: this.ruleForm.userName,
              userCell:this.ruleForm.userCell,
              bindStatus: 'ENABLED',
            })
              .then(res => {
                if (res.data.code != 200) {
                  this.$message.error(res.data.message);
                  return
                }
                this.$message({message: '新增成功', type: 'success'});
                this.handleClose();
                window.location.reload();
              })
              .catch(err => {
                alert("error!")
              })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },

      /** 关闭处理 */
      handleClose() {
        this.visible = false;
        setTimeout(() => {
          this.$emit('update:dialogVisible', false)
        }, 300);
      }
    }
  }
</script>
