<template>
  <div>
    <vxe-modal
      :value="isShow"
      show-footer
      title="角色"
      show-confirm-button
      :width="600"
      :height="600"
      esc-closable
      @input="e => $emit('update:isShow',e)"
    >
      <el-form ref="elForm" :model="form" :rules="rules" label-position="top" label-width="100">
        <el-form-item label="姓名" prop="name" required>
          <el-input v-model="form.name" clearable />
        </el-form-item>
        <el-form-item label="用户名" prop="username" required>
          <el-input v-model="form.username" clearable />
        </el-form-item>
        <el-form-item label="密码" prop="password" required>
          <el-input v-model="form.password" type="password" show-password clearable />
        </el-form-item>
        <el-form-item label="是否关闭打卡" prop="attendance_closed">
          <el-switch v-model="form.attendance_closed" :active-value="1" :inactive-value="0" active-text="是" inactive-text="否" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" clearable />
        </el-form-item>
        <el-form-item label="部门" prop="department_id">
          <el-select v-model="form.department_id" clearable>
            <el-option v-for="item in departments" :key="item.id" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职位" prop="position">
          <el-input v-model="form.position" clearable />
        </el-form-item>
        <el-form-item label="排序" prop="sortnumber">
          <el-input-number v-model="form.sortnumber" controls-position="right" :precision="0" />
        </el-form-item>
      </el-form>

      <template #footer>
        <el-button type="primary" :loading="loading" @click="submit">确认</el-button>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
import { save } from '@/api/user'
export default {
  props: {
    isShow: {
      type: Boolean,
      default: false,
      required: true
    },
    departments: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      loading: false,
      form: {
        name: '',
        username: '',
        password: '',
        attendance_closed: 0,
        mobile: '',
        department_id: '',
        position: '',
        sortnumber: 0
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名' }
        ],
        username: [
          { required: true, message: '请输入用户名' }
        ],
        password: [
          { required: true, message: '请输入密码' },
          {
            validator: (rule, value, callback) => {
              const reg = new RegExp('(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[^a-zA-Z0-9]).{8,20}')
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('您的密码复杂度太低（密码中必须包含字母、数字、特殊字符）！'))
              }
            }
          }
        ],
        mobile: [
          {
            validator: (rule, value, callback) => {
              if (/^1[3456789]\d{9}$/.test(value)) {
                callback()
              } else {
                callback(new Error('手机号格式错误！'))
              }
            }
          }
        ]
      }
    }
  },
  computed: {},
  methods: {
    submit() {
      this.$refs['elForm'].validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            await save(this.form)
            this.$message.success('新增成功')
            this.$emit('refresh')
            this.$emit('update:isShow', false)
            this.loading = false
            this.$refs['elForm'].resetFields()
          } catch (err) {
            this.loading = false
          }
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
</style>
