import 'dart:convert' as convert;

import 'package:flutter/material.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/goods_share_image/goods_share_image_model.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/models/goods_share_temp_add_model.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/widgets/goods_share_item.dart';
import 'package:zhiying_base_widget/pages/goods_share_page/widgets/goods_share_miniprogram_widget.dart';
import 'package:zhiying_base_widget/widgets/public/custom_button/custom_bottom.dart';
import 'package:zhiying_base_widget/widgets/public/custom_button/custom_button_model.dart';

class GoodsShareMiniprogram extends StatefulWidget {
  final Map<String, dynamic> model;

  final GoodsShareTempDataModel datas;

  const GoodsShareMiniprogram(this.model, this.datas, {Key key})
      : super(key: key);

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

class _GoodsShareMiniprogramState extends State<GoodsShareMiniprogram>
    with AutomaticKeepAliveClientMixin {
  GoodsShareImageModel _style;
  Map<int, String> _images = Map();
  List<Widget> widgets = List();

  // 切换Tab导航栏保持页面状态
  bool get wantKeepAlive => true;

  @override
  void initState() {
    String data = widget.model['data'];
    var d = convert.jsonDecode(data);
    _style = GoodsShareImageModel.fromJson(Map<String, dynamic>.from(d));
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: Container(
            margin: EdgeInsets.only(bottom: 4),
            padding: EdgeInsets.only(top: 10),
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.only(
                    bottomLeft: Radius.circular(8),
                    bottomRight: Radius.circular(8))),
            child: SingleChildScrollView(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  GoodsShareItem(
                    imgHeader: _style.cssList.headerImage,
                    imgCheck: _style.cssList.unChooseImage,
                    imgCheckSelected: _style.cssList.chooseImage,
                    enabled: false,
                    child: GoodsShareMiniprogramWidget(
                      title: widget.datas?.miniAppTitle ?? '',
                      imgGoods: widget.datas?.imgUrl ?? '',
                      icon: widget.datas?.appLogo ?? '',
                      name: widget.datas?.appName ?? '',
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        _createBottom(),
      ],
    );
  }

  Widget _createBottom() {
    Map<String, dynamic> shareImages = _style.cssList.shareImage;
    if (shareImages == null) {
      return Container();
    }
    // CustomButtonModel btnCopy = CustomButtonModel.fromJson(
    //     Map<String, dynamic>.from(shareImages['copy_btn']));
    CustomButtonModel btnShare = CustomButtonModel.fromJson(
        Map<String, dynamic>.from(shareImages['share_btn']));
    return SafeArea(
      child: Container(
        margin: EdgeInsets.only(left: 12.5, right: 12.5, top: 4, bottom: 4),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            btnShare == null
                ? Container()
                : GestureDetector(
                    child: Container(
                        margin: EdgeInsets.only(left: 5),
                        width: 110,
                        height: 36,
                        decoration: BoxDecoration(
                          borderRadius: BorderRadius.circular(18),
                        ),
                        child: CustomButton(btnShare)),
                    onTap: () {},
                  ),
          ],
        ),
      ),
    );
  }
}
