<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="wfObjConfig">
      <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="serviceId" :rules="[{ required: true, trigger: 'blur' }]" v-if="!dataId">
                  <el-select v-model="wfObjConfig.serviceId">
                    <el-option v-for="(serviceId,index) in serviceIds" :key="index" :value="serviceId">{{
                        serviceId
                      }}
                    </el-option>
                  </el-select>
                </el-form-item>
                <template v-else>
                  {{ wfObjConfig.serviceId }}
                </template>
              </td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('单据类型编号') }}
              </th>
              <td>
                <el-form-item prop="objTypeCode" :rules="[{ required: true, trigger: 'blur' }]" v-if="!dataId">
                  <el-input autocomplete="off" v-model="wfObjConfig.objTypeCode" clearable/>
                </el-form-item>
                <template v-else>
                  {{ wfObjConfig.objTypeCode }}
                </template>
              </td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('单据类型名称') }}
              </th>
              <td>
                <el-form-item prop="objTypeName" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input autocomplete="off" v-model="wfObjConfig.objTypeName" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('跳转url') }}
              </th>
              <td colspan="5">
                <el-form-item prop="url" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input autocomplete="off" v-model="wfObjConfig.url" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                {{ $t1('移动端url') }}
              </th>
              <td colspan="5">
                <el-form-item prop="mobileUrl" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input autocomplete="off" v-model="wfObjConfig.mobileUrl" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('备注') }}</th>
              <td colspan="5">
                <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" size="small"
                          v-model="wfObjConfig.note"
                          clearable></el-input>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('是否表单') }}</th>
              <td>{{ wfObjConfig.forms ? $t1('是') : $t1('否') }}</td>
            </tr>
            <tr>
              <th>{{ $t1('创建人') }}</th>
              <td>{{ wfObjConfig.createBy }}</td>
              <th>{{ $t1('创建时间') }}</th>
              <td>{{ wfObjConfig.createDate }}</td>
              <th>{{ $t1('更新人') }}</th>
              <td>{{ wfObjConfig.modifyBy }}</td>
              <th>{{ $t1('更新时间') }}</th>
              <td>{{ wfObjConfig.modifyDate }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
  </div>
</template>

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