// 添加/修改账号
<template>
  <el-dialog
    :title="isEdit ? '编辑账号' : '添加账号'"
    width="900px"
    :visible.sync="visible"
    :destroy-on-close="false"
    :close-on-click-modal="false"
  >
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-position="left"
      label-width="110px"
    >
      <el-form-item prop="adminId" style="margin-bottom: 0">&nbsp;</el-form-item>
      <div class="wrap-content">
        <div class="wrap-content__item" style="width: 100%">
          <el-form-item label="部门" prop="deptId" required>
            <el-select
              v-model="form.deptId"
              filterable
              clearable
              placeholder="请选择部门"
              style="width: 100%"
            >
              <el-option
                v-for="item in deptList"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptId"
              />
            </el-select>
          </el-form-item>
        </div>
        <div class="wrap-content__item">
          <el-form-item
            label="账号名称"
            prop="username"
          >
            <el-input
              v-model.trim="form.username"
              :disabled="isEdit"
              placeholder="请输入账号名称"
            />
          </el-form-item>
        </div>
        <div class="wrap-content__item">
          <el-form-item
            label="账号密码"
            :prop="isEdit ? '' : 'password'"
          >
            <el-input
              v-model.trim="form.password"
              placeholder="请输入账号密码"
              show-password
            />
          </el-form-item>
        </div>
        <div class="wrap-content__item">
          <el-form-item
            label="管理员姓名"
            prop="manageName"
          >
            <el-input
              v-model.trim="form.manageName"
              maxlength="20"
              show-word-limit
              placeholder="请输入管理员姓名"
            />
          </el-form-item>
        </div>
        <div class="wrap-content__item">
          <el-form-item
            label="管理员手机号"
            prop="managePhone"
          >
            <el-input
              v-model.trim="form.managePhone"
              maxlength="11"
              show-word-limit
              placeholder="请输入管理员手机号"
            />
          </el-form-item>
        </div>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false"> 取消 </el-button>
      <el-button type="primary" :loading="submitLoading" @click="submit">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

import { authEdit } from '@/api/authAccount'

export default {
  props: {
    deptList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      form: {
        adminId: null,
        deptId: null,
        username: null,
        password: null,
        manageName: null,
        managePhone: null
      },
      rules: {
        deptId: [{ required: true, message: '请选择部门', trigger: 'change' }],
        username: [{ required: true, message: '请输入账号名称', trigger: 'change' }],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern:
              /^(((?=.*\d)(?=.*[a-z])(?=.*[A-Z]))|((?=.*\d)(?=.*[a-z])(?=.*[~!@#$%^&*]))|((?=.*\d)(?=.*[A-Z])(?=.*[~!@#$%^&*]))|((?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*]))|((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[~!@#$%^&*]))).{10,32}$/,
            message:
              '必须大小写字母、数字、特殊字符,4种组合中至少满足3种，且密码长度在10-32位之间'
          }
        ],
        managePhone: [{ required: true, message: '请输入管理员手机号', trigger: 'change' }],
        manageName: [{ required: true, message: '请输入管理员姓名', trigger: 'change' }]
      },
      visible: false,
      submitLoading: false,
      isEdit: false
    }
  },
  created() {
    this.form_copy = JSON.parse(JSON.stringify(this.form))
  },
  methods: {
    open(obj) {
      this.visible = true
      this.submitLoading = false
      this.$nextTick(() => {
        this.$refs.form.resetFields()
        if (obj && Object.keys(obj).length > 0) {
          console.log(obj)
          this.form = obj
          this.isEdit = true
        } else {
          this.form = JSON.parse(JSON.stringify(this.form_copy))
          this.isEdit = false
        }
      })
    },
    submit(e) {
      e.preventDefault()
      this.$refs.form.validate(async(valid) => {
        if (!valid) {
          return false
        }
        if (this.submitLoading) {
          return
        }
        this.submitLoading = true
        try {
          await authEdit(this.form)
          this.visible = false
          this.$emit('success', this.isEdit)
        } catch (e) {
          console.info('add', e)
        } finally {
          this.submitLoading = false
        }
      })
    }
  }
}
</script>
