<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="commonScript">
      <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>
                <el-form-item prop="scriptCode" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="commonScript.scriptCode" clearable/>
                </el-form-item>
              </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="commonScript.enabled">
                    <el-radio :label="true">{{ $t1('启用') }}</el-radio>
                    <el-radio :label="false">{{ $t1('禁用') }}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </td>
              <th>
                {{ $t1('数据类型') }}
              </th>
              <td>
                <el-form-item prop="dataTypeName" :rules="[{ required: false, trigger: ['blur', 'change'] }]">
                  <el-input
                    class="search-input"
                    max="200"
                    v-model="commonScript.dataTypeName"
                    v-el-readonly
                    clearable
                    @clear="commonScript.dataTypeCode=null"
                  >
                    <i slot="suffix" class="el-input__icon el-icon-search" @click="showDataTypeDialog = true"></i>
                  </el-input>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('全部组织使用') }}
              </th>
              <td>
                <el-form-item prop="allCompany" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-radio-group v-model="commonScript.allCompany" @change="()=>{commonScript.exeCompanyCode=null}">
                    <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" v-if="!commonScript.allCompany">*</em>
                {{ $t1('执行组织编码') }}
              </th>
              <td>
                <el-form-item prop="exeCompanyCode" :rules="[{ required: !commonScript.allCompany, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="commonScript.exeCompanyCode" clearable
                            :disabled="commonScript.allCompany"/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('脚本说明') }}
              </th>
              <td colspan="7">
                <el-form-item prop="scriptDesc" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" v-model="commonScript.scriptDesc"
                            clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                {{ $t1('脚本') }}
                <!--                <scriptDescriptionButton path="static/readme/PushSetting.txt"></scriptDescriptionButton>-->
                <scriptTestButton :script.sync="commonScript.script"></scriptTestButton>
              </th>
              <td colspan="7">
                <el-form-item prop="script" :rules="[{ required: false, trigger: 'blur' }]">
                  <code-editor mode="java" :readonly="!1" v-model="commonScript.script" height="475px"
                               v-if="showCodeEditor"></code-editor>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('创建人') }}</th>
              <td>{{ commonScript.createBy }}</td>
              <th>{{ $t1('创建时间') }}</th>
              <td>{{ commonScript.createDate }}</td>
              <th>{{ $t1('更新人') }}</th>
              <td>{{ commonScript.modifyBy }}</td>
              <th>{{ $t1('更新时间') }}</th>
              <td>{{ commonScript.modifyDate }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
    <dataTypeDialog v-if="showDataTypeDialog" :visiable.sync="showDataTypeDialog" multi="false"
                    @confirm="confirmDataType" :dataTypeGroup="dataTypeGroup"/>
  </div>
</template>

<script>
import dataTypeDialog from "../../../views/user/data_type_setting/dialog";

export default {
  name: 'user_commonScriptEdit',
  props: {
    _dataId: [String, Number]
  },
  components: {dataTypeDialog},
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      commonScript: {
        enabled: true,
        allCompany: true,
        dataTypeCode: null,
        dataTypeName: null,
        exeCompanyCode: null
      },
      showCodeEditor: false,
      dataTypeGroup: "commonScript",
      showDataTypeDialog: false
    };
  },
  created() {
    if (this._dataId && !isNaN(this._dataId)) this.dataId = this._dataId;
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      if (this.dataId && !isNaN(this.dataId)) {
        this.isEdit = true;
        this.$commonHttp({
          url: USER_PREFIX + `/common_script/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.commonScript = 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 ? `/common_script/update` : `/common_script/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.commonScript,
              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
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    },
    confirmDataType(rows) {
      if (rows.length) {
        let row = rows[0];
        this.commonScript.dataTypeCode = row.dataTypeCode;
        this.commonScript.dataTypeName = row.dataTypeName;
      }
    }
  }
};

</script>
