<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="500"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      ref="dialogForm"
      label-width="150px"
      label-position="left"
    >
      <el-form-item :label="$t('admin.accountName')" prop="AdminName">
        <el-input
          v-model="form.AdminName"
          clearable
          :placeholder="$t('admin.accountName')"
          style="width: 60%"
        ></el-input>
      </el-form-item>
      <el-form-item v-if="mode == 'add'" :label="$t('admin.password1')">
        <el-input
          v-model="form.Password1"
          clearable
          :placeholder="$t('admin.password1')"
          style="width: 60%"
          type="password"  show-password
        ></el-input>
      </el-form-item>
      <el-form-item v-if="mode == 'add'" :label="$t('admin.password2')">
        <el-input
          v-model="form.Password2"
          clearable
          :placeholder="$t('admin.password2')"
          style="width: 60%"
          type="password" show-password
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('admin.fullName')">
        <el-input
          v-model="form.AdminFullName"
          clearable
          :placeholder="$t('admin.fullName')"
          style="width: 60%"
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('admin.adminRoleQuery')" prop="ParentCode">
        <el-select v-model="form.AdminRoleCode" clearable style="width: 60%">
          <el-option
            v-for="item in adminRoleCodes"
            :key="item.Key"
            :label="item.Value"
            :value="item.Key"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">{{
        $t("dialogBox.cancelButtonText")
      }}</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >{{ this.$t("dialogBox.saveButtonText") }}</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ["success", "closed"],
  data() {
    return {
      mode: "add",
      titleMap: {
        add: this.$t("dialogBox.addTitle"),
        edit: this.$t("dialogBox.modifyTitle"),
      },
      visible: false,
      isSaveing: false,
      adminRoleCodes: [],
      //表单数据
      form: {
        AdminName: "",
        Password1: "",
        Password2: "",
        AdminFullName: "",
        AdminRoleCode: "",
        AdminCode: "",
      },
      //验证规则
      rules: {
        AdminName: [
          {
            required: true,
            message: this.$t("admin.accountName"),
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      this.initRoles();
      return this;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          var res = false;
          //新增或者修改
          if (this.mode === "add") {
            if (this.form.Password1 != this.form.Password2) {
              this.$alert(
                this.$t("admin.passwordVaild"),
                this.$t("dialogBox.confirmWarningTitle"),
                {
                  type: "error",
                }
              );
              this.isSaveing = false;
              return false;
            }
            res = await this.$API.adminManage.createAdmin.post(this.form);
          } else {
            res = await this.$API.adminManage.modifyAdmin.post(this.form);
          }
          this.isSaveing = false;
          if (res == true) {
            this.$emit("success");
            this.visible = false;
            this.$message.success(this.$t("response.operateSuccess"));
          } else {
            this.$alert(res.message, this.$t("dialogBox.confirmWarningTitle"), {
              type: "error",
            });
          }
        }
      });
    },
    //表单注入数据
    async setData(data) {
      var res = await this.$API.wing.post("FindAdminByCodeAsync", {
        AdminCode: data.AdminCode,
      });
      this.form.AdminCode = data.AdminCode;
      this.form.AdminName = res.AdminName;
      this.form.AdminFullName = res.FullName;
      if (res.RoleCodes != null && res.RoleCodes.length > 0) {
        this.form.AdminRoleCode = res.RoleCodes[0];
      }
      this.form.Password1 = res.SecretPassword;
      this.form.Password2 = res.SecretPassword;
    },
    async initRoles() {
      this.adminRoleCodes =
        await this.$API.adminManage.adminRoleSelectList.post();
    },
  },
};
</script>

<style>
</style>
