<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="formTemplate">
      <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>
                模板名称
              </th>
              <td>
                <el-form-item prop="formName" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="formTemplate.formName" clearable/>
                </el-form-item>
              </td>
              <th>模板编码</th>
              <td>
                <el-form-item prop="formCode" :rules="[{ required: true, trigger: 'blur' }]" v-if="!formTemplate.id">
                  <el-input type="text" autocomplete="off" v-model="formTemplate.formCode" clearable/>
                </el-form-item>
                <template v-else>
                  {{ formTemplate.formCode }}
                </template>
              </td>
              <th>
                <em class="f-red">*</em>
                模板版本
              </th>
              <td>{{ formTemplate.formVersion }}</td>
              <th>是否历史版本</th>
              <td>{{ formTemplate.history ? '是' : '否' }}</td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                服务名
              </th>
              <td>
                <el-form-item prop="serviceId" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-select v-model="formTemplate.serviceId">
                    <el-option v-for="(serviceId,index) in serviceIds" :key="index" :value="serviceId">{{
                        serviceId
                      }}
                    </el-option>
                  </el-select>
                </el-form-item>
              </td>
              <th>是否启用</th>
              <td>
                <el-form-item prop="enabled" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-radio-group v-model="formTemplate.enabled">
                    <el-radio :label="true">启用</el-radio>
                    <el-radio :label="false">禁用</el-radio>
                  </el-radio-group>
                </el-form-item>
              </td>
              <th>
                表单类型
              </th>
              <td>
                <el-form-item prop="formTypeCode" :rules="[{ required: false, trigger: ['blur', 'change'] }]">
                  <el-input
                    class="search-input"
                    max="200"
                    v-model="formTemplate.formTypeName"
                    v-el-readonly
                    clearable
                    @clear="formTemplate.formTypeCode=null"
                  >
                    <i slot="suffix" class="el-input__icon el-icon-search" @click="showFormTypeDialog = true"></i>
                  </el-input>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('备注') }}</th>
              <td colspan="3">
                <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" size="small" v-model="formTemplate.remark"
                          clearable></el-input>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('创建人') }}</th>
              <td>{{ formTemplate.createBy }}</td>
              <th>{{ $t1('创建时间') }}</th>
              <td>{{ formTemplate.createDate }}</td>
              <th>{{ $t1('更新人') }}</th>
              <td>{{ formTemplate.modifyBy }}</td>
              <th>{{ $t1('更新时间') }}</th>
              <td>{{ formTemplate.modifyDate }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
    <formTypeDialog v-if="showFormTypeDialog" :visiable.sync="showFormTypeDialog" multi="false"
                    @confirm="confirmFormType"/>
  </div>
</template>

<script>
import formTypeDialog from "../../../../views/user/form/form_type/dialog";

export default {
  name: 'form_templateEdit',
  props: {
    _dataId: [String, Number]
  },
  components: {
    formTypeDialog
  },
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      formTemplate: {
        enabled: true,
        history: false,
        serviceId: 'user',
        formTypeCode: null
      },
      isInited: false,
      serviceIds: ['user'],
      showFormTypeDialog: false
    };
  },
  created() {
    if (this._dataId) this.dataId = this._dataId;
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      if (this.dataId) {
        this.isEdit = true;
        this.$commonHttp({
          url: USER_PREFIX + `/form_template/getByFormCode`,
          method: `post`,
          data: {
            stringOne: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.formTemplate = res.objx || {};
            this.$nextTick(() => {
              this.isInited = true;
            });
          }
        });
      } else {
        this.isInited = true;
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
            var url = USER_PREFIX + (this.isEdit ? `/form_template/update` : `/form_template/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.formTemplate,
              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: this.formTemplate.formCode
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    },
    confirmFormType(rows) {
      if (rows.length) {
        let row = rows[0];
        this.formTemplate.formTypeCode = row.formTypeCode;
        this.formTemplate.formTypeName = row.formTypeName;
      }
    }
  }
};
</script>
