<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="65%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="业务系统名称" prop="businessSystem">
              <el-input v-model="formData.businessSystem" placeholder="请输入业务系统名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="内网地址" prop="internalUrl">
              <el-input v-model="formData.internalUrl" placeholder="请输入内网地址" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="外网代理路径" prop="externalPath">
              <el-input v-model="formData.externalPath" placeholder="请输入外网代理路径" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="代理日期" prop="proxyDate">
              <el-date-picker
                clearable
                v-model="formData.proxyDate"
                type="datetime"
                value-format="timestamp"
                placeholder="选择代理日期"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="代理说明">
              <Editor v-model="formData.description" :min-height="192"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="申请函附件">
              <FileUpload v-model="formData.attachment"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as ReverseProxyApi from '@/api/server/reverseproxy';
import FileUpload from '@/components/FileUpload';
import Editor from '@/components/Editor';
export default {
  name: "ReverseProxyForm",
  components: {
    FileUpload,
    Editor,
  },
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      formData: {
        id: undefined,
        websiteId: undefined,
        businessSystem: undefined,
        internalUrl: undefined,
        externalPath: undefined,
        description: undefined,
        proxyDate: undefined,
        attachment: undefined,
      },
      // 表单校验
      formRules: {
        websiteId: [{ required: true, message: '关联的网站ID不能为空', trigger: 'blur' }],
        businessSystem: [{ required: true, message: '业务系统名称不能为空', trigger: 'blur' }],
        internalUrl: [{ required: true, message: '内网地址不能为空', trigger: 'blur' }],
        externalPath: [{ required: true, message: '外网代理路径不能为空', trigger: 'blur' }],
        proxyDate: [{ required: true, message: '代理日期不能为空', trigger: 'blur' }],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(id) {
      this.dialogVisible = true;
      this.reset();
      // 从路由参数中获取 websiteId 并设置到表单中
      const websiteId = this.$route.params && this.$route.params.websiteId;
      if (websiteId) {
        this.formData.websiteId = websiteId;
      }
      // 修改时，设置数据
      if (id) {
        this.formLoading = true;
        try {
          const res = await ReverseProxyApi.getReverseProxy(id);
          this.formData = res.data;
          this.dialogTitle = "修改反向代理配置";
        } finally {
          this.formLoading = false;
        }
      } else {
        this.dialogTitle = "新增反向代理配置";
      }
    },
    /** 提交按钮 */
    async submitForm() {
      // 校验主表
      await this.$refs["formRef"].validate();
      this.formLoading = true;
      try {
        const data = this.formData;
        // 修改的提交
        if (data.id) {
          await ReverseProxyApi.updateReverseProxy(data);
          this.$modal.msgSuccess("修改成功");
          this.dialogVisible = false;
          this.$emit('success');
          return;
        }
        // 添加的提交
        await ReverseProxyApi.createReverseProxy(data);
        this.$modal.msgSuccess("新增成功");
        this.dialogVisible = false;
        this.$emit('success');
      } finally {
        this.formLoading = false;
      }
    },
    /** 表单重置 */
    reset() {
      this.formData = {
        id: undefined,
        websiteId: undefined,
        businessSystem: undefined,
        internalUrl: undefined,
        externalPath: undefined,
        description: undefined,
        proxyDate: undefined,
        attachment: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>

<style scoped>
.el-form-item__label {
  white-space: nowrap;
}
</style>
