<template>
  <div class="detail-wrap">
    <el-form ref="editForm" :model="codeRules">
      <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="codeType" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="codeRules.codeType" clearable/>
                </el-form-item>
              </td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('编码名称') }}
              </th>
              <td>
                <el-form-item prop="codeName" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="codeRules.codeName" clearable/>
                </el-form-item>
              </td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('编码前缀') }}
              </th>
              <td>
                <el-form-item prop="codePre" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="codeRules.codePre" clearable/>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                <em class="f-red">*</em>
                {{ $t1('规则') }}
              </th>
              <td colspan="7">
                <el-form-item prop="rules" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-radio-group v-model="codeRules.rules" @change="()=>{codeRules.dateFormat=null}">
                    <el-radio :label="1">{{ $t1('按日重置流水号') }}</el-radio>
                    <el-radio :label="2">{{ $t1('按月重置流水号') }}</el-radio>
                    <el-radio :label="3">{{ $t1('按年重置流水号') }}</el-radio>
                    <el-radio :label="0">{{ $t1('流水号一直累加') }}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </td>
            </tr>
            <tr>
              <th>
                {{ $t1('时间格式') }}
              </th>
              <td>
                <el-form-item prop="dateFormat" :rules="[{ required: false, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="codeRules.dateFormat" clearable/>
                </el-form-item>
              </td>
              <th>
                <em class="f-red">*</em>
                {{ $t1('全部组织使用') }}
              </th>
              <td>
                <el-form-item prop="allCompany" :rules="[{ required: true, trigger: 'blur' }]">
                  <el-radio-group v-model="codeRules.allCompany" @change="()=>{codeRules.ruleCompanyCode=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="!codeRules.allCompany">*</em>
                {{ $t1('规则对应组织') }}
              </th>
              <td>
                <el-form-item prop="ruleCompanyCode" :rules="[{ required: !codeRules.allCompany, trigger: 'blur' }]">
                  <el-input type="text" autocomplete="off" v-model="codeRules.ruleCompanyCode" clearable
                            :disabled="codeRules.allCompany"/>
                </el-form-item>
              </td>
              <th>
                {{ $t1('流水固定位数') }}
              </th>
              <td>
                <el-form-item prop="flowDigit" :rules="[{ required: false, trigger: 'blur' }]">
                  <base-input-number v-model="codeRules.flowDigit" :sccale="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="codeRules.remark"
                          clearable></el-input>
              </td>
            </tr>
            <tr>
              <th>{{ $t1('创建人') }}</th>
              <td>{{ codeRules.createBy }}</td>
              <th>{{ $t1('创建时间') }}</th>
              <td>{{ codeRules.createDate }}</td>
              <th>{{ $t1('更新人') }}</th>
              <td>{{ codeRules.modifyBy }}</td>
              <th>{{ $t1('更新时间') }}</th>
              <td>{{ codeRules.modifyDate }}</td>
            </tr>
            </tbody>
          </table>
        </div>
      </div>
    </el-form>
  </div>
</template>

<script>

export default {
  name: 'codeRulesEdit',
  props: {
    _dataId: [String, Number]
  },
  components: {},
  data() {
    return {
      isEdit: false,
      tabIndex: 'first',
      dataId: '',
      codeRules: {
        allCompany: true,
        rules: 1,
        flowDigit: 0,
        customized: false,
        script: null,
        ruleCompanyCode: null,
        dateFormat: 'yyyyMMdd'
      }
    };
  },
  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 + `/code_rules/get`,
          method: `post`,
          data: {
            id: this.dataId
          },
          isLoading: true,
          modalStrictly: true,
          success: res => {
            this.codeRules = res.objx || {};
          }
        });
      }
    },
    saveData() {
      this.$refs.editForm.$baseValidate(valid => {
        if (valid) {
          this.$baseConfirm(this.$t1('您确定要保存吗？')).then(() => {
            var url = USER_PREFIX + (this.isEdit ? `/code_rules/update` : `/code_rules/save`);
            this.$http({
              url: url,
              method: `post`,
              data: this.codeRules,
              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
                        }
                      });
                    }
                  }
                });
              }
            });
          });
        }
      });
    },
    confirmHiprintDesignDialog(val) {
      this.$refs.etText.setValue(val);
    }
  }
};
</script>
