<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="commonAttribute">
      <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>{{ commonAttribute.name }}</td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('属性编号') }}
              </th>
              <td>{{ commonAttribute.code }}</td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('属性值') }}
              </th>
              <td>
                <el-form-item prop="value" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="commonAttribute.value" clearable/>
                </el-form-item>
              </td>
              <th>
                {{ $t1('英文属性值') }}
              </th>
              <td>
                <el-form-item prop="param1" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="commonAttribute.param5" clearable/>
                </el-form-item>
              </td>
              <th>{{ $t1('属性值编号') }}</th>
              <td>
                <el-form-item v-if="!dataId" prop="sn" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="commonAttribute.sn" clearable/>
                </el-form-item>
                <template v-else>
                  {{ commonAttribute.sn }}
                </template>
              </td>
              <td colspan="4"><span class="f-red">{{ $t1('注：不填写，自动产生唯一属性值编号') }}</span></td>
            </tr>
            <tr>
              <th>
                {{ $t1('组织编码') }}
              </th>
              <td colspan="5">
                <el-form-item prop="exeCompanyCode" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="commonAttribute.exeCompanyCode" clearable/>
                </el-form-item>
              </td>
              <td colspan="2"><span
                class="f-red">{{
                  $t1('注：多个组织用逗号隔开，未配置全组织可见，已配置只允许配置组织才可见当前属性值。')
                }}</span></td>
            </tr>
            <tr>
              <th>
                {{ $t1('参数1') }}
              </th>
              <td>
                <el-form-item prop="param1" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="commonAttribute.param1" clearable/>
                </el-form-item>
              </td>
              <th>
                {{ $t1('参数2') }}
              </th>
              <td>
                <el-form-item prop="param1" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="commonAttribute.param2" clearable/>
                </el-form-item>
              </td>
              <th>
                {{ $t1('参数3') }}
              </th>
              <td>
                <el-form-item prop="param1" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="commonAttribute.param3" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                {{ $t1('参数4') }}
              </th>
              <td>
                <el-form-item prop="param1" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="commonAttribute.param4" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('设置') }}</th>
              <td>
                <el-checkbox :label="$t1('是否启用')" v-model="commonAttribute.enabled"></el-checkbox>
              </td>
              <th>
                {{ $t1('序号') }}
              </th>
              <td>
                <el-form-item prop="orders" :rules="[{ required: false, trigger: 'blur' }]">
                  <base-input-number v-model="commonAttribute.orders" :scale="0"/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('备注') }}</th>
              <td colspan="5">
                <el-input type="textarea" :rows="2" :placeholder="$t1('请输入内容')" size="small"
                          v-model="commonAttribute.remark"
                          clearable></el-input>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('创建人') }}</th>
              <td>{{ commonAttribute.createBy }}</td>
              <th>{{ $t1('创建时间') }}</th>
              <td>{{ commonAttribute.createDate }}</td>
              <th>{{ $t1('更新人') }}</th>
              <td>{{ commonAttribute.modifyBy }}</td>
              <th>{{ $t1('更新时间') }}</th>
              <td>{{ commonAttribute.modifyDate }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  name: 'systemDictEdit',
  props: ['_dataId', 'param'],
  data() {
    return {
      tabIndex: 0,
      isEdit: false,
      showCompanyDialog: false,
      dataId: '',
      commonAttribute: {
        name: null,
        code: null,
        enabled: true,
        orders: 0
      }
    };
  },
  created() {
    if (this._dataId) this.dataId = this._dataId;
  },
  mounted() {
    var that = this;
    this.getData();
  },
  methods: {
    getData() {
      if (this.dataId) {
        this.isEdit = true;
        this.$commonHttp({
          url: USER_PREFIX + `/common_attribute/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.commonAttribute = res.objx || {};
          }
        });
      } else {
        let pName = this.param.name || '';
        let pCode = this.param.code || '';
        this.commonAttribute.name = pName;
        this.commonAttribute.code = pCode;
        this.commonAttribute.flag = 1;
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
            var url = this.isEdit ? USER_PREFIX + '/common_attribute/updateItem' : USER_PREFIX + '/common_attribute/saveItem';
            this.$http({
              url: url,
              method: `post`,
              data: this.commonAttribute,
              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>
