<!-- 用户编辑弹窗 -->
<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="code">
            <el-input v-model="form.code" placeholder="请申请编码" disabled>
              <template slot="append">
                <el-link :underline="false" @click.native="getCodeOpen"
                  >申请编码
                </el-link></template
              >
            </el-input>
          </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="SCFS">
            <el-radio-group v-model="SCFS">
              <el-radio :label="1">手动上传</el-radio>
              <el-radio :label="2">选择模板</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col> -->
        <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="storagePath">
            <div style="display: flex; align-items: center">
              <fileUpload
                v-model="form.storagePath"
                module="main"
                :limit="1"
                @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>
    <getCode ref="getCode" @success="success"></getCode>
  </ele-modal>
</template>

<script>
  import { fileSaveAPI, getDocTreeListAPI } from './api';
  import FileUpload from './fileUpload.vue';
  import { setFolderList } from './util.js';
  import getCode from './getCode.vue';
  import { mapGetters } from 'vuex';

  export default {
    components: { FileUpload, getCode },

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

        // 提交状态
        loading: false,
        showEditFlag: false,
        title: '新建附件'
      };
    },
    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;
      },
      getCodeOpen() {
        this.$refs.getCode.open();
      },
      fileChange(file) {
        this.form.name = file.name.replace(/\.[^/.]+$/, '');
      },
      success(code) {
        this.code = code;
        this.$nextTick(() => {
          this.$set(this.form, 'code', code);
        });
      },
      /* 保存编辑 */
      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>
