<template>
  <Layout-Child class="page-scroll">
    <div class="w-7/12 mx-auto my-6">
      <app-steps
        :data="mySteps"
        :currentStep="currentStep"
        @stepOnChange="stepOnChange"
        @stepOnFinished="stepOnFinished"
      ></app-steps>
    </div>
    <div class="w-11/12 mx-auto">
      <div class="step-one" v-if="currentStep == 1">
        <app-cell
          v-model="createNFT.value_name"
          type="input"
          text="剧本名称"
          placeholder="请输入剧本名称"
          class="text-font-white my-3"
          @cellOnChange="(v) => setCreateNFT({ value_name: v })"
        ></app-cell>
        <app-cell
          v-model="createNFT.value_publisher"
          type="input"
          text="发行人"
          placeholder="请输入发行人名称"
          class="text-font-white my-3"
          @cellOnChange="(v) => setCreateNFT({ value_publisher: v })"
        ></app-cell>
        <app-cell
          :pickValue="mySteps"
          type="pick"
          text="剧本题材"
          placeholder="请选择剧本题材"
          class="text-font-white my-3"
          @onClick="what"
        ></app-cell>
        <!-- <app-cell
          text="分类"
          type="select"
          :selected="createNFT.categoryType"
          :list="categoryTypes2"
          class="text-font-white my-3"
          @cellOnChange="(v) => (createNFT.categoryType = v)"
        ></app-cell> -->
        <div>
          <div class="text-font-white my-2">剧本简介</div>
          <textarea
            v-model="createNFT.value_des"
            name="des"
            id="nft-des"
            cols="30"
            rows="10"
            class="
              w-full
              bg-font-gray bg-opacity-20
              text-font-white
              p-3
              text-sm
            "
          ></textarea>
        </div>
        <div class="fixed bottom-0 w-full left-0 z-30">
          <app-btn
            text="下一步"
            class="w-11/12 mx-auto text-font-white rounded-2xl bg-font-blue"
            :disabled="validation"
            border="none"
            @btnClicked="currentStepChange(1)"
          ></app-btn>
        </div>
      </div>
      <div class="step-two" v-if="currentStep == 2">
        <app-cell
          text="剧本上传"
          class="text-font-white my-3"
          type="upload"
          placeholder="请上传剧本"
          :name="fileName"
          @cellOnChange="$router.push({ name: 'NftUpload' })"
        ></app-cell>
        <app-cell
          text="剧本HASH"
          class="text-font-white my-3"
          type="showText"
          :name="fileHash"
          @cellOnChange="getValue"
        ></app-cell>
        <app-cell
          text="平台存档"
          type="select"
          :selected="createNFT.archives"
          :list="archivesList"
          class="text-font-white my-3"
          @cellOnChange="(v) => (createNFT.archives = v)"
        ></app-cell>
        <app-cell
          text="授权阅读"
          type="select"
          :selected="createNFT.grant"
          :list="grantList"
          class="text-font-white my-3"
          @cellOnChange="(v) => (createNFT.grant = v)"
        ></app-cell>
        <div class="fixed bottom-0 w-full left-0 flex flex-row z-30">
          <app-btn
            text="上一步"
            class="w-5/12 mx-auto text-font-white rounded-2xl bg-font-blue"
            border="none"
            @btnClicked="currentStepChange(-1)"
          ></app-btn>
          <app-btn
            text="下一步"
            class="w-5/12 mx-auto text-font-white rounded-2xl bg-font-blue"
            :disabled="validation"
            border="none"
            @btnClicked="currentStepChange(1)"
          ></app-btn>
        </div>
      </div>
      <div class="step-three" v-if="currentStep == 3">
        <app-cell
          text="NFT编号"
          type="showText"
          :name="publish.nftId"
          class="text-font-white my-3"
        ></app-cell>
        <app-cell
          text="剧本HASH"
          type="showText"
          :name="publish.fileHash"
          class="text-font-white my-3"
          @cellOnChange="getValue"
        ></app-cell>
        <app-cell
          text="发行人地址"
          type="showText"
          :name="publish.wallet"
          class="text-font-white my-3"
          @cellOnChange="getValue"
        ></app-cell>
        <app-cell
          key="count"
          text="数量"
          :value="1"
          type="input-num"
          class="text-font-white my-3"
          @cellOnChange="
            (v) => {
              publish.count = v;
            }
          "
        ></app-cell>
        <div class="fixed bottom-0 w-full left-0 flex flex-row z-30">
          <app-btn
            text="上一步"
            class="w-5/12 mx-auto text-font-white rounded-2xl bg-font-blue"
            border="none"
            @btnClicked="currentStepChange(-1)"
          ></app-btn>
          <app-btn
            text="发行NFT"
            class="w-5/12 mx-auto text-font-white rounded-2xl bg-font-blue"
            border="none"
            :disabled="validation"
            @btnClicked="currentStepChange(1)"
          ></app-btn>
        </div>
      </div>
    </div>
  </Layout-Child>
</template>

<script lang="ts">
import Vue from "vue";
import { mapState } from "vuex";

export default Vue.extend({
  data() {
    return {
      a: "",
      createNFT: {
        value_name: "",
        value_publisher: "",
        value_picker: [],
        value_des: "",
        archives: 0,
        grant: 0,
        categoryType: NaN,
      },
      publish: {
        nftId: "",
        id: NaN,
        fileHash: "",
        wallet: "",
        count: 1,
      },
      archivesList: [
        { value: 0, name: "加密存档" },
        { value: 1, name: "不存档" },
      ],
      grantList: [
        { value: 0, name: "不需要授权" },
        { value: 1, name: "需要授权" },
      ],
      categoryTypes: [] as any[],
      mySteps: [
        {
          text: "step1",
        },
        {
          text: "step2",
        },
        {
          text: "step3",
        },
      ],
      currentStep: 1,
    };
  },
  components: {
    "Layout-Child": () => import("@/layout/Child.vue"),
    "app-steps": () => import("@/components/common/Steps.vue"),
    "app-cell": () => import("@/components/common/Cell.vue"),
    "app-btn": () => import("@/components/common/Btn.vue"),
  },
  async mounted() {
    const list = await this.$service.nftService.getCategory();
    this.categoryTypes = list;
    this.createNFT.categoryType = (list.length !== 0 && list[0].id) || NaN;
  },
  computed: {
    ...mapState("create", {
      fileHash: "fileHash",
      fileName: "fileName",
      file: "file",
      pickedList: "pickedList",
    }),
    validation(): boolean {
      let disabled = true;
      const { value_name, value_publisher } = this.createNFT;
      if (this.currentStep === 1) {
        if (
          value_name &&
          value_publisher &&
          this.$store.state.create.pickedList.length !== 0
        ) {
          disabled = false;
        }
      } else if (this.currentStep === 2) {
        if (this.fileHash && this.fileName) {
          disabled = false;
        }
      } else if (this.currentStep === 3) {
        disabled = !(this.publish.count > 0);
      }
      return disabled;
    },
    categoryTypes2(): any[] {
      return this.categoryTypes.map((item: any) => {
        return {
          name: item.categoryName,
          value: item.id,
        };
      });
    },
  },
  methods: {
    setCreateNFT(obj: any) {
      this.createNFT = {
        ...this.createNFT,
        ...obj,
      };
    },
    addStep() {
      this.currentStep++;
    },
    stepOnFinished(e: boolean) {
      console.log(e, "finished");
    },
    stepOnChange(e: string) {
      console.log(e, "change");
    },
    getValue(val: string) {
      console.log(val);
    },
    what(item: Function) {
      item(this.$router.push("/Nft/create/pick"));
    },
    async currentStepChange(val: number) {
      if (this.currentStep === 2 && val === 1) {
        let mistake = false;
        try {
          const { id, nftId, wallet, fileHash } =
            await this.$service.nftService.save(
              this.createNFT.value_publisher,
              1,
              this.fileHash,
              this.createNFT.archives,
              this.createNFT.value_name,
              this.createNFT.value_des,
              this.pickedList.map((i: any) => i.id as string).toString(),
              this.file,
              this.createNFT.grant
            );
          this.publish.fileHash = fileHash;
          this.publish.nftId = nftId;
          this.publish.id = id;
          this.publish.wallet = wallet;
        } catch (err) {
          mistake = true;
          return;
        }
        if (mistake) return;
      } else if (this.currentStep === 3 && val === 1) {
       await this.$service.nftService.publish({
          fileHash: this.fileHash,
          id: this.publish.id,
          wallet: this.publish.wallet,
          nftId: this.publish.nftId,
        });
        this.$router.back()
      }
      if (this.currentStep < this.mySteps.length + 1) {
        this.currentStep += val;
      }
    },
  },
});
</script>
