<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="500px"
    :visible.sync="showEditFlag"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    append-to-body
    @close="cancel"
    title="申请编码"
    :maxable="true"
    :resizable="true"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col :span="24">
          <el-form-item label="编码分类" prop="type">
            <ele-tree-select
              clearable
              :data="list"
              v-model="form.type"
              placeholder="请选择"
              default-expand-all
              labelKey="name"
              childrenKey="sonDirectoryList"
              valueKey="id"
              @change="typeChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="编码方案" prop="type1">
            <el-select
              v-model="form.type1"
              placeholder="请选择"
              style="width: 100%"
              @change="type1Change"
            >
              <el-option
                v-for="item in options"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="编码预览" prop="code">
            <el-input v-model="form.code" disabled></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <headerTitle title="编码码段"></headerTitle>
    <template v-slot:footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="save"> 确认 </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import { selectTreeList, listParentId, getCode } from './api';

  export default {
    data() {
      return {
        rules: {
          type: [{ required: true, message: '请选择', trigger: 'blur' }],
          type1: [{ required: true, message: '请选择', trigger: 'blur' }],
          code: [{ required: true, message: '请申请', trigger: 'blur' }]
        },
        list: [],
        options: [],
        // 表单数据
        form: {
          type: '',
          code: '',
          type1: ''
        },

        showEditFlag: false
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    components: {
    },
    created() {},
    methods: {
      async open() {
        this.list = await selectTreeList();
        this.setTree(this.list);
        this.showEditFlag = true;
      },
      setTree(data) {
        data.forEach((item) => {
          item.sonDirectoryList = item.sonDirectoryList.filter(
            (item) => item.type == 1
          );
          if (item.sonDirectoryList.length > 0) {
            this.setTree(item.sonDirectoryList);
          }
        });
      },
      async typeChange(val) {
        let data = await listParentId({
          pageNum: 1,
          size: 100,
          parentId: val
        });
        this.options = data.list.filter((item) => item.type == 2);
        this.form.type1 = '';
      },
      async type1Change(val) {
        // this.form.code = await getCode(val);
      },
      /* 保存编辑 */
      save() {
        this.$emit('success', this.form.code);
        this.cancel();
      },

      cancel() {
        this.form.type = '';
        this.form.code = '';
        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>
