import { queenApi } from "queenjs";
import { ResourceModule } from "..";
import { getPathname } from "@/dict";

export const promotionAction = ResourceModule.action({
  async renamePromotion(record: any) {
    const title = await queenApi.showInput({
      title: "请输入标题",
      defaultValue: record.title,
    });
    if (!title) return;
    await this.https.updatePromotion({ _id: record._id, title });
    record.title = title;
  },

  async publishPromotion(record: any, publish: boolean) {
    await this.https.publishPromotion(record._id, publish);
    record.published = publish;
    queenApi.messageSuccess("操作成功!");
  },

  async deletePromotion(record: any) {
    const res = await queenApi.showConfirm({
      content: `删除后无法恢复，确定要删除：${record.title}？`,
      type: "danger",
    });
    if (!res) return;
    await this.https.deletePromotion(record._id);
    // this.controls.promotionListCtrl.fresh();
  },

  async deleteCustomComp(record: any) {
    const res = await queenApi.showConfirm({
      content: `删除后无法恢复，确定要删除当前组合?`,
      type: "danger",
    });
    if (!res) return;
    await this.https.deleteComp(record._id);
  },
  async deleteUserComp(record: any, title: string) {
    const res = await queenApi.showConfirm({
      content: `删除后无法恢复，确定要删除当前${title}:${record.title}?`,
      type: "danger",
    });
    if (!res) return;
    await this.https.deleteComp(record._id);
  },
  async editSource(record: any, sourceType: string) {
    // const itemRes = await this.https.detailComp(record._id);
    // if (itemRes.errorNo != 200) {
    //   queenApi.messageWarn("未查询到数据！");
    //   return;
    // }
    let type = sourceType.toLowerCase();
    const res = await this.showModal(
      <this.components.SouceModal item={record} sourceType={type} />,
      {
        width: "360px",
        title: "编辑",
        maskClosable: false,
      }
    );

    if (type == "image" || type == "video") {
      await this.https.updateResouce(res);
    } else if (type == "template") {
      await this.https.updatePromotion(res);
    } else {
      await this.https.updateComp(res);
    }
    queenApi.messageSuccess("保存成功");
  },

  async createPromotion() {
    // const title = await queenApi.showInput({
    //   title: "请输入标题",
    // });
    const data = await this.showModal(
      <this.components.PromotionCreate sourceType="template" />,
      {
        width: "340px",
        maskClosable: false,
        title: "新建",
      }
    );
    if (!data) return;
    const res = await this.https.createPromotion(data);
    //console.log(location.host, location.host == "www.infish.cn");
    // if (location.host == "www.infish.cn") {
    //   const url = `${location.origin}${location.pathname}/projects/queenshowv1/editor.html#/?id=${res.result}`;
    //   location.href = url;
    //   return;
    // }

    const url = `${location.origin}${getPathname()}editor.html#/?id=${res.result}`;
    location.href = url;
  },

  async createComp() {
    const title = await queenApi.showInput({
      title: "请输入标题",
    });
    if (!title) return;
    const res = await this.https.createComp({ title });
    // console.log(location.host, location.host == "www.infish.cn");
    // if (location.host == "www.infish.cn") {
    //   const url = `${location.origin}${location.pathname}/projects/queenshowv1/editor.html#/?id=${res.result}&mode=editComp`;
    //   location.href = url;
    //   return;
    // }

    const url = `${location.origin}${getPathname()}editor.html#/?id=${res.result}&mode=editComp`;
    location.href = url;
  },

  async deleteComp(record: any) {
    const res = await queenApi.showConfirm({
      content: `删除后无法恢复，确定要删除：${record.title}？`,
      type: "danger",
    });
    if (!res) return;
    await this.https.deleteComp(record._id);
    // this.controls.promotionListCtrl.fresh();
  },
  async renameComp(record: any) {
    const title = await queenApi.showInput({
      title: "请输入标题",
      defaultValue: record.title,
    });
    if (!title) return;
    await this.https.updateComp({ _id: record._id, title });
    record.title = title;
  },
});
