import React, { useEffect, useState } from 'react'
import { OrderForGoodsBox } from './styled'
import type { FormInstance, RadioChangeEvent } from 'antd'
import { Button, Radio, Space, Input, message } from 'antd'
import api, { UserAddress } from './api'
import moment from 'moment'
import { ShopDetail } from '../../[id].page'
import { GetWebDeviceDetailResult } from '../../api'
const { TextArea } = Input

interface PropsBox {
  setIsorderForGoods: (boolean: boolean) => void
  detailData?: GetWebDeviceDetailResult | null
  days?: number
  shopDetail?: ShopDetail
}

export default function OrderForGoods(props: PropsBox) {
  const { setIsorderForGoods, shopDetail, days, detailData } = props
  const [value, setValue] = useState(1)
  const [areaValue, setAreaValue] = useState<string>()
  const [list, setList] = useState<Array<UserAddress> | null>()
  const [detail, setDetail] = useState<ShopDetail>()
  const onChange = (e: RadioChangeEvent) => {
    console.log('radio checked', e.target.value)
    setValue(e.target.value)
  }
  const onChangeValue = (index: number) => {
    setValue(index)
  }
  const detailSumbit = () => {
    message.success('提交成功')
    console.log(shopDetail)
    console.log(list![value])
    console.log(areaValue)
    console.log(detailData)

    console.log(list![value].takeAddress)
    console.log(list![value].takeRegion)
    console.log(list![value].takeName)
    console.log(Number(list![value].takePhone))

    if (detailData && shopDetail && list) {
      const pushList = {
        actualPay: shopDetail.num * detailData.price!,
        deposit: 0,
        endDate: moment(new Date(shopDetail.dateDetail[1])).format(
          'YYYY-MM-DD'
        ),
        orderReceipt: {
          detailAddress: list[value].takeAddress,
          receiptMethod: 0,
          region: list[value].takeRegion,
          takeName: list[value].takeName,
          takePhone: Number(list[value].takePhone),
        },
        rentPrice: shopDetail.num * detailData.price!,
        returnDate: moment(new Date(shopDetail.dateDetail[1])).format(
          'YYYY-MM-DD'
        ),
        shouldPay: shopDetail.num * detailData.price!,
        specsId: shopDetail.id,
        startDate: moment(new Date(shopDetail.dateDetail[0]!)).format(
          'YYYY-MM-DD'
        ),
        wareDescription: detailData.goodsName,
        wareImg: detailData.images[0].imgUrl,
        wareInfoId: shopDetail.id,
        wareNo: detailData.goodsNo,
        wareNum: shopDetail.num,
        wareTitle: detailData.goodsName,
        remark: areaValue,
      }

      api.FeignAddLease(pushList).then((res) => {
        console.log(res)
      })
    }
    // setIsorderForGoods(false)
  }

  useEffect(() => {
    console.log(days)

    api
      .listUserAddress({})
      .then((res) => {
        console.log(res)
        setList(res.result)
        res.result?.map((item, index) => {
          if (item.type === 0) {
            setValue(index)
          }
        })
      })
      .catch((err) => {
        console.log(err)
      })
    if (shopDetail) {
      setDetail(shopDetail)
    }
  }, [])
  return (
    <OrderForGoodsBox>
      <div className="address">
        <div className="top">
          <div className="left">确认收货地址</div>
          <div className="right">
            <Button type="link" style={{ color: '#007aff' }}>
              管理收货地址
            </Button>
          </div>
        </div>
        <div className="bottom">
          {list?.map((item, index) => (
            <div
              key={item.id}
              className={`item ${value === index ? 'active' : ''}`}
              onClick={() => onChangeValue(index)}
            >
              <div className="left">
                <div className="active">
                  <div className="icon"></div>
                  <div className="label">寄送至</div>
                </div>
                <Radio.Group onChange={onChange} value={value}>
                  <Space direction="vertical">
                    <Radio value={index}>{item.takeAddress}</Radio>
                  </Space>
                </Radio.Group>
              </div>
              {value === index ? (
                <div className="right">
                  <Button type="link" style={{ color: '#007aff' }}>
                    修改地址
                  </Button>
                </div>
              ) : null}
            </div>
          ))}
        </div>
      </div>
      <div className="info">
        <div className="title">确认订单信息</div>
        <div className="table">
          <div className="table-title">
            <div className="table-item" style={{ width: 290 }}>
              宝贝
            </div>
            <div className="table-item" style={{ width: 130 }}>
              单价
            </div>
            <div className="table-item" style={{ width: 130 }}>
              数量
            </div>
            <div className="table-item" style={{ width: 300 }}>
              租期
            </div>
            <div className="table-item" style={{ width: 135 }}>
              合计
            </div>
          </div>
          <div className="table-body">
            <div className="body-item article" style={{ width: 290 }}>
              <div className="image"></div>
              <div className="right">
                <div className="top">
                  智多星航电版智多星航电版智多星航电版智多
                </div>
                <div className="bottom">
                  商品简介商品简介商品简介商品简介商品简介
                </div>
              </div>
            </div>
            <div className="body-item" style={{ width: 130 }}>
              800.00/天
            </div>
            <div className="body-item" style={{ width: 130 }}>
              1
            </div>
            <div className="body-item lease-term" style={{ width: 300 }}>
              {moment(new Date(shopDetail?.dateDetail[0]!)).format(
                'YYYY/MM/DD'
              )}
              <div className="num">{days}天</div>
              {moment(new Date(shopDetail?.dateDetail[1]!)).format(
                'YYYY/MM/DD'
              )}
            </div>
            <div className="body-item total-price" style={{ width: 135 }}>
              800.00
            </div>
          </div>
        </div>
      </div>
      <div className="notes">
        <div className="left">
          <div className="label">备注：</div>
          <TextArea
            value={areaValue}
            onChange={(e) => setAreaValue(e.target.value)}
            placeholder="请输入备注"
            autoSize={{ minRows: 3, maxRows: 5 }}
            style={{ width: 385, height: 72 }}
          />
        </div>
        <div className="right">
          <div className="top">
            <div className="font">
              <div className="label">运费：</div>
              <div className="value">邮寄到付,由客户自己承担</div>
            </div>
            <div className="price">200.00</div>
          </div>
          <div className="bottom">
            <div className="font">
              <div className="label">押金：</div>
              <div className="value">渠道商可免押金</div>
            </div>
            <div className="price">1000.00</div>
          </div>
        </div>
      </div>
      <div className="detail-box">
        <div className="right-box">
          <div className="detail">
            <div className="top">
              <div className="label">实付款</div>
              <div className="price">¥20000.00</div>
            </div>
            <div className="bottom">
              <div className="value">寄送至</div>
              <div className="value-content">
                广东省深圳市南山区国际创谷6栋国际创谷6
              </div>
            </div>
          </div>
          <div className="detail-sumbit">
            <Button className="btn" onClick={detailSumbit}>
              提交订单
            </Button>
          </div>
        </div>
      </div>
    </OrderForGoodsBox>
  )
}
