
import 'package:azlistview/azlistview.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:liftmanager/internal/lift/model/lift_brand_entity.dart';
import 'package:liftmanager/net/api_service.dart';
import 'package:liftmanager/routers/fluro_navigator.dart';
import 'package:liftmanager/utils/toast.dart';
import 'package:liftmanager/widgets/app_bar.dart';

class LiftBrandPage extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return LiftBrandPageState();
  }
}

class LiftBrandPageState extends State<LiftBrandPage>{

  List<LiftBrandEntity> _cityList = [];

  @override
  void initState() {
    super.initState();
    _loadData();
  }

  ///获取品牌数据
  _loadData(){
    ApiService(context: context).liftBrand(onSuccess: (data){
      _cityList = data;
      _cityList.sort((a, b) => (a.getSuspensionTag()).compareTo(b.getSuspensionTag()));
      setState(() {});
    },onError: (code,msg){
      toasts(msg);
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: MyAppBar(
        centerTitle: "选择品牌",
      ),
      body:_cityList.length==0? Center(
        child: Text("数据加载中..."),
      ):
      SafeArea(
        child: AzListView(
          data: _cityList,
          itemBuilder: (context, model) => _buildListItem(model),
          isUseRealIndex: true,
          itemHeight: 40,
          suspensionWidget: null,
          suspensionHeight: 0,
          indexBarBuilder:(context, list, onTouch){
            return IndexBar(
                onTouch: onTouch,
                data: list,
                itemHeight: 18,
                touchDownColor: Colors.transparent,
                textStyle: Theme.of(context).textTheme.subtitle
            );
          },
        ),
      ),
    );
  }

  Widget _buildListItem(LiftBrandEntity model) {
    return InkWell(
      onTap: () => NavigatorUtils.goBackWithParams(context, model),
      child: Container(
        padding: const EdgeInsets.only(left: 16.0, right: 34.0),
        height: 40.0,
        child: Container(
          decoration: BoxDecoration(
              border: (model.isShowSuspension) ? Border(
                top: Divider.createBorderSide(context, width: 0.6),
              ) : null
          ),
          child: Row(
            children: <Widget>[
              Opacity(
                  opacity: model.isShowSuspension ? 1 : 0,
                  child: SizedBox(
                    width: 28.0,
                    child: Text(model.getSuspensionTag()),
                  )
              ),
              Expanded(
                child: Text(model.name),
              )
            ],
          ),
        ),
      ),
    );
  }
}