<template>
  <el-dialog
    title="用户设置"
    width="50%"
    @close="$emit('close')"
    :visible="show"
  >
    <el-form
      :model="form"
      ref="useredit"
      :rules="rule"
      :label-width="formLabelWidth"
    >
      <div class="formcon">
        <div class="formtop">
          <div class="left">
            <el-form-item label="账号：" prop="userName">
              <el-input
                :disabled="true"
                show-word-limit
                :maxlength="INPUTLENGTH"
                v-model="form.userName"
              ></el-input>
            </el-form-item>

            <el-form-item label="用户昵称：" prop="nickName">
              <el-input
                :maxlength="INPUTLENGTH"
                show-word-limit
                v-model="form.nickName"
              ></el-input>
            </el-form-item>

            <el-form-item label="单位名称：" prop="unit">
              <el-input
                :disabled="true"
                :maxlength="INPUTLENGTH * 5"
                v-model="form.unit"
              ></el-input>
            </el-form-item>

            <el-form-item label="联系电话：" prop="phone">
              <el-input
                :maxlength="INPUTLENGTH"
                show-word-limit
                v-model="form.phone"
              ></el-input>
            </el-form-item>
          </div>
          <div class="right">
            <el-form-item label="用户角色：" prop="roleName">
              <el-input
                :disabled="true"
                :maxlength="INPUTLENGTH"
                show-word-limit
                v-model="form.roleName"
              ></el-input>
            </el-form-item>

            <el-form-item label="真实姓名：" prop="realName">
              <el-input
                :disabled="true"
                :maxlength="INPUTLENGTH"
                show-word-limit
                v-model="form.realName"
              ></el-input>
            </el-form-item>

            <el-form-item label="通讯地址：" prop="address">
              <el-input
                v-model="form.address"
                show-word-limit
                :maxlength="INPUTLENGTH * 5"
              ></el-input>
            </el-form-item>

            <el-form-item label="性别：" prop="sex">
              <el-radio-group v-model="form.sex">
                <el-radio :label="0">男</el-radio>
                <el-radio :label="1">女</el-radio>
              </el-radio-group>
            </el-form-item>
          </div>
        </div>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('close')">取 消</el-button>
      <el-button type="primary" @click="audit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { saveSys } from "@/configue/api";

let rule = {
  userName: [{ required: true, message: "请输入账号", trigger: "blur" }],
  nickName: [{ required: true, message: "请输入用户昵称", trigger: "blur" }],
  unit: [{ required: true, message: "请输入单位名称", trigger: "blur" }],
  roleId: [{ required: true, message: "请选择用户角色", trigger: "change" }],
  realName: [{ required: true, message: "请输入真实姓名", trigger: "blur" }],
};

export default {
  props: {
    id: {
      type: String || Number,
    },
    show: {
      type: Boolean,
    },
    form: Object,
  },
  data() {
    return {
      formLabelWidth: "120px",
      rule,
    };
  },
  methods: {
    audit() {
      this.$refs.useredit.validate(async (valid) => {
        if (valid) {
          let params = {
            address: "",
            nickName: "",
            phone: "",
            realName: "",
            roleName: "",
            sex: 0,
            unit: "",
            userName: "",
            ...this.form,
          };
          saveSys("user", params, () => {
            this.$alert("编辑成功", "提示", {
              confirmButtonText: "确定",
              callback: () => {
                this.$emit("close");
              },
            });
          });
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.formcon {
  width: 100%;
  .formtop,
  .formbom {
    width: 100%;
    display: flex;
    position: relative;
    > div {
      width: 100%;
      &:first-of-type {
        margin-right: 2%;
      }
      > div {
        display: flex;
        > div {
          flex: 1;
        }
        & /deep/ .el-form-item__content {
          flex: 2;
          margin-left: 0 !important;
        }
      }
    }
  }
  .formbom {
    align-items: flex-end;
  }
}
</style>
