import { Button, Popup } from "vant";
import { } from "vant";
import { defineComponent, nextTick, onMounted, reactive, ref, watch } from "vue";
import styles from "./index.module.less";
import { getImage } from "./images";
import { getQuery } from "/src/utils/queryString";
import {getGuidance,setGuidance} from './api'
import state, { IPlatform } from "/src/state";

export default defineComponent({
  name: "teacherTop-guide",
  emits: ["close"],
  setup(props, { emit }) {
    const data = reactive({
      box: {},
      show: false,
      steps: state.platform === IPlatform.PC ? [
        {
          ele: "",
          eleRect: {} as DOMRect,
          img: getImage("pc_teacherBottom1.png"),
          handStyle: {
            top: "0.91rem",
          },
          imgStyle: {
            bottom: "2.5rem",
            width:'3.63rem',
            height:'2.28rem',
            left:'-0.45rem'
          },
          btnsStyle: {
            bottom: "2.72rem",
            left:'1.1rem',
            transform: "scale(.83)",

          },
        },
        {
          ele: "",
          img: getImage("pc_teacherBottom2.png"),
          handStyle: {
            top: "-1.39rem",
            left:'0.15rem',
            transform: 'rotate(180deg)'
          },
          imgStyle: {
            bottom: "2.5rem",
            width:'4.20rem',
            height:'2.28rem',
            left:'-0.45rem'
          },
          btnsStyle: {
            bottom: "2.72rem",
            left:'1.1rem',
            transform: "scale(.83)",
          },
        },
        {
          ele: "",
          img: getImage("pc_teacherBottom3.png"),
          handStyle: {
            top: "-1.39rem",
            left:'0.17rem',
            transform: 'rotate(180deg)'
          },
          imgStyle: {
            bottom: "2.5rem",
            width:'5.15rem',
            height:'2.28rem',
            left:'-0.45rem'
          },
          btnsStyle: {
            bottom: "2.72rem",
            left:'1.1rem',
            transform: "scale(.83)",
          },
        },
        {
          ele: "",
          img: getImage("pc_teacherBottom4.png"),
          handStyle: {
            top: "-1.39rem",
            left:'1.4rem',
            transform: 'rotate(180deg)'
          },
          imgStyle: {
            bottom: "2.5rem",
            width:'4.39rem',
            height:'2.28rem',
            left:'-0.45rem'
          },
          btnsStyle: {
            bottom: "2.72rem",
            left:'1.1rem',
            transform: "scale(.83)",
          },
        },
        {
          ele: "",
          img: getImage("pc_teacherBottom5.png"),
          handStyle: {
            top: "-1.39rem",
            left:'1.4rem',
            transform: 'rotate(180deg)'
          },
          imgStyle: {
            bottom: "2.5rem",
            width:'4.58rem',
            height:'2.28rem',
            left:'-0.45rem'
          },
          btnsStyle: {
            bottom: "2.72rem",
            left:'1.1rem',
            transform: "scale(.83)",
          },
        },
        {
          ele: "",
          img: getImage("pc_teacherBottom6.png"),
          handStyle: {
            top: "-1.39rem",
            left:'1.4rem',
            transform: 'rotate(180deg)'
          },
          imgStyle: {
            bottom: "2.5rem",
            width:'4.01rem',
            height:'2.28rem',
            left:'-3.4rem'
          },
          btnsStyle: {
            bottom: "2.72rem",
            left:'-2.1rem',
            transform: "scale(.83)",
          },
        },
      ] : [
        {
          ele: "",
          eleRect: {} as DOMRect,
          img: getImage("teacherTop1.png"),
          handStyle: {
            top: "0.91rem",
          },
          imgStyle: {
            top: "1.32rem",
            width:'3.63rem',
            height:'2.28rem',
            left:'-2rem'
          },
          btnsStyle: {
            top: "2.8rem",
            left:'-0.5rem',
            transform: "scale(.83)",

          },
        },
        {
          ele: "",
          img: getImage("teacherTop2.png"),
          handStyle: {
            top: "-1.39rem",
            left:'0.15rem',
            transform: 'rotate(180deg)'
          },
          imgStyle: {
            top: "1.32rem",
            width:'4.20rem',
            height:'2.28rem',
            left:'-2rem'
          },
          btnsStyle: {
            top: "2.8rem",
            left:'-0.5rem',
            transform: "scale(.83)",
          },
        },
        {
          ele: "",
          img: getImage("teacherTop3.png"),
          handStyle: {
            top: "-1.39rem",
            left:'0.17rem',
            transform: 'rotate(180deg)'
          },
          imgStyle: {
            top: "1.32rem",
            width:'5.15rem',
            height:'2.28rem',
            left:'-2rem'
          },
          btnsStyle: {
            top: "2.8rem",
            left:'-0.5rem',
            transform: "scale(.83)",
          },
        },
        {
          ele: "",
          img: getImage("teacherTop4.png"),
          handStyle: {
            top: "-1.39rem",
            left:'1.4rem',
            transform: 'rotate(180deg)'
          },
          imgStyle: {
            top: "1.32rem",
            width:'4.39rem',
            height:'2.28rem',
            left:'-2rem'
          },
          btnsStyle: {
            top: "2.8rem",
            left:'-0.5rem',
            transform: "scale(.83)",
          },
        },
        {
          ele: "",
          img: getImage("teacherTop5.png"),
          handStyle: {
            top: "-1.39rem",
            left:'1.4rem',
            transform: 'rotate(180deg)'
          },
          imgStyle: {
            top: "1.32rem",
            width:'4.58rem',
            height:'2.28rem',
            left:'-2rem'
          },
          btnsStyle: {
            top: "2.8rem",
            left:'-0.5rem',
            transform: "scale(.83)",
          },
        },
        {
          ele: "",
          img: getImage("teacherTop6.png"),
          handStyle: {
            top: "-1.39rem",
            left:'1.4rem',
            transform: 'rotate(180deg)'
          },
          imgStyle: {
            top: "1.32rem",
            width:'4.01rem',
            height:'2.28rem',
            left:'-3.4rem'
          },
          btnsStyle: {
            top: "2.8rem",
            left:'-2.1rem',
            transform: "scale(.83)",
          },
        },
      ],
      step: 0,
    });
    const tipShow = ref(false)
    const query: any = getQuery();
  //  const guideInfo = localStorage.getItem('guideInfo' || !query.showGuide)
  //  if(guideInfo&&JSON.parse(guideInfo).teacherTop){
  //   tipShow.value =false
  //  }else {
  //   tipShow.value =true
  //  }
  const guideInfo = ref({} as any)

    const getStepELe = () => {
      console.log(`teacherTop${data.step}`)
      const ele: HTMLElement = document.getElementById(`teacherTop-${data.step}`)!;
      if (ele) {
        if (ele.style.display === 'none'){
					handleNext()
					return
				}
        const eleRect = ele.getBoundingClientRect();
        data.box = {
          left: eleRect.x + "px",
          top: eleRect.y + "px",
          width: eleRect.width + "px",
          height: eleRect.height + "px",
        };
      }
    };
    onMounted(() => {
      // 底部栏有个动画过度效果，位置会移动，需要加个延迟时间计算其移动后的位置
      setTimeout(() => {
        getStepELe();
        // data.initDone = true;
      }, 1200);
    });


    const handleNext = () => {
      if (data.step >= 5) {
       endGuide();
        return;
      }
      data.step = data.step + 1;
      getStepELe();
    };
    
    const endGuide = async()=>{
      // let guideInfo = JSON.parse(localStorage.getItem('guideInfo') || '{}') || null
      if(!guideInfo.value){
        guideInfo.value = {teacherTop:true}
      }else{
        guideInfo.value.teacherTop = true
      }
      try{
				const res = await setGuidance({guideTag:'guideInfo',guideValue:JSON.stringify(guideInfo.value)})
			  }catch(e){
				console.log(e)
			  }
      // localStorage.setItem('guideInfo',JSON.stringify(guideInfo))
      state.guideInfo = guideInfo.value;
      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()}>
        <div
          class={styles.backBtn}
          onClick={(e: Event) => {
            e.stopPropagation();
           endGuide()
          }}
        >
          跳过
        </div>
        <div class={styles.box} style={data.box} 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 ? (
                  <>
                      <div class={[styles.endBtn]}  onClick={() =>endGuide()}>
                      完成
                    </div>
                    <div
                      class={[styles.nextBtn]}
                      style={{ "border-color": "#fff" }}
                  
                      onClick={() => {
                        data.step = 0;
                        getStepELe();
                      }}
                    >
                      再看一遍
                    </div>
                
                  </>
                ) : (
                  <Button class={styles.teacherBtn} round type="primary" onClick={() => handleNext()}>

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