import { View, Input } from '@tarojs/components'
import Taro,{useReady} from "@tarojs/taro"
import React,{useState,useEffect} from 'react'
const Weather =(date)=> {
  const [futureli,setFutureli]=useState([])
  const [today,setToday]=useState([])
  const [success,setSuccess]=useState(false)
  const [time,setTime]=useState('')
  const weather=(a)=> {
    Taro.request({
      url: a.url, //仅为示例，并非真实的接口地址
      data: {
        app: 'weather.'+a.type,
        //cityId:"101210101",
        //citynm:'北京',
        weaId: a.weaId,
        appkey: '10003',
        sign: 'b59bc3ef6191eb9f747dd4e83c99f2a4',
        format: 'json'
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        //console.log(res.data.result)
        if(a.type==='future')
        {
          setFutureli(res.data.result);

        }
          else if(a.type==='today')
          {
            //console.log(res.data.result);
            setToday(res.data.result);
          }
      }
    })
  }
  const getTime=()=>{
    var time = new Date();
    var h =time.getHours(); //h用来放时
    h = h < 10? '0' + h:h;
    var m = time.getMinutes();//m用来放分
    m= m<10?'0'+m:m;
    //console.log(time.getMonth()+1+'月'+time.getDate()+'日'+' '+h + ':' + m);
    setTime(time.getMonth()+1+'月'+time.getDate()+'日'+' '+h + ':' + m)
}
const intoarea=(a)=>{
  if(typeof(a)==='string')
  return a.split('℃')[1].replace('/','')+'~'+ a.split('℃')[0]+'℃'

}
  useReady(() => {
    weather({url:'https://sapi.k780.com',weaId:date.props.weaId,type:'future'});
    weather({url:'https://sapi.k780.com',weaId:date.props.weaId,type:'today'});
    setSuccess(true)
    setInterval(getTime,1000);
   })
  //  useEffect(()=>{
  //   setInterval(getTime,1000);
  //  },[])
  return (
    <View className='weather'>
      <View className="dev">
        <View className="title">气象数据</View>
        <View className="address">{today.citynm}市</View>
      </View>
      <View className="today">
        <View className="temp_curr">{today.temp_curr}</View>
        <View className="todaydate">
         <View className="du">℃</View>
         <View className="sun">{today.weather}</View>
         <View className="date">{time}</View>
         <View className="temparea">{intoarea(today.temperature)}</View>
        </View>

      </View>
      <View className="futuretitle">未来7天天气预报</View>
      {
        futureli.map((item)=>{
          return (
            <View className="futurediv">
              <View className='data'>{item.days.split('-')[1]}月{item.days.split('-')[2]}日</View>
              <View className="wea zhui">{item.weather}</View>
              <View className="temp">{intoarea(item.temperature)}</View>
            </View>
          )
        })
      }
    </View>
  )
}
export default Weather