<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="notifyTemplate">
      <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>
                  <el-form-item prop="notifyType" :rules="[{ required: true, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="notifyTemplate.notifyType" clearable/>
                  </el-form-item>
                </td>
                <th>
                  <em class="f-red">*</em>
                  {{ $t1('主题') }}
                </th>
                <td>
                  <el-form-item prop="ntName" :rules="[{ required: true, trigger: 'blur' }]">
                    <el-input type="text" autocomplete="off" v-model="notifyTemplate.ntName" clearable/>
                  </el-form-item>
                </td>
                <th>{{ $t1('是否启用') }}</th>
                <td>
                  <el-form-item prop="enabled" :rules="[{ required: false, trigger: 'blur' }]">
                    <el-radio-group v-model="notifyTemplate.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-input type="textarea" :rows="2" size="small" v-model="notifyTemplate.remark"
                            clearable></el-input>
                </td>
              </tr>
              <tr>
                <th><em class="f-red">*</em>{{ $t1('内容') }}</th>
                <td colspan="7">
                  <el-form-item prop="content" :rules="[{ required: true, trigger: 'blur' }]">
                              <tinymce v-model="notifyTemplate.content" :language="lan" :height="300"/>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <th>
                  {{ $t1('脚本(获取通知人)') }}
                  <scriptTestButton :script.sync="notifyTemplate.script"></scriptTestButton>
                </th>
                <td colspan="7">
                  <el-form-item prop="script" :rules="[{ required: false, trigger: 'blur' }]">
                    <code-editor mode="java" :readonly="!1" v-model="notifyTemplate.script"
                                 v-if="showCodeEditor"></code-editor>
                  </el-form-item>
                </td>
              </tr>
              <tr>
                <th>{{ $t1('创建人') }}</th>
                <td>{{ notifyTemplate.createBy }}</td>
                <th>{{ $t1('创建时间') }}</th>
                <td>{{ notifyTemplate.createDate }}</td>
                <th>{{ $t1('更新人') }}</th>
                <td>{{ notifyTemplate.modifyBy }}</td>
                <th>{{ $t1('更新时间') }}</th>
                <td>{{ notifyTemplate.modifyDate }}</td>
              </tr>
              </tbody>
            </table>
          </template>
        </baseTabPane>
      </baseTabs>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@base/components/Tinymce';
export default {
  name: 'notify_templateEdit',
  props: {
    _dataId: [String, Number]
  },
  components: {Tinymce},
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      notifyTemplate: {
        enabled: true,
        ntType:1
      },
      showCodeEditor: 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 + `/notify_template/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.notifyTemplate = 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 ? `/notify_template/update` : `/notify_template/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.notifyTemplate,
              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
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    }
  }
};
</script>
