<!-- 用户编辑弹窗 -->
<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"
    ref="Emodal"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="82px">
      <el-row :gutter="15">
        <el-col :span="24">
          <el-form-item label="编码分类" prop="codeType">
            <ele-tree-select
              clearable
              :data="list"
              v-model="form.codeType"
              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="businessCodeId">
            <el-select
              v-model="form.businessCodeId"
              placeholder="请选择"
              style="width: 100%"
            >
              <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="type">
            <DictSelection
              dictName="文档类型"
              v-model="form.type"
            ></DictSelection>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="文档" prop="storagePath">
            <div style="display: flex; align-items: center">
              <fileUpload
                v-model="form.storagePath"
                module="main"
                :limit="100"
                :multiple="true"
                @fileChange="fileChange"
              >

              </fileUpload>
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="文档位置" prop="directoryId">
            <el-cascader
              style="width: 100%"
              v-model="form.directoryId"
              :options="folderList"
              :props="{
                value: 'id',
                label: 'name',
                children: 'sonDirectoryList',
                emitPath: false,
                checkStrictly: true
              }"
            ></el-cascader>
          </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-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 { fileSaveAPI,selectTreeList, listParentId,getDocTreeListAPI,listCode} from './api/index';
import FileUpload from './fileUpload.vue';
import { setFolderList } from './util.js';
import { mapGetters } from 'vuex';
export default {
  components: { FileUpload },

  data() {
    const defaultForm = {
      name: '', //名称
      type: '', //类型
      sizeUnit: '', //大小，
      unit: '', //单位
      remark: '', //备注
      status: '', //状态
      storagePathId: '',
      directoryId: '',
      businessCodeId: '',
      storagePath: [],
      id: '',
      lcyStatus: 1,
      fileType:0
    };
    return {
      rules: {
        businessCodeId: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        // codeType: [{ required: true, message: '请选择', trigger: 'blur' }],
        directoryId: [{ required: true, message: '请选择', trigger: 'blur' }],
        storagePath: [{ required: true, message: '请选择', trigger: 'blur' }]
      },
      templateVisible: false,
      folderList: [],
      list: [],
      options: [],
      defaultForm,
      code: '',
      // 表单数据
      form: { ...defaultForm },

      // 提交状态
      loading: false,
      showEditFlag: false,
      title: '',
      type: '新建附件'
    };
  },
  computed: {
    // 是否开启响应式布局
    styleResponsive() {
      return this.$store.state.theme.styleResponsive;
    },
    ...mapGetters(['user'])
  },
  async created() {
    let query = {
      type: 0,
      currentUserId: this.user.info.userId
    };
    this.folderList = await getDocTreeListAPI(query);
    setFolderList(this.folderList); //权限过滤
  },
  methods: {
    async open() {
    
      this.showEditFlag = true;
      this.list = await selectTreeList();
      this.options= await listCode();
      if(this.options.length>0){
        this.form.businessCodeId=this.options[0].id
      }
      this.setTree(this.list);
    },
    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.businessCodeId = '';
    },

    fileChange(file) {
      this.form.name = file.name.replace(/\.[^/.]+$/, '');
    },

    /* 保存编辑 */
    save() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) {
          return false;
        }

        const data = {
          ...this.form,
        };

        this.loading = true;
        fileSaveAPI(data)
          .then( (msg) => {
            this.loading = false;

            this.cancel();
            this.$emit('done',msg);
          })
          .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>
