<template>
  <div>
    <base-right-dialog
      ref="baseRightDialog"
      :footerShow="true"
      :dialogVisible.sync="dialogVisible"
      :title="dialogTitle + '角色'"
      :type="dialogType"
      @handleClose="handleDialogClose"
      @handleConfirmClick="handleConfirmClick"
      :size="'820px'"
      :submitShow="submitShow"
    >
      <div class="dialogList">
        <base-form
          ref="basicsForm"
          :formRow="formRow"
          :isFunBtn="false"
          :rules="basicsRules"
          class="dialog_form"
          :spanWidth="`120px`"
          :loading="vLoading"
          style="padding-bottom: 30px"
        >
        </base-form>
      </div>
    </base-right-dialog>
  </div>
</template>

<script>
import baseRightDialog from "@/components/base/baseRightDialog";
import baseForm from "@/components/base/baseNewForm";
import configData from "./configData";
import { authApi } from "@/api/apis/auth";
export default {
  components: {
    baseRightDialog,
    baseForm,
  },
  data() {
    return {
      dialogVisible: false,
      dialogTitle: "",
      dialogType: "",
      formRow: configData.formRowRoles,
      basicsRules: configData.rolesRules,
      vLoading: false,
      submitShow: true,
    };
  },
  methods: {
    openDialog(type, row) {
      this.dialogVisible = true;
      this.submitShow = true;
      // 新增
      if (type == "add") {
        this.dialogTitle = "新增";
        this.dialogType = "add";
        this.$nextTick(() => {
          this.$refs.basicsForm.getField("sorts", "1");
        });
      }
      // 编辑
      if (type == "edit") {
        this.dialogTitle = "编辑";
        this.dialogType = "edit";
        this.GetWarehouseGetById(row.id);
      }
      //查看
      if (type == "show") {
        this.dialogTitle = "查看";
        this.dialogType = "show";
        this.submitShow = false;
        this.GetWarehouseGetById(row.id);
      }
    },
    // 编辑 详情
    async GetWarehouseGetById(id) {
      let param = {
        id: id,
      };
      let res = await authApi("roleService", "role", "queryEntity", "", param);
      if (res.status == "200") {
        this.$nextTick(() => {
          this.$refs.basicsForm.incomingParameters(res.attribute);
        });
      }
    },
    // 弹窗关闭
    handleDialogClose() {
      this.$refs.basicsForm.resetFields();
      this.dialogVisible = false;
    },
    // 弹窗确认按钮
    handleConfirmClick() {
      this.$refs.basicsForm.$refs["ruleForm"].validate((valid) => {
        if (!valid) {
          return;
        } else {
          let params = {
            ...this.$refs.basicsForm.ruleForm,
          };
          if (this.dialogType == "add") {
            this.openLoading("submit");
            this.GetWarehouseSaveDto(params);
          }
          if (this.dialogType == "edit") {
            this.openLoading("submit");
            this.GetWarehouseUpdateDto(params);
          }
        }
      });
    },
    // 新增保存
    async GetWarehouseSaveDto(params) {
      let res = await authApi("roleService", "role", "addEntity", "", params);
      if (res.status == "200") {
        this.$nextTick(() => {
          this.handleDialogClose();
          this.$vmNews("新增成功", "success");
          this.$emit("resetTable");
        });
      }
    },
    // 编辑保存
    async GetWarehouseUpdateDto(params) {
      let res = await authApi(
        "roleService",
        "role",
        "updateEntity",
        "",
        params
      );
      if (res.status == "200") {
        this.$nextTick(() => {
          this.handleDialogClose();
          this.$vmNews("新增成功", "success");
          this.$emit("resetTable");
        });
      }
    },
  },
};
</script>

<style scoped>
::v-deep .el-form-item__content .el-checkbox-group {
  display: flex;
  flex-direction: column;
}
</style>