<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="fileViewItem">
      <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>{{ fileViewItem.viewAreaCode }}</td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('查询目录名称') }}
              </th>
              <td>
                <el-form-item prop="itemName" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="fileViewItem.itemName" clearable/>
                </el-form-item>
              </td>
              <th>{{ $t1('查询目录编码') }}</th>
              <td>
                {{ fileViewItem.itemCode }}
              </td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('服务名') }}
              </th>
              <td>
                <el-form-item prop="serviceId" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-select v-model="fileViewItem.serviceId">
                    <el-option v-for="(serviceId,index) in serviceIds" :key="index" :value="serviceId">{{ serviceId }}
                    </el-option>
                  </el-select>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('备注') }}</th>
              <td colspan="5">
                <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" size="small"
                          v-model="fileViewItem.note"
                          clearable></el-input>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('脚本') }}
                <!--                  <scriptDescriptionButton path="static/readme/Sql.txt"></scriptDescriptionButton>-->
                <scriptTestButton :script.sync="fileViewItem.script"></scriptTestButton>
              </th>
              <td colspan="7">
                <el-form-item prop="script" :rules="[{ required: true, trigger: 'blur' }]">
                  <code-editor mode="java" :readonly="!1" v-model="fileViewItem.script"
                               v-if="showCodeEditor"></code-editor>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('创建人') }}</th>
              <td>{{ fileViewItem.createBy }}</td>
              <th>{{ $t1('创建时间') }}</th>
              <td>{{ fileViewItem.createDate }}</td>
              <th>{{ $t1('更新人') }}</th>
              <td>{{ fileViewItem.modifyBy }}</td>
              <th>{{ $t1('更新时间') }}</th>
              <td>{{ fileViewItem.modifyDate }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'file_view_itemEdit',
  props: ['_dataId', 'param'],
  data() {
    return {
      tabIndex: 0,
      isEdit: false,
      showCompanyDialog: false,
      dataId: '',
      fileViewItem: {
        viewAreaCode: null,
        serviceId: null
      },
      serviceIds: [],
      showCodeEditor: false
    };
  },
  created() {
    if (this._dataId) this.dataId = this._dataId;
  },
  mounted() {
    var that = this;
    this.wfServiceIds();
    this.getData();
  },
  methods: {
    getData() {
      if (this.dataId) {
        this.isEdit = true;
        this.$commonHttp({
          url: USER_PREFIX + `/file_view_item/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.fileViewItem = res.objx || {};
            this.showCodeEditor = true;
          }
        });
      } else {
        let viewAreaCode = this.param.viewAreaCode || '';
        this.fileViewItem.viewAreaCode = viewAreaCode;
        this.showCodeEditor = true;
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
            var url = this.isEdit ? USER_PREFIX + '/file_view_item/update' : USER_PREFIX + '/file_view_item/save';
            this.$http({
              url: url,
              method: `post`,
              data: this.fileViewItem,
              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
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    },
    wfServiceIds() {
      this.$http({
        url: USER_PREFIX + `/wf_obj_config/wfServiceIds`,
        method: `post`,
        data: {},
        isLoading: true,
        modalStrictly: true,
        success: res => {
          let serviceIds = res.objx || [];
          if (!this.isEdit && serviceIds.length) {
            this.fileViewItem.serviceId = serviceIds[0];
          }
          this.serviceIds = serviceIds;
        }
      });
    }
  }
};
</script>
