<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="pushSetting">
      <div class="d-header clearfix">
        <div class="fl">
          <i class="el-icon-info"/>
          {{ dataId ? $t1('查看推送数据设置') : $t1('新增推送数据设置') }}
        </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('推送数据类型') }}
              </th>
              <td>
                <el-form-item prop="dataType" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="pushSetting.dataType" clearable/>
                </el-form-item>
              </td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('推送组织编码') }}
              </th>
              <td>
                <el-form-item prop="pushCompanyCode" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="pushSetting.pushCompanyCode" clearable/>
                </el-form-item>
              </td>
              <th v-if="pushSetting.pushType==0">
                <em class="f-red">*</em>
                {{ $t1('调度任务编码') }}
              </th>
              <td v-if="pushSetting.pushType==0">
                <el-form-item prop="taskCode" :rules="[{ required: pushSetting.pushType==0, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="pushSetting.taskCode" clearable/>
                </el-form-item>
              </td>
              <th v-if="pushSetting.pushType==0">
                <em class="f-red">*</em>
                {{ $t1('Api请求编码') }}
              </th>
              <td v-if="pushSetting.pushType==0">
                <el-form-item prop="apiReqCode" :rules="[{ required: pushSetting.pushType==0, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="pushSetting.apiReqCode" clearable/>
                </el-form-item>
              </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="pushSetting.enabled">
                    <el-radio :label="true">{{ $t1('启用') }}</el-radio>
                    <el-radio :label="false">{{ $t1('禁用') }}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('推送类型') }}
              </th>
              <td colspan="3">
                <el-form-item prop="pushType" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-radio-group v-model="pushSetting.pushType" @change="changePushType">
                    <el-radio :label="0">{{ $t1('异步推送') }}</el-radio>
                    <el-radio :label="1">{{ $t1('同步调用') }}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('脚本备注') }}
              </th>
              <td colspan="7">
                <el-form-item prop="remark" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" size="small"
                            v-model="pushSetting.remark"
                            clearable></el-input>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('脚本') }}
                <scriptDescriptionButton path="static/readme/PushSetting.txt"></scriptDescriptionButton>
                <scriptTestButton :script.sync="pushSetting.script"></scriptTestButton>
              </th>
              <td colspan="7">
                <el-form-item prop="script" :rules="[{ required: true, trigger: 'blur' }]">
                  <code-editor mode="java" :readonly="!1" v-model="pushSetting.script"
                               v-if="showCodeEditor"></code-editor>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('创建人') }}</th>
              <td>{{ pushSetting.createBy }}</td>
              <th>{{ $t1('创建时间') }}</th>
              <td>{{ pushSetting.createDate }}</td>
              <th>{{ $t1('更新人') }}</th>
              <td>{{ pushSetting.modifyBy }}</td>
              <th>{{ $t1('更新时间') }}</th>
              <td>{{ pushSetting.modifyDate }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'report_pushSettingEdit',
  props: {
    _dataId: [String, Number],
    copyId: [String, Number]
  },
  components: {},
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      pushSetting: {
        enabled: true,
        pushType: 0,
        apiReqCode: null,
        taskCode: null,
        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 + `/push_setting/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.pushSetting = 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 ? `/push_setting/update` : `/push_setting/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.pushSetting,
              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
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    },
    changePushType(val) {
      if (val == 1) {
        this.pushSetting.apiReqCode = null;
        this.pushSetting.taskCode = null;
      }
    },
    copy() {
      this.$baseReload({
        updateParam: {
          copyId: this.dataId,
          _dataId: null
        }
      });
    },
    getDataCopy() {
      if (this.copyId) {
        this.$commonHttp({
          url: USER_PREFIX + `/push_setting/get`,
          method: `post`,
          data: {
            id: this.copyId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            let obj = res.objx || {};
            obj = this.handleCopyField(obj);
            this.pushSetting = 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.status = null;
        obj.createDate = null;
        obj.modifyDate = null;
        obj.createBy = null;
        obj.modifyBy = null;
      }
      return obj;
    },
  }
};

</script>
