import { Component } from "react";
import { View, Image, Button, getCurrentInstance } from "@tarojs/components";
import Taro from "@tarojs/taro";
import { AtCheckbox } from "taro-ui";
import { login, getUserInfo, bindShareJump } from "../../service";
import { objToParam } from "../../common/toParams";
import "./index.less";
import logo1 from "../../images/logo1.png";
export default class Index extends Component {
  state = {
    isChecked: false,
  };
  // 页面卸载
  componentWillUnmount() {
    Taro.removeStorageSync("currentPage");
    global.globalData.optionsData = {};
  }
  // 同意协议
  handleCheckboxChange = (value) => {
    this.setState({
      isChecked: value.length > 0,
    });
  };
  // 同意协议
  handleAgree = () => {
    Taro.navigateTo({
      url: "/pages/mineSub/userSpecification/index",
    });
  };

  // handleGetPhoneNumber = (e) => {
  //   if (e.detail.errMsg === "getPhoneNumber:ok") {
  //     // 处理获取到的手机号信息
  //     console.log("用户手机号信息：", e);
  //     // 你可以在这里调用后端接口，将手机号信息发送到服务器
  //   } else {
  //     console.log("用户拒绝授权获取手机号");
  //   }
  // };
  login = () => {
    Taro.showLoading({
      title: "正在登录",
    });
    if (!this.state.isChecked) {
      Taro.showToast({
        title: "请先同意协议",
        icon: "none",
      });
      return;
    }
    Taro.getUserProfile({
      desc: "获取用户信息",
      success: async (userInfo) => {
        const loginRes = await Taro.login();
        const res = await login({
          code: loginRes.code,
          head_img: userInfo.avatarUrl,
          name: userInfo.nickName,
        });

        Taro.setStorageSync("session_key", res.session_key);
        Taro.setStorageSync("loginInfo", res);
        Taro.hideLoading();
        Taro.showToast({
          title: "登录成功",
          icon: "none",
        });

        // 获取用户信息
        getUserInfo().then(userInfoRes => {
          Taro.setStorageSync("userInfo", userInfoRes);
        });
        try {
          // 先处理分享跳转
          let shareJump = Taro.getStorageSync("shareJump");
          let shareType = Taro.getStorageSync("shareType");
          if (shareJump) {
            await bindShareJump({
              share_unique_value: shareJump,
              share_type: shareType || 2,
            });
            Taro.removeStorageSync("shareJump");
            Taro.removeStorageSync("shareType");
            if (global.globalData.optionsData) {
              delete global.globalData.optionsData.shareJump;
            }
          }
        } finally {
          // 最后处理页面跳转
          let lastPage = Taro.getStorageSync("currentPage");
          if (!lastPage || lastPage === "pages/mineSub/login/index") {
            lastPage = "pages/index/index";
          }
          await Taro.reLaunch({
            url: `/${lastPage}?${objToParam(global.globalData.optionsData)}`,
          });
          Taro.removeStorageSync("currentPage");
          global.globalData.optionsData = {};
        }
      },
    });
  };
  render() {
    const checkboxOption = [
      {
        value: "agree",
        label: "",
      },
    ];

    return (
      <View className="index">
        <View className="login-content">
          <Image src={logo1} className="logo" />
          <View className="title">鱼市</View>
          <View className="desc">
            您暂时未获取微信授权，将无法正常使用小程序的功能。请先阅读且同意《鱼市平台用户协议》后再授权登录。
          </View>
          {/* <Button open-type="getPhoneNumber" onGetPhoneNumber={this.handleGetPhoneNumber} className="login-button">授权登录</Button> */}
          <View className="checkbox-login-container">
            <AtCheckbox
              options={checkboxOption}
              selectedList={this.state.isChecked ? ["agree"] : []}
              onChange={this.handleCheckboxChange}
            />
            <View onClick={this.handleAgree} className="checkbox-text">
              同意《鱼市平台用户协议》
            </View>
          </View>

          <Button
            className="login-button"
            // openType={this.state.isChecked ? "getPhoneNumber" : null}
            onClick={this.login}
          >
            授权登录
          </Button>
        </View>
        <Image src='https://yushi.tos-cn-beijing.volces.com/login/loginBg.png' className="login-bg" />
      </View>
    );
  }
}
