<template>
  <ed-drawer ref="BasicDrawer" :title="title"
          :visible="visible"
          ok-text="保存并关闭"
          @close="closeDialog"
          @ok="saveMock">
    <el-form :key="dataId" ref="form" :model="form"
             class="demo-form-inline">
      <el-form-item label="是否启用Mock"
                    prop="is_mock">
        <el-switch v-model="form.is_mock"></el-switch>
      </el-form-item>
      <el-form-item label="接口请求数据"
                    prop="requestData">
        <ed-code-editor :value="form.requestData"
                    language="javascript" @input="changeTextReq"></ed-code-editor>
      </el-form-item>
      <el-form-item
        label="接口返回数据" prop="responseData">
        <ed-code-editor ref="codeEditor" :value="form.responseData"
                    language="javascript" @input="changeTextResp"></ed-code-editor>
      </el-form-item>
    </el-form>
  </ed-drawer>
</template>

<script>
import {getOneData, updateData, insertData} from "../../../../api/easyData";

export default {
  name: "InterfaceMock",
  data() {
    return {
      dataId: '',
      visible: false,
      title: '',
      row: {},
      form: {requestData: '', responseData: '', is_mock: false},
    }
  },
  methods: {
    show(row, title) {
      this.row = row
      this.title = title
      this.visible = true
      this.initRequestData()
    },
    changeTextResp(val) {
      this.form.responseData = val;
    },
    changeTextReq(val) {
      this.form.requestData = val;
    },
    initRequestData() {
      let param = {
        dataSourceName: 'interface_params',
        wheres: [{
          fieldName: 'interface_id',
          fieldValue: this.row.id,
          operator: '='
        }]
      }
      let interfaceParams = {}
      getOneData(param).then((res) => {
        if (res.success) {
          if (res.data) {
            let params = res.data.dataInfoArray
            params.forEach((p) => {
              interfaceParams[p.fieldName] = ''
            })
            this.initMockData(JSON.stringify(interfaceParams))
          } else {
            this.initMockData()
          }
        } else {
          this.$message.warning(res.msg);
        }
      })
    },
    initMockData(requestData) {
      if (this.row.id) {
        let param = {
          dataSourceName: 'interface_mock',
          wheres: [{
            fieldName: 'interface_id',
            fieldValue: this.row.id,
            operator: '='
          }]
        }
        getOneData(param).then((res) => {
          if (res.success) {
            if (res.data) {
              let result = res.data.keyInfoObject
              this.dataId = res.data.id
              this.form = Object.assign(result, {})
              if (requestData) {
                this.form.requestData = requestData
              }
            } else {
              if (requestData) {
                this.form.requestData = requestData
              }
            }
          } else {
            this.$message.warning(res.msg);
          }
        })
      }
    },
    getFormData() {
      //查询参数
      let param = {}
      param.dataSourceName = 'interface_mock'
      param.key = {
        interface_id: this.row.id,
        is_mock: this.form.is_mock,
        responseData: this.form.responseData
      }
      if (this.dataId) {
        param.id = this.dataId
      }
      return param;
    },
    closeDialog() {
      this.visible = false
    },
    saveMock() {
      if (this.form.is_mock) {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            const loading = this.$loading({
              lock: true,
              text: 'Loading',
              spinner: 'el-icon-loading',
            });
            const that = this;
            let formData = this.getFormData()
            if (this.dataId) {
              updateData(formData).then((res) => {
                if (res.success) {
                  that.$message.success(res.msg);
                  this.visible = false
                } else {
                  that.$message.warning(res.msg);
                }
              }).finally(() => {
                loading.close();
              });
            } else {
              insertData(formData).then((res) => {
                if (res.success) {
                  that.$message.success(res.msg);
                  this.visible = false
                } else {
                  that.$message.warning(res.msg);
                }
              }).finally(() => {
                loading.close();
              });
            }

          }
        })
      } else {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
        });
        const that = this;
        let formData = this.getFormData()
        if (formData.id) {
          updateData(formData).then((res) => {
            if (res.success) {
              that.$message.success(res.msg);
              this.visible = false
            } else {
              that.$message.warning(res.msg);
            }
          }).finally(() => {
            loading.close();
          });
        } else {
          insertData(formData).then((res) => {
            if (res.success) {
              that.$message.success(res.msg);
              this.visible = false
            } else {
              that.$message.warning(res.msg);
            }
          }).finally(() => {
            loading.close();
          });
        }

      }
    }
  }
}
</script>

<style scoped>
</style>
