import Popup from '@/components/popup'
import React, { useState, memo, useEffect } from "react";
import Taro from "@tarojs/taro";
import { View } from "@tarojs/components";
// import { AtIcon } from 'taro-ui'
import './index.scss'




interface Props {
    showTime: true | false,
    closePopup?: () => void
}
export default memo((props: Props) => {
    const {
        showTime = false,
        closePopup
    } = props
    //每月多少天
    let MONTH_DAYS = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    const WEEK_NAMES = ['日', '一', '二', '三', '四', '五', '六'];
    const LINES = [1, 2, 3, 4, 5, 6];
    const [year, setLoinsYear] = useState(0);
    let nowTime = new Date(Date.parse(new Date().toString()));
    const [month, seLoinstMonth] = useState(nowTime.getMonth());
    const [currentDate, setcurrentDate] = useState(new Date());
    const [tag, setTag] = useState(false);
    //获取当前月份
    const getMonth = (date: Date): number => {
        return date.getMonth();
    }
    //获取当前年份
    const getFullYear = (date: Date): number => {
        // console.log(date.getFullYear())
        return date.getFullYear();
    }

    const getCurrentMonthDays = (month: number, year: number): number => {
        let _year = year + currentDate.getFullYear();
        if (_year % 100 != 0 && _year % 4 == 0 || _year % 400 == 0) {
            MONTH_DAYS = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        }
        return MONTH_DAYS[month]
    }
    //当前月第一天是周几
    const getDateByYearMonth = (year: number, month: number, day: number = 1): Date => {
        var date = new Date()
        date.setFullYear(year)
        date.setMonth(month, day)
        return date
    }
    const getWeeksByFirstDay = (year: number, month: number): number => {
        var date = getDateByYearMonth(year, month)
        return date.getDay()
    }
    const getDayText = (line: number, weekIndex: number, weekDay: number, monthDays: number): any => {
        var number = line * 7 + weekIndex - weekDay + 1
        if (number <= 0 || number > monthDays) {
            return <View className="day-c" key={weekIndex}>&nbsp;</View>
        }
        return <View className="day-c" key={weekIndex}>
            <View className="day" onClick={() => clickItem(line)}>{number}</View>
        </View>
    }

    const setCurrentYearMonth = (date) => {
        var month = getMonth(date)
        var year = getFullYear(date)
        setLoinsYear(year);
        seLoinstMonth(month)
        setTag(false)
    }

    const monthChange = (monthChanged: number) => {
        if (tag) {
            return;
        } else {
            setTag(true)
        }

        var monthAfter = month + monthChanged
        var date = getDateByYearMonth(year, monthAfter)
        setCurrentYearMonth(date)
    }
    const formatNumber = (num: number): string => {
        var _num = num + 1
        return _num < 10 ? `0${_num}` : `${_num}`
    }

    // let monthDays = getCurrentMonthDays(month);
    let weekDay = getWeeksByFirstDay(year, month);

    let _startX = 0;
    const clickItem = (item) => {
        console.log(item, 666)
    }
    return (
        <Popup title={'选择时间'} show={showTime} onClose={() => closePopup?.()}>
            <View className="loins-calendar"
                onTouchEnd={(val) => {
                    if (_startX > val.changedTouches[0]['clientX'] + 30) {
                        monthChange(1);
                    }
                    if (_startX < val.changedTouches[0]['clientX'] - 30) {
                        monthChange(-1);
                    }
                }} onTouchStart={(val) => {
                    _startX = val.changedTouches[0]['clientX']

                }}
            >
                <View className="loins-calendar-tabbar">
                    <View>
                        {/* <AtIcon value='chevron-left' size='30' color='#297AF8' onClick={() => {
                            monthChange(-1);
                        }}>
                        </AtIcon> */}
                    </View>
                    <View className="loins-calendar-title">{year + currentDate.getFullYear()} 年 {formatNumber(month)}月</View>
                    <View>
                        {/* <AtIcon value='chevron-right' size='30' color='#297AF8' onClick={() => {
                            monthChange(1);
                        }}></AtIcon> */}
                    </View>
                </View>
                {
                    WEEK_NAMES.map((week, key) => {
                        return <View className="title-c" key={key}>{week}</View>
                    })
                }
                {
                    LINES.map((l, key) => {
                        return <View key={key} className="day-content" >
                            {
                                WEEK_NAMES.map((week, index) => {
                                    return getDayText(key, index, weekDay, getCurrentMonthDays(month, year))
                                })
                            }
                        </View>
                    })
                }
            </View>
        </Popup>
    )
})