import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_tzh/home/view/YSInputView.dart';
import 'package:flutter_tzh/tool/YSChooseView.dart';
import 'package:flutter_tzh/tool/YSNetWork.dart';

import '../base/YSBase.dart';
import '../tool/YSTools.dart';

class YSHomeListDetailEmail extends StatefulWidget {
  final String planNum;
  const YSHomeListDetailEmail({Key? key, required this.planNum}) : super(key: key);

  @override
  YSHomeListDetailEmailState createState() => YSHomeListDetailEmailState();
}

class YSHomeListDetailEmailState extends State<YSHomeListDetailEmail> {
  final List _dataArray = [
    {'title':'收件地址','array':[
      {'title':'收件人','key':'name'},
      {'title':'电    话','key':'phone'},
      {'title':'地    址','key':'address'}
    ]},
    {'title':'快递单号','array':[
      {'title':'快递公司','type':3,'key':'express'},
      {'title':'快递单号','type':1,'key':'expressCode'}
    ]},
    {'title':'备注信息','array':[
      {'title':'备注信息','type':2,'key':'remark'},
    ]},
  ];
  final List _expressList = [];
  @override
  void initState() {
    networkDelay((){
      _getDeliveryIndex();
    });
    super.initState();
  }
  _getDeliveryIndex() async{
    YSNetWork.ysRequestHttp(context, type: RequestType.get, api: '/delivery/index', parameter: {}, successSetter: (dict){
      Map data = dict['data']??{};
      Map item0 = _dataArray.firstWhere((element) => element['title']=='收件地址');
      List array0 = item0['array']??[];
      for (var element in array0) {
        element['value'] = data['delivery'][element['key']];
      }
      List expressList = data['expressList']??[];
      _expressList.clear();
      for (var element in expressList) {
        _expressList.add({'label':element});
      }
      setState(() {});
    });
  }
  @override
  Widget build(BuildContext context) {
    return YSBase(
      ysTitle: '邮寄',
      ysChild: SingleChildScrollView(
        child: Column(
          children: [
            SizedBox(
              height: ysHeight(context)-ysTOP(context)-44-hsp(72),
              child: SingleChildScrollView(
                padding: EdgeInsets.only(left: hsp(20),right: hsp(20),top: hsp(20)),
                child: Column(
                  children: [
                    ListView.separated(
                      itemBuilder: (context, index) {
                        Map item = _dataArray[index];
                        List array = item['array']??[];
                        return Container(
                          padding: EdgeInsets.all(hsp(15)),
                          decoration: BoxDecoration(
                              color: const Color(0xFF2E3138),
                              borderRadius: const BorderRadius.all(Radius.circular(24)),
                              border: Border.all(color: const Color(0xFF3E434E),width: hsp(1))
                          ),
                          child: Column(
                            children: [
                              if('${item['title']}'.isNotEmpty)Container(
                                height: hsp(30),
                                alignment: Alignment.centerLeft,
                                child: Text('${item['title']}',style: TextStyle(fontSize: zsp(14),color: const Color(0xFFDBE1EA)),),
                              ),
                              ListView.builder(
                                itemBuilder: (context, indexSub) {
                                  Map itemSub = array[indexSub];
                                  int type = itemSub['type']??0;
                                  return Container(
                                    padding: EdgeInsets.only(top: hsp(10)),
                                    child: LayoutBuilder(builder: (context,conSize){
                                      return type==2?YSInputView(
                                        item: itemSub
                                      ):Row(
                                        children: [
                                          SizedBox(
                                            width: conSize.maxWidth*0.3,
                                            child: Text('${itemSub['title']}:',style: TextStyle(fontSize: zsp(14),color: ysTitleColor),),
                                          ),
                                          Container(
                                            width: conSize.maxWidth*0.7,
                                            alignment: Alignment.centerRight,
                                            child: type==1?CupertinoTextField(
                                              placeholderStyle: TextStyle(fontSize: zsp(14),color: ysValueColor),
                                              style: TextStyle(fontSize: zsp(14),color: ysValueColor),
                                              padding: const EdgeInsets.all(0),
                                              placeholder: '请输入${item['title']}',
                                              decoration: const BoxDecoration(color: Colors.transparent),
                                              textAlign: TextAlign.right,
                                              onChanged: (value){
                                                if(value.isNotEmpty){
                                                  itemSub['value'] = value;
                                                }else{
                                                  itemSub.remove('value');
                                                }
                                              },
                                            ):type==3?GestureDetector(
                                              onTap: (){
                                                ysShowBottomAlertView(context, YSPicker(dataArray: _expressList, choose: (value){
                                                  itemSub['value'] = value['label'];
                                                  setState(() {});
                                                }),isBarr: true);
                                              },
                                              behavior: HitTestBehavior.opaque,
                                              child: Row(
                                                children: [
                                                  Container(
                                                    width: conSize.maxWidth*0.7-hsp(20),
                                                    alignment: Alignment.centerRight,
                                                    child: Text(itemSub['value']??'请选择${item['title']}',style: TextStyle(fontSize: zsp(14),color: ysValueColor),),
                                                  ),
                                                  Icon(Icons.chevron_right,size: hsp(20),color: Colors.white,)
                                                ],
                                              ),
                                            ):Text(itemSub['value']??'',style: TextStyle(fontSize: zsp(14),color: ysValueColor),),
                                          )
                                        ],
                                      );
                                    }),
                                  );
                                },
                                itemCount: array.length,
                                padding: const EdgeInsets.all(0),
                                physics: const NeverScrollableScrollPhysics(),
                                shrinkWrap: true,
                              )
                            ],
                          ),
                        );
                      },
                      separatorBuilder: (context,index){
                        return Container(height: hsp(12),);
                      },
                      itemCount: _dataArray.length,
                      shrinkWrap: true,
                      padding: EdgeInsets.only(bottom: hsp(12)),
                      physics: const NeverScrollableScrollPhysics(),
                    ),
                  ],
                ),
              ),
            ),
            GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () async{
                //{'title':'快递单号','array':[
                //       {'title':'快递公司','value':'顺丰快递','type':3},
                //       {'title':'快递单号','value':'请输入快递单号','type':1}
                //     ]},
                //     {'title':'快递信息','array':[
                //       {'title':'快递信息','type':2},
                //     ]},
                Map item1 = _dataArray.firstWhere((element) => element['title']=='快递单号');
                List array1 = item1['array']??[];
                Map item2 = _dataArray.firstWhere((element) => element['title']=='快递信息');
                List array2 = item2['array']??[];
                Map request = {'planNum':widget.planNum};
                for (var element in array1) {
                  if(element['value']==null){
                    ysFlutterToast('请完善${element['title']}');
                    return;
                  }else{
                    request[element['key']] = element['value'];
                  }
                }
                for (var element in array2) {
                  if(element['value']==null){
                    ysFlutterToast('请完善${element['title']}');
                    return;
                  }else{
                    request[element['key']] = element['value'];
                  }
                }
                LogUtil.d(request);
                YSNetWork.ysRequestHttp(context, type: RequestType.post, api: '/samp/express', parameter: request, successSetter: (dict){
                  ysFlutterToast('样本已邮寄');
                  Navigator.of(context).pop('');
                },isLoading: true);
              },
              child: Image.asset('images/tzh_submit.png',height: hsp(72),),
            )
          ],
        ),
      ),
    );
  }
}

