select_table_page.dart 11.6 KB
import 'dart:ffi';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:one_key/models/area.dart';
import 'package:one_key/models/notebook.dart';
import 'package:one_key/models/tableOuter.dart';
import 'package:one_key/models/table_list.dart';
import 'package:one_key/provider/choose_table_model.dart';
import 'package:one_key/provider/notebook_detail_change_model.dart';
import 'package:one_key/provider/notebook_model.dart';
import 'package:one_key/provider/select_table_model.dart';
import 'package:one_key/provider/user_model.dart';
import 'package:one_key/res/colors.dart';
import 'package:one_key/utils/utils.dart';
import 'package:one_key/widgets/app_bar.dart';
import 'package:one_key/widgets/area_item.dart';
import 'package:one_key/widgets/common_button.dart';
import 'package:one_key/widgets/table_item.dart';
import 'package:one_key/models/table.dart' as t;
import 'package:provider/provider.dart';
import 'dart:convert' as convert;

class SelectTablePage extends StatefulWidget {
  final String rid; //预订id
  final DateTime dateTime; //日期
  final String ddate_slot; //时段
  final bool canChoose; //变更预订可以取消原来已预订的选中状态
  final List<TableList> tableList;

  SelectTablePage(this.rid, this.dateTime, this.ddate_slot,
      {this.canChoose = false, this.tableList});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new SelectTablePageState();
  }
}

class SelectTablePageState extends State<SelectTablePage> {
  List<Area> areas = []; //区域列表
  List<t.Table> selectTables = []; //选择的桌位

  List<TableOuter> tableOuterList = []; //区域-桌位列表

  Map<String, List<t.Table>> areaTableMap = {}; //区域-桌位Map

  SelectTableModel selectTableModel = new SelectTableModel();

  ChooseTableModel _chooseTableModel;

  Map<String, Notebook> tableMap = {}; //记录已预订桌位对应的订单

  String currentArea; //当前选中区域

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
      getTableList();
    });
  }

  //获取桌位列表
  getTableList() async {
    UserModel userModel = Provider.of<UserModel>(context, listen: false);
    Map<String, String> params = {"sellerid": userModel.user.sellerid};
    tableOuterList = await selectTableModel.getTableList(context, params);
    for (TableOuter item in tableOuterList) {
      // if (item.name == "推荐") {
      //   itemModelList.remove(item);
      // }
      for (var item in item.list) {
        for (var table in widget.tableList) {
          if (item.id == table.id) {
            item.selected = true;
            // if (!tableList.contains(item)) {
            //   tableList.add(item);
            // }
          }
        }
      }
    }
    _getReserveList(userModel.user.sellerid);
  }

  //获取已预订列表
  _getReserveList(String sid) async {
    NoteBookModel model = new NoteBookModel();
    Map<String, String> params = {
      "sid": sid,
      "rdate": _getRdate(),
      "ddate_solt": widget.ddate_slot,
      "y": "200",
      "x": "1",
      "keywords": "",
      "sort": "1"
    };
    List<Notebook> noteBooks = await model.getReserveList(context, params);
    for (Notebook notebook in noteBooks) {
      if (notebook.tid.isEmpty) continue;
      if (notebook.status == "1" ||
          notebook.status == "2" ||
          notebook.status == "5") {
        //已预订,用餐中,已安排
        List<String> tids = notebook.tid.split(",");
        for (String tid in tids) {
          tableMap["$tid"] = notebook;
        }
      }
    }
    print(tableMap);
    initData();
  }

  //初始桌位数据
  initData() {
    for (TableOuter outer in tableOuterList) {
      int count = 0;
      for (t.Table table in outer.list) {
        if (table.tablestatus == "0") {
          //没有用餐中
          if (tableMap["${table.id}"] != null) {
            Notebook notebook = tableMap["${table.id}"];
            table.sign = notebook.status;
            table.notebookId = notebook.id;
            table.reserver = notebook.rnameStr;
          } else {
            table.sign = "";
            table.notebookId = "";
            if (table.enable == "1") {
              count++;
            }
          }
        } else {
          table.sign = "2";
          table.notebookId = "";
        }
      }
      areaTableMap["${outer.name}"] = outer.list;
      Area area = Area();
      area.name = outer.name;
      area.count = count;
      areas.add(area);
    }
    currentArea = areaTableMap.keys.first;
    setState(() {});
  }

  //获取一天开始-结束时间字符串
  String _getRdate() {
    //一天开始时间
    double startDay = (DateTime.parse((widget.dateTime.year.toString() +
                "-" +
                Utils.addZeroNumber(widget.dateTime.month) +
                "-" +
                Utils.addZeroNumber(widget.dateTime.day) +
                " 00:00:00"))
            .millisecondsSinceEpoch) /
        1000;
    //一天结束时间
    double endDay = (DateTime.parse((widget.dateTime.year.toString() +
                "-" +
                Utils.addZeroNumber(widget.dateTime.month) +
                "-" +
                Utils.addZeroNumber(widget.dateTime.day) +
                " 23:59:59"))
            .millisecondsSinceEpoch) /
        1000;
    return startDay.floor().toString() + "-" + endDay.floor().toString();
  }

  @override
  Widget build(BuildContext context) {
    _chooseTableModel = Provider.of<ChooseTableModel>(context);
    return SafeArea(
      child: areaTableMap.isEmpty
          ? Container(
              width: double.infinity,
              height: double.infinity,
              color: MyColours.color_ffffff,
            )
          : Column(
              children: <Widget>[
                MyAppBar(
                  "选择桌位",
                  showBack: true,
                ),
                Expanded(
                  child: Container(
                    color: MyColours.color_ffffff,
                    child: Row(
                      children: <Widget>[
                        Container(
                          width: ScreenUtil().setWidth(189),
                          height: double.infinity,
                          color: MyColours.color_f1f1f1,
                          child: ListView.builder(
                            itemBuilder: (context, index) {
                              return AreaItem(areas[index], currentArea,
                                  (area) {
                                setState(() {
                                  currentArea = (area as Area).name;
                                });
                              });
                            },
                            itemExtent: ScreenUtil().setHeight(207),
                            itemCount: areas.length,
                          ),
                        ),
                        Expanded(
                          child: Padding(
                            padding: EdgeInsets.symmetric(
                                vertical: ScreenUtil().setHeight(10),
                                horizontal: ScreenUtil().setWidth(9)),
                            child: GridView.builder(
                              gridDelegate:
                                  SliverGridDelegateWithFixedCrossAxisCount(
                                      crossAxisCount: 3, childAspectRatio: 0.8),
                              itemBuilder: (context, index) {
                                return TableItem(
                                    areaTableMap[currentArea][index],
                                    onSelected: (table) {
                                  if ((table as t.Table).selected) {
                                    selectTables.add(table);
                                    widget.tableList
                                        .add(tableToTableList(table));
                                  } else {
                                    widget.tableList
                                        .removeWhere((t) => t.id == table.id);
                                    selectTables
                                        .removeWhere((t) => t.id == table.id);
                                  }
                                },
                                    canSelected: true,
                                    canChoose: widget.canChoose,
                                    tableMap: tableMap,
                                    rid: widget.rid);
                              },
                              itemCount: areaTableMap[currentArea].length,
                            ),
                          ),
                        )
                      ],
                    ),
                  ),
                ),
                Container(
                    width: double.infinity,
                    height: ScreenUtil().setHeight(100),
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                        color: MyColours.color_ffffff,
                        border: Border(
                            top: BorderSide(
                                color: MyColours.color_bfbfbf,
                                width: ScreenUtil().setHeight(1)))),
                    child: CommonButton(
                      content: "确定",
                      width: 600,
                      height: 84,
                      fontSize: 34,
                      state: 1,
                      onPress: () {
                        if (widget.canChoose) {
                          // if (widget.tableList.length > 0) {
                            String tid = "";
                            for (TableList table in widget.tableList) {
                              tid += table.id + ",";
                            }
                            if (widget.tableList.length > 0) {
                              tid = tid.substring(0, tid.length - 1);
                            }

                            Map temp = {'object': widget.tableList, 'tid': tid};
                            Navigator.of(context).pop(temp);
                          // }
                        } else {
                          if (selectTables.length > 0) {
                            String tid = "";
                            for (t.Table table in selectTables) {
                              tid += table.id + ",";
                            }
                            if (selectTables.length > 0) {
                              tid = tid.substring(0, tid.length - 1);
                            }

                            Map temp = {'object': widget.tableList, 'tid': tid};
                            Navigator.of(context).pop(temp);
                          }
                        }
                      },
                    ))
              ],
            ),
    );
  }

  tableToTableList(t.Table table) {
    TableList _tableList = TableList(
        '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
    _tableList.id = table.id;
    _tableList.rid = table.rid;
    _tableList.tid = table.tid;
    _tableList.name = table.name;
    _tableList.tablePnum = table.table_pnum;
    _tableList.sellerid = null;
    _tableList.qrcode = null;
    _tableList.mostPnum = table.most_pnum;
    _tableList.hot = null;
    _tableList.orderO = table.order_o;
    _tableList.status = table.status;
    _tableList.enable = table.enable;
    _tableList.spell = table.spell;
    _tableList.cdate = null;
    _tableList.rname = table.rname;
    _tableList.regionName = table.rname;
    _tableList.selected = true;
    return _tableList;
  }
}