import 'package:feelverapp/model/Login/login_model.dart';
import 'package:feelverapp/model/category/category_model.dart';
import 'package:feelverapp/model/editprofile/getprofilemodel.dart';
import 'package:feelverapp/model/home/home_model.dart';
import 'package:feelverapp/model/home_search/searchhomepage_model.dart';
import 'package:feelverapp/service/Loading.dart';

import 'package:feelverapp/service/api.dart';
import 'package:feelverapp/service/base_presenter.dart';
import 'package:feelverapp/ui/home/home.dart';
import 'package:feelverapp/util/Accout_util.dart';
import 'package:flutter/material.dart';
import 'package:rflutter_alert/rflutter_alert.dart';
import 'package:shared_preferences/shared_preferences.dart';

class HomePresenter extends BasePresenter<HomePage> {
  Api _api;
  var category_model;
  String uid;
  final formkey = GlobalKey<FormState>();
  GetprofileModel getmodel;
  LoginModel logmodel;

  CategoryModel CatModel;

  TextEditingController searchCtrl = TextEditingController();
  SearchhomeModel searchhomeModel;

  HomePresenter(State<HomePage> state) : super(state);

  Listitem() async {
 
    _api = Api<CategoryModel>();
   
    var res = await _api.category({
      "access_type": "mobile",
    });

// LoadingView(state.context).hide();
    if (res.success != null){
      setState(() {


        CatModel = res.success;

     
      });

    } else {
      print('res Fail');
    }
  }

   getSearch() async{
     LoadingView(state.context).show();
    _api = Api<SearchhomeModel>();
    var res = await _api.homesearch({
      "keyword_shop" : searchCtrl.text,
      "lang" : "all",
      "access" : "portal",
    });
    LoadingView(state.context).hide();
   print('$searchCtrl');


    if (res.fail == null)  
      setState(() {
        searchhomeModel = res.success;
        category_model = res.success;
      });
    
    else{

    }

  }

  getid() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    setState(() {
      uid = pref.getString('Id');
    });
    return uid;
  }

  getprofile() async {
    _api = Api<GetprofileModel>();

    var res = await _api.getprofile({"id": await AccountUtil.getID()});

    if (res.fail == null) {
      setState(() {
        getmodel = res.success;
      });
      print("Hii" + getmodel.result[0].name);
    } else {
      Alert(
          style: AlertStyle(
            animationType: AnimationType.fromTop,
            isCloseButton: false,
          ),
          context: state.context,
          title: "ไม่สามารถติดต่อ\nเซิร์ฟเวอร์ได้",
          content: Icon(
            Icons.warning,
            color: Colors.orange,
            size: 80,
          ),
          buttons: [
            DialogButton(
              color: Color.fromRGBO(106, 179, 170, 1),
              onPressed: () => Navigator.pop(state.context),
              child: Text(
                "ลองอีกครั้ง",
                style: TextStyle(color: Colors.white, fontSize: 20),
              ),
            )
          ]).show();
    }
  }

}
