import { Button, Popup } from "vant";
import { } from "vant";
import { defineComponent, nextTick, onMounted, onUnmounted, reactive, ref, watch } from "vue";
import styles from "./index.module.less";
import { getImage } from "./images";
import { useRect } from "@vant/use";

export default defineComponent({
  name: "knowledge-guide",
  emits: ["close"],
  setup(props, { emit }) {
    const data = reactive({
      box: {},
      show: false,
      steps: [
        {
          ele: "",
          eleRect: {} as DOMRect,
          img: getImage("knowledge1.png"),
          handStyle: {
            top: "1.3rem",
            left:'1.5rem'
          },
          imgStyle: {
            width:'5.46rem',
            height:'2.30rem',
            top: "1.9rem",
          },
          btnsStyle: {
            top: "4.5rem",
            left:'1rem'
          },
          boxStyle:{
            transform: 'scale(1.2)!important'
          },
        },
        {
          ele: "",
          eleRect: {} as DOMRect,
          img: getImage("knowledge2.png"),
          handStyle: {
            top: "1.3rem",
            left:'1.5rem'
          },
          imgStyle: {
            top: "1.9rem",
            left:'-1.5rem'
          },
          btnsStyle: {
            top: "4.5rem",

          },
          boxStyle:{
            transform: 'scale(1.2)!important'
          },
        },
        {
          ele: "",
          img: getImage("knowledge3.png"),
          handStyle: {
            top: "4.1rem",
            left:'3.6rem',

          },
          imgStyle: {
            top: "4.8rem",
            left:'1.2rem',
            width:'7.2rem',
            height:'3.45rem'
          },
          btnsStyle: {
            top: "8.8rem",
            left:'1.3rem',
          },
          boxStyle:{
            transform: 'scale(1)'
          },
        },
      ],
      step: 0,
    });
    const tipShow = ref(false)

    const getStepELe = () => {

      const ele: HTMLElement = document.getElementById(`knowledge-${data.step}`) as any;
      console.log(ele,'ele')
      if (ele) {
        const eleRect = ele.getBoundingClientRect();
        data.box = {
          left: eleRect.x + "px",
          top: eleRect.y + "px",
          width: eleRect.width + "px",
          height: eleRect.height + "px",
        };
      }else{
        handleNext()
      }
    };
    onMounted(() => {

        const guideInfo = localStorage.getItem('guideInfo')
        if(guideInfo&&JSON.parse(guideInfo).knowledge){
         tipShow.value =false
        }else {
         tipShow.value =true
        }
        getStepELe();

      window.addEventListener("resize", resetSize);
    });
    const resetSize = ()=>{
      getStepELe();
    }

    onUnmounted(()=>{
      window.removeEventListener("resize", resetSize);
    })
    const handleNext = () => {
      console.log(data.step,'')
      if (data.step >= 2) {
       endGuide();
        return;
      }
      data.step = data.step + 1;
      getStepELe();
    };

    const endGuide = ()=>{
      let guideInfo = JSON.parse(localStorage.getItem('guideInfo') || '{}')

      if(!guideInfo){
        guideInfo = {knowledge:true}
      }else{
        guideInfo.knowledge = true
      }
      localStorage.setItem('guideInfo',JSON.stringify(guideInfo))
      tipShow.value = false
    //  localStorage.setItem('endC')
    }
    return () => (
      <Popup teleport="body" overlay={false} closeOnClickOverlay={false} class={["popup-custom", styles.guidePopup]} v-model:show={tipShow.value}>
      <div class={styles.content} onClick={() => handleNext()}>
      {data.step!=data.steps.length-1&&<div
          class={[styles.backBtn,styles.right]}
          onClick={(e: Event) => {
            e.stopPropagation();
           endGuide()
          }}
        >

          跳过
        </div>}
        <div class={[styles.box,styles.noscalc]} style={{...data.box,...data.steps[data.step].boxStyle}} id={`modeType-${data.step}`}>
          {data.steps.map((item: any, index) => (

            <div
              onClick={(e: Event) => e.stopPropagation()}
              class={styles.item}
              style={{
                display: index === data.step ? "" : "none",
                left: `${item.eleRect?.left}px`,
                top: `${item.eleRect?.top}px`,
              }}
            >
              <img class={styles.img} style={item.imgStyle} src={item.img} />
              <img class={styles.iconHead} style={item.handStyle} src={getImage("indexDot.png")} />
              <div class={styles.btns} style={item.btnsStyle}>
                {data.step + 1 == data.steps.length ? (
                  <>
                    <Button
                      class={styles.btn}
                      round
                      color="transparent"
                      style={{ "border-color": "#fff" }}
                      type="primary"
                      onClick={() => {
                        data.step = 0;
                        getStepELe();
                      }}
                    >
                      再看一遍
                    </Button>
                    <Button class={[styles.btn,styles.endBtn]} round type="primary" onClick={() =>endGuide()}>
                      完成
                    </Button>
                  </>
                ) : (
                  <Button class={styles.btn} round type="primary" onClick={() => handleNext()}>

                    下一步 ({data.step + 1}/{data.steps.length})
                  </Button>
                )}
              </div>
            </div>
          ))}
        </div>
      </div>
      </Popup>
    );
  },
});
