import Buttons from "@/components/Buttons";
import { setPlan } from "@/services/trackTimeDuration";
import { View, Text } from "@tarojs/components";
import { Picker } from '@tarojs/components'
import Taro from "@tarojs/taro";
// import { AtList, AtListItem } from 'taro-ui'
import { useState } from "react";
import { useSelector } from "react-redux";

export default function Component(props: { scenario: any }) {
  const scenario = useSelector((state: any) => state.scenario);
  var scheduleObj: { start_time: any; end_time: any; };
  if (scenario.scenario == 'FAST') {
    scheduleObj = props.scenario.schedule.fast
  }
  else if (scenario.scenario == 'SLEEP') {
    scheduleObj = props.scenario.schedule.sleep
  }
  else {
    if (scenario.schedule) {
      scheduleObj = props.scenario.schedule.sleep
    }
    else {
      scheduleObj = props.scenario.schedule.fast
    }
  }

  const [startTime, setStartTime] = useState(scheduleObj.start_time)
  const [endTime, setEndTime] = useState(scheduleObj.end_time)

  function start() {
    if (scenario.scenario == 'FAST' || scenario.scenario == 'SLEEP') {
      setPlan({
        scenario: scenario.scenario,
        schedule: scenario.scenario == 'FAST' ? {
          fast: {
            start_time: startTime,
            end_time: endTime,
          }
        } : {
          sleep: {
            start_time: startTime,
            end_time: endTime,
          }
        }
      }).then(res => {
        Taro.navigateBack({ delta: 3 })
        // console.log('success')
      })
    }
    else {

    }
  }

  function onStartTimeChange(e: any) {
    setStartTime(e.detail.value)
  }

  function onEndTimeChange(e: any) {
    setEndTime(e.detail.value)
  }

  return <View >
    <Text>hello world</Text>
    <Picker mode='time' value={startTime} onChange={onStartTimeChange}>
      <Text>开始时间{startTime}</Text>
      {/* <AtList>
        <AtListItem title='请选择时间' extraText={timeSel} />
      </AtList> */}
    </Picker>

    <Picker mode='time' value={endTime} onChange={onEndTimeChange}>
      <Text>结束时间{endTime}</Text>
      {/* <AtList>
        <AtListItem title='请选择时间' extraText={timeSel} />
      </AtList> */}
    </Picker>
    <Buttons title="Start" onClick={() => start()}></Buttons>
  </View>;
}