<template>
  <div>
    <el-dialog
      title="选择文件夹"
      :visible.sync="Visible"
      width="40%"
      @close="handleClose"
      :close-on-click-modal="false"
    >
      <div class="folder-list">
        <el-radio
          v-model="form.categoryId"
          name="type"
          v-for="v in folderList"
          :key="v.id"
          :label="v.id"
        >
          <div class="folder-item flex flexc aic jca">
            <div class="folder-img-box flex aic jcc">
              <i class="iconfont icon-folder"></i>
            </div>
            <p class="folder-name tac">{{ v.categoryName }}</p>
          </div>
        </el-radio>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleRest">重 置</el-button>
        <el-button
          :loading="loading"
          size="small"
          type="primary"
          @click="handleOk"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
    
    <script>
import { getMatterFolderList } from "@/api/matter";
import { batchSaveDatumToCategory } from "@/api/materials";
export default {
  props: {
    folderListVisible: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      categoryId: "",
      folderList: [], // 分类列表
      materialList: [], // 材料列表
      form: {
        categoryId: "",
        categoryName: "",
        materialId: "",
        materialName: "",
        sort: 79,
      },
      loading: false,
    };
  },
  computed: {
    Visible: {
      get() {
        return this.folderListVisible;
      },
      set(val) {
        this.$emit("update:folderListVisible", val);
      },
    },
  },
  created() {},
  methods: {
    // 获取事项文件夹列表
    async getMatterFolderList() {
      let res = await getMatterFolderList({ page: 1, size: -1 });
      if (res.data.code == 1) {
        this.folderList = res.data.data.data;
      }
    },
    // 确定
    async handleOk() {
      if (!this.form.categoryId) {
        this.$message.warning("请勾选文件夹");
        return;
      }
      this.loading = true;
      let arr = this.materialList.map((v) => {
        return {
          categoryId: this.categoryId,
          materialId: v.id,
          materialName: v.materialName,
        };
      });
      let res = await batchSaveDatumToCategory(arr);
      let { code, msg } = res.data;
      this.loading = false;
      if (code === 1) {
        this.$message.success(msg);
        this.$emit("addSuccess");
        this.handleClose();
      }
    },
    // 新增
    onAdd(row) {
      Object.assign(this.form, this.$options.data().form);
      this.form.id && this.$delete(this.form, "id");
      //   this.form.materialId = row.id;
      //   this.form.materialName = row.materialName;
      this.materialList = row;
      this.getMatterFolderList();
    },
    // 编辑
    onEdit(row) {
      this.form = { ...row };
    },
    // 重置
    handleRest() {
      this.checked = "";
    },
    // 关闭
    handleClose() {
      this.categoryId = "";
      this.loading = false;
      this.Visible = false;
    },
  },
};
</script>
    
<style lang="less" scoped>
.folder-list {
  min-height: 400px;
  max-height: 600px;
  overflow-y: auto;
  display: grid;
  grid-template-columns: 182px 182px 182px 182px;
  justify-content: center;
  grid-row-gap: 10px;
  .folder-item {
    width: 120px;
    height: 140px;
    // border: 1px solid red;

    .icon-folder {
      font-size: 50px;
      color: #188fff;
    }
    .folder-img-box {
      width: 80px;
      height: 80px;
      background-color: rgba(24, 143, 255, 0.1);
      border-radius: 4px;
    }
    .folder-name {
      width: 100%;
      font-size: 18px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }
}
/deep/.el-checkbox__input {
  position: absolute !important;
}
</style>