<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="apiRequest">
      <div class="d-header clearfix">
        <div class="fl">
          <i class="el-icon-info"/>
          {{ dataId ? $t1('查看API请求定义') : $t1('新增API请求定义') }}
        </div>
        <div class="fr">
          <el-button type="warning" class="button-sty" icon="el-icon-copy-document" @click="copy" v-if="!!dataId">
            {{ $t1('复制') }}
          </el-button>
          <el-button type="primary" plain class="button-sty" @click="$baseReload()" icon="el-icon-refresh-right">
            {{ $t1('重置') }}
          </el-button>
          <el-button type="primary" class="button-sty" icon="el-icon-check" @click="saveData">{{ $t1('保存') }}
          </el-button>
        </div>
      </div>
      <div class="d-cont">
        <div class="d-item">
          <div class="title first"><b>{{ $t1('基本信息') }}</b></div>
          <table class="table-detail">
            <tbody>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('API请求名称') }}
              </th>
              <td colspan="3">
                <el-form-item prop="apiReqName" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="apiRequest.apiReqName" clearable/>
                </el-form-item>
              </td>
              <th>{{ $t1('API请求编码') }}</th>
              <td>{{ apiRequest.apiReqCode }}</td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('是否启用') }}
              </th>
              <td>
                <el-form-item prop="enabled" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-radio-group v-model="apiRequest.enabled">
                    <el-radio :label="true">{{ $t1('启用') }}</el-radio>
                    <el-radio :label="false">{{ $t1('禁用') }}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('请求URL') }}
              </th>
              <td colspan="7">
                <el-form-item prop="url" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="apiRequest.url" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('备注') }}</th>
              <td colspan="7">
                <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" size="small"
                          v-model="apiRequest.remark"
                          clearable></el-input>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('脚本') }}
                <scriptDescriptionButton path="static/readme/ApiRequest.txt"></scriptDescriptionButton>
                <scriptTestButton :script.sync="apiRequest.script"></scriptTestButton>
              </th>
              <td colspan="7">
                <el-form-item prop="script" :rules="[{ required: true, trigger: 'blur' }]">
                  <code-editor mode="java" :readonly="!1" v-model="apiRequest.script"
                               v-if="showCodeEditor"></code-editor>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('创建人') }}</th>
              <td>{{ apiRequest.createBy }}</td>
              <th>{{ $t1('创建时间') }}</th>
              <td>{{ apiRequest.createDate }}</td>
              <th>{{ $t1('更新人') }}</th>
              <td>{{ apiRequest.modifyBy }}</td>
              <th>{{ $t1('更新时间') }}</th>
              <td>{{ apiRequest.modifyDate }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>

export default {
  name: 'user_apiRequestEdit',
  props: {
    _dataId: [String, Number],
    copyId: [String, Number]
  },
  components: {},
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      apiRequest: {
        enabled: true,
        script: null
      },
      showCodeEditor: false
    };
  },
  created() {
    if (this._dataId && !isNaN(this._dataId)) this.dataId = this._dataId;
  },
  mounted() {
    this.getData();
    this.getDataCopy();
  },
  methods: {
    getData() {
      if (this.copyId) {
        return;
      }
      if (this.dataId && !isNaN(this.dataId)) {
        this.isEdit = true;
        this.$commonHttp({
          url: USER_PREFIX + `/api_request/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.apiRequest = res.objx || {};
            this.showCodeEditor = true;
          }
        });
      } else {
        this.showCodeEditor = true;
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
            var url = USER_PREFIX + (this.isEdit ? `/api_request/update` : `/api_request/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.apiRequest,
              isLoading: true,
              success: res => {
                this.$message({
                  message: res.content,
                  type: 'success',
                  duration: 500,
                  onClose: t => {
                    if (this.isEdit) {
                      this.$baseReload();
                    } else {
                      this.$baseReload({
                        updateParam: {
                          _dataId: res.objx,
                          copyId: null
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    },
    copy() {
      this.$baseReload({
        updateParam: {
          copyId: this.dataId,
          _dataId: null
        }
      });
    },
    getDataCopy() {
      if (this.copyId) {
        this.$commonHttp({
          url: USER_PREFIX + `/api_request/get`,
          method: `post`,
          data: {
            id: this.copyId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            let obj = res.objx || {};
            obj = this.handleCopyField(obj);
            this.apiRequest = obj;
            this.showCodeEditor = true;
          }
        });
      }
    },
    handleCopyField(obj) {
      if (obj instanceof Array) {
        obj.forEach(item => {
          item.id = null;
          item.status = null;
          item.createDate = null;
          item.modifyDate = null;
          item.createBy = null;
          item.modifyBy = null;
        })
      } else {
        obj.id = null;
        obj.sn = null;
        obj.apiReqCode = null;
        obj.apiReqName = null;
        obj.status = null;
        obj.createDate = null;
        obj.modifyDate = null;
        obj.createBy = null;
        obj.modifyBy = null;
      }
      return obj;
    },
  }
};
</script>
