import { queenApi } from "queenjs";
import { ResourceModule } from "..";

export const materialActions = ResourceModule.action({
  async uploadMaterial() {
    const files = await this.helper.uploadResource({
      accept: "image/*, video/mp4",
      multiple: true,
    });

    let souceObj = files[0];
    for (const key in files) {
      souceObj = files[key];
      await this.https.createResource(souceObj);
    }

    this.controls.materialListCtrl.state.query = {
      fileType: this.store.type,
    };
    this.controls.materialListCtrl.loadPage(1);
    return souceObj;
  },

  async deleteMaterial(record) {
    const res = await queenApi.showConfirm({
      content: `删除后不可恢复，是否要删除此素材？`,
      type: "danger",
    });
    if (!res) return;
    await this.https.deleteResource(record._id);
    // this.controls.materialListCtrl.fresh();
  },

  downloadMaterial(record) {
    window.open(record.file.url);
  },

  async selectMaterial(record) {
    const _params = new URLSearchParams(decodeURIComponent(location.search));
    const host = _params.get("host");

    // if (location.host == "www.infish.cn") {
    //   const url = `${location.origin}/projects/queenshowv1/index.html?host=${host}#/create/${record._id}`;
    //   location.href = url;
    //   return;
    // }

    const url = `${location.origin}/index.html?host=${host}#/create/${record._id}`;
    location.href = url;
  },
});
