<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="requestAsyncSetting">
      <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>
      <baseTabs>
        <baseTabPane :label="$t1('基本信息')">
          <template #default>
            <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="requestAsyncSetting.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="requestAsyncSetting.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="requestAsyncSetting.exeCompanyCode" clearable/>
                  </el-form-item>
                </td>
                <td colspan="2"><span class="tips_1">{{ $t1('注：多个用逗号","隔开') }}</span></td>
              </tr>

<!--              <tr>
                <th>
                  {{ $t1('连接超时(ms)') }}
                </th>
                <td>
                  <el-form-item prop="connectTimeout" :rules="[{ required: false, trigger: 'blur' }]">
                    <base-input-number v-model="requestAsyncSetting.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="requestAsyncSetting.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="requestAsyncSetting.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="requestAsyncSetting.remark"
                              clearable></el-input>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <th>
                  {{ $t1('参数1') }}
                </th>
                <td colspan="7">
                  <el-form-item prop="param1" :rules="[{ required: false, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="requestAsyncSetting.param1" clearable/>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <th>
                  {{ $t1('参数2') }}
                </th>
                <td colspan="7">
                  <el-form-item prop="param2" :rules="[{ required: false, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="requestAsyncSetting.param2" clearable/>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <th>
                  {{ $t1('参数3') }}
                </th>
                <td colspan="7">
                  <el-form-item prop="param3" :rules="[{ required: false, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="requestAsyncSetting.param3" clearable/>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <th>
                  {{ $t1('参数4') }}
                </th>
                <td colspan="7">
                  <el-form-item prop="param1" :rules="[{ required: false, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="requestAsyncSetting.param4" clearable/>
                  </el-form-item>
                </td>
              </tr>

              <tr>
                <th>{{ $t1('创建人') }}</th>
                <td>{{ requestAsyncSetting.createBy }}</td>
                <th>{{ $t1('创建时间') }}</th>
                <td>{{ requestAsyncSetting.createDate }}</td>
                <th>{{ $t1('更新人') }}</th>
                <td>{{ requestAsyncSetting.modifyBy }}</td>
                <th>{{ $t1('更新时间') }}</th>
                <td>{{ requestAsyncSetting.modifyDate }}</td>
              </tr>
              </tbody>
            </table>
          </template>
        </baseTabPane>
        <baseTabPane :label="$t1('数据组合脚本')">
          <template #default>
            <table class="table-detail">
              <tbody>
              <tr>
                <tr>
                  <th>
                    {{ $t1('脚本') }}
                    <scriptDescriptionButton path="static/readme/RequestAsyncSetting.txt"></scriptDescriptionButton>
                    <scriptTestButton :script.sync="requestAsyncSetting.script"></scriptTestButton>
                  </th>
                  <td colspan="7">
                    <el-form-item prop="postScript" :rules="[{ required: false, trigger: 'blur' }]">
                      <code-editor mode="java" :readonly="!1" v-model="requestAsyncSetting.script"
                                   v-if="showCodeEditor"></code-editor>
                    </el-form-item>
                  </td>
                </tr>
              </tr>
              </tbody>
            </table>
          </template>
        </baseTabPane>
        <baseTabPane :label="$t1('推送数据服务信息')">
          <template #default>
            <table class="table-detail">
              <tbody>
              <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="requestAsyncSetting.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="requestAsyncSetting.header"
                              clearable></el-input>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <th>
                  <em class="f-red">*</em>
                  {{ $t1('调度任务编码') }}
                </th>
                <td colspan="7">
                  <el-form-item prop="taskCode" :rules="[{ required: true, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="requestAsyncSetting.taskCode" clearable/>
                  </el-form-item>
                </td>
              </tr>
              </tbody>
            </table>
          </template>
        </baseTabPane>
      </baseTabs>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'request_async_settingEdit',
  props: {
    _dataId: [String, Number],
    copyId: [String, Number]
  },
  components: {},
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      requestAsyncSetting: {
        enabled: true,
        connectTimeout: 10000,
        readTimeout: 60000,
        script: 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_async_setting/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.requestAsyncSetting = 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_async_setting/update` : `/request_async_setting/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.requestAsyncSetting,
              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>
