<template>
  <el-form ref="form" :rules="rules" label-width="80px" :model="form">
    <el-form-item label="用户名" prop="account">
      <el-input
        v-model="form.account"
        placeholder="只能输入字母或数字，不可输入汉字"
        :disabled="isEdit"
      ></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="form.password"></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="name">
      <el-input v-model="form.name"></el-input>
    </el-form-item>
    <el-form-item label="所属产线" prop="proLineId">
      <el-select
        v-model="form.proLineId"
        placeholder="所属产线"
        style="width: 100%"
      >
        <el-option
          v-for="(item, i) in lineList"
          :key="i"
          :label="item.name"
          :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="角色" prop="role">
      <el-select v-model="form.role" placeholder="角色" style="width: 100%">
        <el-option
          v-for="(item, i) in roleList"
          :key="i"
          :label="item.dataName"
          :value="item.dataName"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="状态" prop="enabled" v-show="isEdit">
      <el-select v-model="form.enabled" placeholder="状态" style="width: 100%">
        <el-option
          v-for="(item, i) in stateList"
          :key="i"
          :label="item.dataName"
          :value="item.dataCode"
        ></el-option>
      </el-select>
    </el-form-item>
  </el-form>
</template>
<script>
import { addAccount, getAccount, accountUpdate } from '@/api/basicConfig'
export default {
  name: 'AccountAdd',
  data() {
    return {
      form: {
        id: '',
        account: '',
        password: '123456',
        name: '',
        proLineId: '',
        role: '',
        enabled: 1
      },
      isEdit: false, //是否是编辑
      lineList: JSON.parse(localStorage.getItem('publicList')).proLineVoList,
      roleList: JSON.parse(localStorage.getItem('publicList')).roleVoList,
      stateList: JSON.parse(localStorage.getItem('publicList')).enabledVoList,
      rules: {
        account: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 3, max: 18, message: '长度在 3 到 18 个字符', trigger: 'blur' }
        ],
        proLineId: [
          { required: true, message: '请选择所属产线', trigger: 'change' }
        ],
        role: [{ required: true, message: '请选择角色', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (id) {
        this.isEdit = true
        this.form.id = id
        getAccount({ id }).then((res) => {
          if (res.code === 0) {
            this.form.account = res.data.account
            this.form.password = res.data.password
            this.form.name = res.data.name
            this.form.proLineId = res.data.proLineId
            this.form.role = res.data.role
            this.form.enabled = res.data.enabled
          }
        })
      } else {
        this.isEdit = false
        this.form.id = ''
      }
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.account = this.form.account.replace(/[\W]/g, '')
          if (this.isEdit) {
            //编辑
            accountUpdate({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.$emit('successSubmit')
              }
            })
          } else {
            addAccount({ ...this.form }).then((res) => {
              if (res.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500
                })
                this.$emit('successSubmit')
              }
            })
          }
        } else {
          return false
        }
      })
    },
    formClear() {
      this.$refs.form.resetFields()
      this.isEdit = false
    }
  }
}
</script>
