<template>
  <el-dialog
    class="addchannelDialog"
    :title="title"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    width="470px"
    @close="handleclose"
  >
    <el-form
      label-position="left"
      ref="formInfo"
      label-width="100px"
      :rules=" title=='新增' ? rules : xgrules"
      :model="formdata"
    >
      <el-form-item label="用户名：" prop="userName">
        <el-input
          placeholder="请输入用户名"
          v-model="formdata.userName"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="角色：" prop="role">
        <el-select v-model="formdata.role" placeholder="请选择">
          <el-option
            v-for="item in roleoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="密码：" prop="password">
        <el-input
          placeholder="请输入密码"
          v-model="formdata.password"
          show-password
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { addUserApi, updateUserApi } from "@/utils/api/index";
export default {
  props: {
    title: String,
  },
  data() {
    return {
      isShow: false,
      roleoptions: [
        {
          value: 1,
          label: "管理员",
        },
        {
          value: 2,
          label: "用户",
        },
      ],
      formdata: {},
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        role: [{ required: true, message: "请选择角色", trigger: "blur" }],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 8, message: '请输入6-8位字符', trigger: 'blur' }
        ],
      },
      xgrules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        role: [{ required: true, message: "请选择角色", trigger: "blur" }]
      },
    };
  },
  methods: {
    //判断
    getdataform(val) {
      console.log(val);
      if (val == null) {
        return (this.formdata = {});
      }
      this.formdata = val;
    },
    // 保存确定操作
    submitForm() {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          if (this.title == "新增") {
            addUserApi(this.formdata)
              .then((res) => {
                if (res.code == 200) {
                  this.$message({
                    showClose: true,
                    message: "添加成功",
                    type: "success",
                  });
                  this.isShow = false;
                } else {
                  this.$message.error(res.msg);
                }
              })
              .catch((err) => {});
          } else {
            updateUserApi(this.formdata)
              .then((res) => {
                if (res.code == 200) {
                  this.$message.success("修改成功");
                  this.isShow = false;
                } else {
                  this.$message.error(res.msg);
                }
              })
              .catch((err) => {});
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    display() {
      this.isShow = true;
    },
    hide() {
      this.isShow = false;
    },
    handleclose() {
      this.$parent.deviceList();
    },
  },
  mounted() {},
};
</script>
<style lang="less">
.addchannelDialog {
  .el-form-item {
    .el-input,
    .el-input-number {
      width: 100%;
    }
  }
}
</style>
