<template>
  <div class="laadmin-form">
    <el-form ref="ruleForm" :model="ruleForm" size="small" label-width="180px">
      <el-form-item label="用户名" prop="username"  :rules="[{
                    required: true,validator:validateName, trigger: 'blur'
                  }]">
        <el-input :disabled="ruleForm.disabled?true:false" maxlength="20" clearable v-model="ruleForm.username" size="small" placeholder="请输入用户名"/>
      </el-form-item>
      <el-form-item label="姓名" prop="realname" :rules="[{
                    required: true, message: '用户名不能为空', trigger: 'blur'
                  }]">
        <el-input :disabled="ruleForm.disabled?true:false" maxlength="20" clearable v-model="ruleForm.realname" size="small" placeholder="请输入姓名"/>
      </el-form-item>
      <el-form-item label="角色"  prop="authGroup" :rules="[{
                    required: true, message: '角色不能为空', trigger: 'change'
                  }]">
        <el-select :disabled="ruleForm.disabled?true:false" filterable multiple clearable v-model="ruleForm.authGroup" size="small" placeholder="-请选择-">
          <el-option v-for="(item,index) in roleList" :key="index" :label="item.name" :value="item.id+''"/>
        </el-select>
      </el-form-item>
      <el-form-item label="河长等级"  prop="level" :rules="[{
                    required: true, message: '河长等级不能为空', trigger: 'change'
                  }]">
        <el-select :disabled="ruleForm.disabled?true:false" filterable clearable v-model="ruleForm.level" size="small" placeholder="-请选择-">
          <el-option v-for="(value,key,index) in levelList" :key="index" :label="value" :value="parseInt(key)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="性别"  prop="gender">
        <el-select :disabled="ruleForm.disabled?true:false" filterable clearable v-model="ruleForm.gender" size="small" placeholder="-请选择-">
          <el-option v-for="(item,index) in sexList" :key="index" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="职务" prop="duty">
        <el-input :disabled="ruleForm.disabled?true:false" maxlength="20" clearable v-model="ruleForm.duty" size="small" placeholder="请输入职务"/>
      </el-form-item>
      <el-form-item label="手机" prop="mobile" :rules="[
                    {validator: validatePhone, trigger: 'blur'}
                    ]">
        <el-input :disabled="ruleForm.disabled?true:false" maxlength="20" clearable v-model="ruleForm.mobile" size="small" placeholder="请输入手机"/>
      </el-form-item>
      <el-form-item label="邮箱" prop="email" :rules="[
                    {validator: validateEmail, trigger: 'blur'}
                    ]">
        <el-input :disabled="ruleForm.disabled?true:false" maxlength="50" clearable v-model="ruleForm.email" size="small" placeholder="请输入邮箱"/>
      </el-form-item>
      <el-form-item label="区划" prop="driverArea" :rules="[{
                    required: true, message: '区划不能为空', trigger: 'change'
                  }]">
        <driverAreaSelect :clearValidate="clearValidate" :disabled="ruleForm.disabled?true:false" v-model="ruleForm.driverArea"></driverAreaSelect>
      </el-form-item>
      <el-form-item label="备注" prop="note" :rules="[
        {min: 0, max: 100, message: '最多100 个字符', trigger: 'blur'}]">
        <el-input :disabled="ruleForm.disabled?true:false" show-word-limit maxlength="100" clearable type="textarea" rows="5" v-model="ruleForm.note" size="small" placeholder="请输入备注"/>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button v-if="!ruleForm.disabled" icon="el-icon-document-add" :loading="addBtnLoading" type="primary" size="medium" @click="submitForm('ruleForm')">提交
      </el-button>
      <el-button icon="el-icon-back" v-if="ruleForm.id" style="margin-left: 15px" size="medium"
                 @click="onBack('ruleForm')">返回
      </el-button>
    </div>
  </div>
</template>

<script>
  import lsadminApi from '@/api/system/lsadmin'
  import driverAreaSelect from '@/components/driverAreaSelect'

  export default {
    name: "laadminForm",

    data() {
      var phoneReg = /^1\d{10}$/
      var validatePhone = (rule, value, callback) => {
        if (value) {
          if (!phoneReg.test(value)) {
            callback(new Error('请输入正确手机号码'));
          } else {
            callback();
          }
        } else {
          callback();
        }
      };
      var emailReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
      var validateEmail = (rule, value, callback) => {
        if (value) {
          if (!emailReg.test(value)) {
            callback(new Error('请输入正确邮箱'));
          } else {
            callback();
          }
        } else {
          callback();
        }
      };
      var nameReg = /^[0-9a-zA-Z]+$/
      var validateName = (rule, value, callback) => {
        if (value) {
          if (!nameReg.test(value)) {
            callback(new Error('只能输入数字及字母'));
          } else {
            callback();
          }
        } else {
          callback(new Error('请输入用户名'));
        }
      };
      return {
        validateName:validateName,
        validatePhone:validatePhone,
        validateEmail:validateEmail,
        addBtnLoading: false,
        sexList:[
          {id:1,name:'男'},
          {id:2,name:'女'},
          {id:3,name:'保密'}
        ]
      }
    },
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {};
        }
      },
      levelList: {
        type: Object,
        default() {
          return {};
        }
      },
      roleList: {
        type: Array,
        default() {
          return [];
        }
      },
    },
    created() {
    },
    mounted() {
    },
    components: {driverAreaSelect},
    methods: {
      clearValidate(){
        this.$refs.ruleForm.clearValidate('driverArea')
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method = 'lsadminAdd'
            if (this.ruleForm.id) {
              method = 'lsadminEdit'
            }
            let ruleForm=JSON.parse(JSON.stringify(this.ruleForm))
            if(ruleForm.authGroup&&ruleForm.authGroup.length>0){
              ruleForm.authGroup=ruleForm.authGroup.join(',')
            }else{
              ruleForm.authGroup=''
            }
            this.addBtnLoading = true
            lsadminApi[method](ruleForm).then(res => {
              this.addBtnLoading = false
              this.$refs[formName].resetFields()
              this.$message({
                type: 'success',
                message: this.ruleForm.id ? '修改成功' : '添加成功'
              })
              if (this.ruleForm.id) {
                this.$emit('submit',2)
                this.$emit('onBack')
              }else{
                this.$emit('submit',1)
              }
            }).catch(e => {
              this.addBtnLoading = false
            })
          }
        })
      },
      onBack() {
        this.$refs['ruleForm'].resetFields()
        this.$emit('onBack')
      }
    }
  }
</script>

<style lang="less">
  .laadmin-form{

  }
</style>
