import classnames from "classnames";
import Taro from "@tarojs/taro";
import { Component } from "react";

import {
  Block,
  View,
  Image,
  Text,
  Button,
  PageMeta,
  Swiper,
  SwiperItem,
} from "@tarojs/components";

import { Popup } from "@antmjs/vantui";

import "./popup.less";
import "./popup-instrument-upload-tips.less";
import "./fade.css";

import { go, goJump } from "../../utils/traoAPI";

/*** props
 * isLarge            是否大尺寸
 * isShow             是否显示
 * data               数据
 * @confirm           关闭回调
 * ***/
export default class PopupInstrumentUploadTips extends Component<any, any> {
  constructor(props) {
    super(props);
    this.state = {
      name: "打卡介绍弹窗",
      current: 0,
      toRight: false,
      isClick: false,
    };
  }

  async onLoad() {}
  componentDidMount() {}

  componentWillUnmount() {}

  componentDidShow() {}

  componentDidHide() {}

  initData = () => {
    if (this.props.siteData) {
      let arr = JSON.parse(JSON.stringify(this.props.siteData));
      this.setState({ siteList: arr });
    }
  };

  onClose = () => {
    this.props.close();
  };

  onClickStop = (e) => {
    e.stopPropagation();
  };

  onChange(event) {
    const current = event.detail.current;
    const { current: curCurrent } = this.state;
    const { siteData } = this.props;
    if (curCurrent + 2 === siteData.length && current + 1 === siteData.length) {
      this.setState({ toRight: true });
    }
    this.setState({ current });
  }
  onClickSwiperItem(item) {
    goJump(item);
  }
  onFinish(event) {
    const { current } = event.detail;
    const { toRight, isClick } = this.state;
    const { siteData } = this.props;
    if (current === siteData.length - 1) {
      if (toRight || isClick) {
        this.setState({ toRight: false, isClick: false });
      } else {
        // this.toHomePage();
      }
    }
  }

  onPrev = async () => {
    let { current } = this.state;
    this.setState({ current: current - 1 });
  };
  onNext = async () => {
    let { current } = this.state;

    if (current === this.props.data.length) {
      this.onClose();
    } else {
      this.setState({ current: current + 1 });
    }
  };

  render() {
    let { isShow, data, isLarge, title } = this.props;
    let { current } = this.state;

    return (
      <Block>
        <PageMeta pageStyle={isShow ? "overflow: hidden;" : ""} />
        <Popup
          style="background-color: #fff;"
          show={isShow}
          closeOnClickOverlay={false}
          round
          overlayStyle="width: 100vw;padding: 0;"
          onClick={this.onClickStop}
        >
          <View
            className="at-icon at-icon-close site-close"
            onClick={this.onClose}
          ></View>
          <View className={classnames("common-box upload-tips-common-box")}>
            <View className="site-popup-content-box">
              <Swiper
                className="absolutely"
                style="width: 600rpx;height: 720rpx"
                current={current}
                onChange={this.onChange.bind(this)}
                onAnimationFinish={this.onFinish.bind(this)}
                duration={800}
                indicatorDots={false}
                indicatorColor="#999"
                indicatorActiveColor="#333"
                disableTouch={true} // 禁止滑动
              >
                {data.map((item, index) => {
                  return (
                    <SwiperItem
                      className="absolutely"
                      onClick={this.onClickSwiperItem.bind(this, item)}
                      key={"swiper_" + index}
                    >
                      <View
                        style={{
                          width: "100%",
                          height: "600rpx",
                        }}
                      >
                        <Image
                          className="cover"
                          src={item.file}
                          mode="aspectFit"
                        ></Image>
                      </View>
                      <View className="tips-message">{item.message}</View>
                    </SwiperItem>
                  );
                })}
              </Swiper>
            </View>

            <View>
              {data.length === 1 && (
                <View className="popup-btn-one">
                  <Button className="popup-btn" onClick={this.onClose}>
                    知道了
                  </Button>
                </View>
              )}
              {data.length > 1 && current === 0 && (
                <View className="popup-btn-one">
                  <Button className="popup-btn" onClick={this.onPrev}>
                    下一步
                  </Button>
                </View>
              )}
              {data.length > 1 && (
                <View className="common-popup-btns">
                  <Button className="common-popup-btn2" onClick={this.onPrev}>
                    {current > 0 && current < data.length - 1 && "上一步"}
                  </Button>
                  <Button className="common-popup-btn2" onClick={this.onNext}>
                    {current > 0 && current < data.length - 1 && "下一步"}
                    {current > 0 && current === data.length - 1 && "知道了"}
                  </Button>
                </View>
              )}
            </View>
          </View>
        </Popup>
      </Block>
    );
  }
}
