<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="500px"
    :visible.sync="showEditFlag"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    append-to-body
    @close="cancel"
    :title="title + '编码码段'"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col :span="24">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="描述" prop="remark">
            <el-input
              v-model="form.remark"
              type="textarea"
              placeholder="请输入"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            :rules="{
              required: form.roughRule == 4 ? false : true,
              trigger: ['blur'],
              message: '请输入'
            }"
            label="长度"
            prop="length"
          >
            <el-input
              v-model="form.length"
              type="number"
              min="0"
              max="12"
              :disabled="form.roughRule == 4"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="间隔符" prop="delimiter">
            <DictSelection
              dictName="编码间隔符"
              v-model="form.delimiter"
            ></DictSelection>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="粗规则" prop="roughRule">
            <el-select
              v-model="form.roughRule"
              placeholder="请选择"
              @change="roughRuleChange"
              style="width: 100%"
            >
              <el-option
                v-for="item in roughRule"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="细规则" prop="fineRule">
            <el-select
              v-model="form.fineRule"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="item in fineRule"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        确认
      </el-button>
    </template>
  </ele-modal>
</template>
  
  <script>
import { saveSegment } from '@/api/businessCode';
import { roughRule, fineRule } from '@/enum/dict.js';

export default {
  components: {},
  props: {
    // 上级id
    businessCodeCategoryId: [Number, String]
  },
  data() {
    const defaultForm = {
      name: '', //码段名称
      businessCodeCategoryId: '', //分类id
      length: '', //长度
      delimiter: '', //间隔符
      roughRule: '', //粗规则
      fineRule: '', //细规则
      value: [],
      remark: '' //描述
    };
    return {
      roughRule,
      fineRule: [],
      defaultForm,
      // 表单数据
      form: { ...defaultForm },
      // 表单验证规则
      rules: {},
      // 提交状态
      loading: false,
      showEditFlag: false,
      title: '',
      type: '',
      rules: {
        name: [
          {
            required: true,
            message: '请输入',
            trigger: 'blur'
          }
        ],
        // delimiter: [
        //   {
        //     required: true,
        //     message: '请选择',
        //     trigger: 'blur'
        //   }
        // ],
        roughRule: [
          {
            required: true,
            message: '请选择',
            trigger: 'blur'
          }
        ],
        fineRule: [
          {
            required: true,
            message: '请选择',
            trigger: 'blur'
          }
        ]
      }
    };
  },
  computed: {
    // 是否开启响应式布局
    styleResponsive() {
      return this.$store.state.theme.styleResponsive;
    }
  },
  created() {},
  methods: {
    async open(type, row) {
      this.title = type === 'edit' ? '编辑' : '新增';
      this.showEditFlag = true;
      this.type = type;
      if (type == 'add') {
        this.form.businessCodeCategoryId = this.businessCodeCategoryId;
      } else {
        this.form = JSON.parse(JSON.stringify(row));
        this.fineRule = fineRule.filter(
          (item) => item.pid == this.form.roughRule
        );
      }
    },
    roughRuleChange(val) {
      if (val == 4) {
        this.form.length = '';
      }
      this.fineRule = fineRule.filter((item) => item.pid == val);
      this.form.fineRule = '';
    },
    /* 保存编辑 */
    save() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return false;
        }

        const data = {
          ...this.form
        };
        if (this.type != 'edit') {
          delete data.id;
        }
        this.loading = true;
        saveSegment(data)
          .then((msg) => {
            this.loading = false;
            this.cancel();
            this.$emit('done');
          })
          .catch((e) => {
            this.loading = false;
          });
      });
    },

    cancel() {
      this.form = { ...this.defaultForm };
      this.$refs.form.clearValidate();
      this.showEditFlag = false;
    }
  }
};
</script>
  <style scoped lang="scss">
.aaa {
  width: 100%;

  ::v-deep .upload-demo {
    width: 100%;

    .el-upload--text {
      width: 100%;

      button {
        width: 100%;
        background: #ffffff;
        border: 1px solid #dbdbdb;
        border-radius: 5px;
      }
    }

    .el-upload-list {
      transform: translate(10px, -39px);
      position: absolute;
    }
  }
}
</style>
  