<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="33%"
    :before-close="handleClose"
     v-loading.fullscreen.lock="fullscreenLoading"
  >
    <el-form ref="form" :model="form" label-width="100px" :rules="rules">
      <el-form-item prop="account">
        <template slot="label" class="status"
          ><span>账&#8195;&emsp;号</span>：</template
        >
        <el-input placeholder="请输入" v-model.trim="form.account"></el-input>
      </el-form-item>
      <el-form-item prop="name">
        <template slot="label" class="status"
          ><span>姓&#8195;&emsp;名</span>：</template
        >
        <el-input placeholder="请输入" v-model.trim="form.name"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <template slot="label" class="status"
          ><span>密&#8195;&emsp;码</span>：</template
        >
        <el-input placeholder="请输入" v-model.trim="form.password"></el-input>
      </el-form-item>
      <el-form-item label>
        <template slot="label" class="status"
          ><span>状&#8195;&emsp;态</span>：</template
        >
        <el-radio-group v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button
        size="small"
        style="margin-left: 60px"
        type="primary"
        @click="addList('form')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import * as api from '@/api/api'
export default {
  data() {
    const validatePass = (rule, value, callback) => {
      if (value.length < 6 || value.length > 18) {
        callback(new Error('密码不能小于6位，大于18位'))
      } else {
        callback()
      }
    }
    return {
      fullscreenLoading:false,
      dialogVisible: false,
      radio: '',
      title: '',
      form: {
        account: '',
        name: '',
        password: '',
        status: 0,
        userId: ''
      },
      userString: {},
      rules: {
        account: [{ required: true, message: '请输入账号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      }
    }
  },
  methods: {
    handleClose() {
      this.dialogVisible = false
      this.$parent.search()
      this.form = {
        account: '',
        name: '',
        password: '',
        status: '',
        userId: ''
      }
      this.$refs['form'].resetFields()
    },
    openClose(val) {
      this.title = val.title
      if (val) {
        this.userString = val
        if (val.id) {
          this.detail()
        }
      }

      this.dialogVisible = true
    },
    /**
     * @method 用户确定
     * **/
    addList(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.fullscreenLoading = true
          let code = {
            account: this.form.account,
            name: this.form.name,
            password: this.form.password,
            status: this.form.status
          }
          this.userString.show
            ? this.addListData(code)
            : this.editListData(code)
        }
      })
    },
    /**
     * @method 详情
     * **/
    detail() {
      const code = {
        id: this.userString.id
      }
      api.GET('/user/detail', code).then(data => {
        if (data.code == 0) {
          this.form = data.data
        }
      })
    },
    /**
     * @method 新增
     * **/
    addListData(val) {
      api.POST('/user/add', val).then(data => {
        if (data.code == 0) {
          this.handleClose()
          this.$message.success(data.message)
          this.fullscreenLoading = false
        }
      }).catch(()=>{
        this.fullscreenLoading = false
      })
    },
    /**
     * @method 编辑
     * **/
    editListData(val) {
      val.userId = this.form.userId
      api.POST('/user/update', val).then(data => {
        if (data.code == 0) {
          this.handleClose()
          this.$message.success(data.message)
          this.fullscreenLoading = false
        }
      }).catch(()=>{
        this.fullscreenLoading = false
      })
    }
  }
}
</script>
<style lang="less" scoped>
.status {
  padding: 0 12px 0 0;
}
.status::before {
  content: '*';
  color: #f56c6c;
  margin-right: 4px;
}
</style>