<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class IndexController extends Controller
{
    /**
     * 小程序首页
     */
    public function index(){
        file_put_contents("./Logs/index.log", date("Y-m-d H:i:s ") . serialize($_GET) . "\r\n\r\n", FILE_APPEND);
        $live_place = $_GET['live_place'];
        if (!$live_place || $live_place == "null" || $live_place == "undefined") {
            $data = array(
                "status" => -1,
                "msg" => "生活地区有误"
            );
        } else {
            if ($live_place == 1) {
                $data = $this->ncUser();
            } elseif ($live_place == 2) {
                $data = $this->csUser();
            }
        }
        apiReturn($data);
    }

    /**
     * 农村用户
     */
    public function ncUser()
    {
        $village_id = $_GET['village_id'];
        if (!$village_id || $village_id == "null" || $village_id == "undefined") {
            $data = array(
                "status" => -1,
                "msg" => "村ID有误"
            );
        } else {
            // 每日特惠
            $daily = M("goods")
                ->where("`is_sale`=2 AND (`is_daily`=2 OR `is_hot`=2 OR `is_new`=2) AND `deleted`=-1")
                ->order("`sort` ASC, `id` DESC")
                ->field("`id`,`goods_name`,`goods_price`,`thumb`,`des`,`is_daily`,`is_hot`,`is_new`")
                ->select();
            foreach ($daily as $key => $value) {
                $daily[$key]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value['thumb'];
                if ($value['is_daily'] == 2) {
                    // 每日特价
                    $daily[$key]['is_daily'] = 1;
                } elseif ($value['is_hot'] == 2) {
                    // 热销商品
                    $daily[$key]['is_daily'] = 2;
                } elseif ($value['is_new'] == 2) {
                    // 新品上线
                    $daily[$key]['is_daily'] = 3;
                }
                unset($daily[$key]['is_hot'], $daily[$key]['is_new']);
            }
            unset($key, $value);
            $result = array(
                "carousel" => D("Carousel")->getList($village_id),    // 轮播图
                "categories" => D("GoodsCategories")->getCategories(),    // 分类列表
                "daily" => $daily,
                "goods" => D("GoodsCategories")->getIndexGoods($village_id)    // 分类及商品
            );
            $data = array(
                "status" => 1,
                "result" => $result
            );
        }
        return $data;
    }

    /**
     * 城市用户
     */
    public function csUser()
    {
        file_put_contents("./Logs/csUser.log", date("Y-m-d H:i:s ") . serialize($_GET) . "\r\n\r\n", FILE_APPEND);
        $lat = $_GET['lat'];
        $lng = $_GET['lng'];
        $county_id = $_GET['county_id'];
        $village_id = $_GET['village_id'];
        $bool_1 = $lat && $lat != "null" && $lat != "undefined";
        $bool_2 = $lng && $lng != "null" && $lng != "undefined";
        $bool_3 = $county_id && $county_id != "null" && $county_id != "undefined";
        if (!$bool_1) {
            $data = array(
                "status" => -1,
                "msg" => "纬度有误"
            );
        } elseif (!$bool_2) {
            $data = array(
                "status" => -1,
                "msg" => "经度有误"
            );
        } elseif (!$bool_3) {
            $data = array(
                "status" => -1,
                "msg" => "区ID有误"
            );
        } else {
            $result = array(
                "carousel" => D("CarouselCity")->getList($village_id),    // 轮播图
                "stores" => D("Stores")->inRadius($county_id)    // 商家列表
            );
            $data = array(
                "status" => 1,
                "result" => $result
            );
        }
        return $data;
    }
    
    /**
     * 分类or商家页
     */
    public function cateOrStore()
    {
        $live_place = $_GET['live_place'];
        if ($live_place == 1) {
            $village_id = $_GET['village_id'];    // 村ID有误
            if (!$village_id || $village_id == "null" || $village_id == "undefined") {
                $data = array(
                    "status" => -1,
                    "msg" => "村ID有误"
                );
            } else {
                // 农村地区用户
                $res = D("GoodsCategories")->getCatogoryGoods($live_place);
                // 每日特惠
                $daily = M("goods")
                    ->where("`is_sale`=2 AND `is_daily`=2 AND `deleted`=-1")
                    ->order("`sort` ASC, `id` DESC")
                    ->field("`id`,`goods_name`,`goods_price`,`thumb`,`des`")
                    ->select();
                foreach ($daily as $key => $value) {
                    $daily[$key]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $value['thumb'];
                }
                unset($key, $value);
                $data = array(
                    "status" => 1,
                    "result" => $res,
                    "daily" => $daily
                );
            }
        } else {
            // 城市地区用户
            $store_id = $_GET['store_id'];
            if (!$store_id || $store_id == "null" || $store_id == "undefined") {
                $data = array(
                    "status" => -1,
                    "msg" => "商家ID有误"
                );
            } else {
                $res = $this->cateFromStore($store_id);
                // 获取商家信息
                $store = M("stores")
                    ->where("`id`={$store_id}")
                    ->find();
                $store['logo'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $store['logo'];
                $store['store_photo'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . $store['store_photo'];
                $store['create_time'] = date("Y-m-d", $store['create_time']);
                $store['invalid_time'] = date("Y-m-d", $store['invalid_time']);
                $data = array(
                    "status" => 1,
                    "store" => $store,
                    "hot" => $res['hot'],
                    "goodsArr" => $res['goodsArr'],
                    "cate" => $res['cate']
                );
            }
        }
        apiReturn($data);
    }

    /**
     * 城市(商家)分类页 本控制器使用
     * $store_id 商家ID
     */
    public function cateFromStore($store_id)
    {
        // 分类
        $cates = M("storesGoodsCategories")
            ->order("`sort` ASC")
            ->where("`store_id`={$store_id}")
            ->select();
        // 分类
        $list = $hot = array();
        // 分类 循环开始
        foreach ($cates as $key => $value) {
            $pcate = $value['id'];
            $goods = M("storesGoods")
                ->join("sg LEFT JOIN `am_stores` s ON sg.`store_id`=s.`id`")
                ->where("sg.`pcate`={$pcate} AND sg.`store_id`={$store_id} AND sg.`deleted`=-1")
                ->field("sg.*,s.`name` AS `store_name`")
                ->select();
            if (!$goods) {
                $goods = array();
            }
            foreach ($goods as $kg => $vg) {
                if ($vg['is_hot'] == 2) {
                    array_push(
                        $hot,
                        $vg
                    );
                }
                $thumbName = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/{$vg['thumb']}";
                if (file_get_contents($thumbName)) {
                    $goods[$kg]['thumb'] = $thumbName;
                } else {
                    $goods[$kg]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Public/img/torch.png";
                }
            }
            unset($kg, $vg);
            $list[$pcate] = $goods;
        }
        // 分类 循环结束
        unset($key, $value);
        return array(
            "goodsArr" => $list,
            "hot" => $hot,
            "cate" => $cates
        );
    }

    /**
     * 本村信息
     */
    public function village_info()
    {
        // $village_id = $_GET['village_id'];
        // if (!$village_id || $village_id == "null" || $village_id == "undefined" || !is_numeric($village_id)) {
        //     $data = array("status" => -1, "msg" => "村ID有误");
        // } else {
        //     // 村信息
        //     $info = M("regionVillage")->where("`id`={$village_id}")->find();
        //     // 村长信息
        //     $villager = M("users")->where("`village_id`={$village_id} AND `identity`=2")->find();
        //     $data = array(
        //         "status" => 1,
        //         "info" => $info,
        //         "villager" => $villager
        //     );
        // }
        // apiReturn($data);
        // send_mail('595053294@qq.com','测试','测试','测试');
    }


}