<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog
      v-dialogDrag
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="45%"
      append-to-body
    >
      <el-form
        ref="formRef"
        v-loading="formLoading"
        :model="formData"
        :rules="formRules"
        label-width="100px"
      >
        <el-form-item label="应用名" prop="appName">
          <el-input v-model="formData.appName" placeholder="请输入应用名" />
        </el-form-item>
        <el-form-item label="服务名" prop="serviceName">
          <el-input v-model="formData.serviceName" placeholder="请输入服务名" />
        </el-form-item>
        <el-form-item label="模块名" prop="moduleName">
          <el-input v-model="formData.moduleName" placeholder="请输入模块名" />
        </el-form-item>
        <el-form-item label="是否启动" prop="isEnabled">
          <el-radio-group v-model="formData.isEnabled">
            <el-radio
              v-for="dict in statusDictDatas"
              :key="parseInt(dict.value)"
              :label="parseInt(dict.value)"
              >{{ dict.label }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" prop="memo">
          <el-input
            v-model="formData.memo"
            type="textarea"
            placeholder="请输入备注"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="formLoading" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as SystemDockingMasterApi from "@/api/system/dev/dockingMaster";
import { getDictDatas, DICT_TYPE } from "@/utils/dict";
export default {
  name: "SystemDockingMasterForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        appName: undefined,
        serviceName: undefined,
        moduleName: undefined,
        memo: undefined,
        isEnabled: 1,
      },
      // 数据字典
      statusDictDatas: getDictDatas(DICT_TYPE.MES_COMMON_STATUS),
      // 表单校验
      formRules: {
        appName: [
          { required: true, message: "应用名不能为空", trigger: "change" },
        ],
        serviceName: [
          { required: true, message: "服务名不能为空", trigger: "blur" },
        ],
        moduleName: [
          { required: true, message: "模块名不能为空", trigger: "blur" },
        ],
        isEnabled: [
          { required: true, message: "状态不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await SystemDockingMasterApi.getSystemDockingMaster(id);
          this.formData = res.data;
          this.title = "修改配置";
        } finally {
          this.formLoading = false;
        }
      }
      this.title = "新增配置";
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        delete data.detailDOS;
        // 修改的提交
        if (data.id) {
          await SystemDockingMasterApi.updateSystemDockingMaster(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit("success");
          return;
        }
        // 添加的提交
        await SystemDockingMasterApi.createSystemDockingMaster(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit("success");
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        appName: undefined,
        serviceName: undefined,
        moduleName: undefined,
        memo: undefined,
        isEnabled: 1,
      };
      this.resetForm("formRef");
    },
  },
};
</script>
