<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="requestSetting">
      <div class="d-header clearfix">
        <div class="fl">
          <i class="el-icon-info"/>
          {{ dataId ? $t1('查看同步调用接口') : $t1('新增同步调用接口') }}
        </div>
        <div class="fr">
          <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('请求编码') }}
              </th>
              <td colspan="5">
                <el-form-item prop="reqCode" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="requestSetting.reqCode" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('请求名称') }}
              </th>
              <td colspan="5">
                <el-form-item prop="reqName" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="requestSetting.reqName" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('执行组织编码') }}
              </th>
              <td colspan="5">
                <el-form-item prop="exeCompanyCode" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="requestSetting.exeCompanyCode" clearable/>
                </el-form-item>
              </td>
              <td colspan="2"><span class="tips_1">{{ $t1('注：多个用逗号","隔开') }}</span></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="requestSetting.url" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                {{ $t1('请求头') }}
              </th>
              <td colspan="7">
                <el-form-item prop="header" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" size="small"
                            v-model="requestSetting.header"
                            clearable></el-input>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                {{ $t1('连接超时(ms)') }}
              </th>
              <td>
                <el-form-item prop="connectTimeout" :rules="[{ required: false, trigger: 'blur' }]">
                  <base-input-number v-model="requestSetting.connectTimeout" clearable/>
                </el-form-item>
              </td>
              <td colspan="2"><span class="tips_1">{{ $t1('注：不填默认10000毫秒') }}</span></td>
              <th>
                {{ $t1('读取超时(ms)') }}
              </th>
              <td>
                <el-form-item prop="readTimeout" :rules="[{ required: false, trigger: 'blur' }]">
                  <base-input-number v-model="requestSetting.readTimeout" clearable/>
                </el-form-item>
              </td>
              <td colspan="2"><span class="tips_1">{{ $t1('注：不填默认60000毫秒') }}</span></td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('是否启用') }}
              </th>
              <td>
                <el-form-item prop="enabled" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-radio-group v-model="requestSetting.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>
                {{ $t1('备注') }}
              </th>
              <td colspan="7">
                <el-form-item prop="remark" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" size="small"
                            v-model="requestSetting.remark"
                            clearable></el-input>
                </el-form-item>
              </td>
            </tr>
            <!--            <tr>
                          <th>
                            {{ $t1('前置脚本') }}
                            <scriptTestButton :script.sync="requestSetting.preScript"></scriptTestButton>
                          </th>
                          <td colspan="7">
                            <el-form-item prop="preScript" :rules="[{ required: false, trigger: 'blur' }]">
                              <code-editor mode="java" :readonly="!1" v-model="requestSetting.preScript"
                                           v-if="showCodeEditor"></code-editor>
                            </el-form-item>
                          </td>
                        </tr>-->
            <tr>
              <th>
                {{ $t1('后置脚本') }}
                <scriptTestButton :script.sync="requestSetting.postScript"></scriptTestButton>
              </th>
              <td colspan="7">
                <el-form-item prop="postScript" :rules="[{ required: false, trigger: 'blur' }]">
                  <code-editor mode="java" :readonly="!1" v-model="requestSetting.postScript"
                               v-if="showCodeEditor"></code-editor>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('创建人') }}</th>
              <td>{{ requestSetting.createBy }}</td>
              <th>{{ $t1('创建时间') }}</th>
              <td>{{ requestSetting.createDate }}</td>
              <th>{{ $t1('更新人') }}</th>
              <td>{{ requestSetting.modifyBy }}</td>
              <th>{{ $t1('更新时间') }}</th>
              <td>{{ requestSetting.modifyDate }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'request_setttingEdit',
  props: {
    _dataId: [String, Number],
    copyId: [String, Number]
  },
  components: {},
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      requestSetting: {
        enabled: true,
        connectTimeout: 10000,
        readTimeout: 60000,
        preScript: null,
        postScript: null,
        exeCompanyCode: null
      },
      showCodeEditor: false
    };
  },
  created() {
    if (this._dataId && !isNaN(this._dataId)) this.dataId = this._dataId;
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      if (this.copyId) {
        return;
      }
      if (this.dataId && !isNaN(this.dataId)) {
        this.isEdit = true;
        this.$commonHttp({
          url: USER_PREFIX + `/request_setting/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.requestSetting = 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 ? `/request_setting/update` : `/request_setting/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.requestSetting,
              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
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    }
  }
};

</script>
