<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class CenterController extends Controller
{
    /**
     * 个人中心页
     */
    public function index()
    {
        $live_place = $_GET['live_place'];
        $openid = $_GET['openid'];
        $village_id = $_GET['village_id'];
        if (!$live_place || $live_place == "null" || $live_place == "undefined") {
            $data = array("status" => -1, "msg" => "live_place 有误");
        } elseif (!$openid || $openid == "null" || $openid == "undefined") {
            $data = array("status" => -1, "msg" => "openid 有误");
        } elseif (!$village_id || $village_id == "null" || $village_id == "undefined") {
            $data = array("status" => -1, "msg" => "村ID 有误");
        } else {
            // 判断该村是否有村管理员
            $existNum = M("users")->where("`village_id`={$village_id} AND `identity`=2")->count();
            if ($existNum > 0) {
                $hasVillager = 1;
            } else {
                $hasVillager = -1;
            }
            // 获取该用户的 身份、佣金
            $info = M("users")->where("`openid`='{$openid}'")->field("`identity`,`fund_enable`")->find();
            if ($info['identity'] == 2) {
                $isVillager = 1;
            } else {
                $isVillager = -1;
            }
            if ($info['identity'] == 3) {
                $isGrouper = 1;
            } else {
                $isGrouper = -1;
            }
            if ($info['identity'] == 4) {
                $isCook = 1;
            } else {
                $isCook = -1;
            }
            $fund = $info['fund_enable'];
            $data = array(
                "status" => 1,
                "result" => array(
                    "hasVillager" => $hasVillager,
                    "isVillager" => $isVillager,
                    "isGrouper" => $isGrouper,
                    "isCook" => $isCook,
                    "fund" => $fund
                )
            );
        }
        apiReturn($data);
    }

    /**
     * 保存用户地址
     */
    public function createAddress()
    {
        $result = D("Address")->saveForm($_GET);
        if ($result) {
            $data = array(
                "status" => 1,
                "msg" => "连接成功",
                "result" => $result
            );
        } else {
            $data = array(
                "status" => -1,
                "msg" => "操作失败"
            );
        }
        apiReturn($data);
    }

    /**
     * 删除用户地址
     */
    public function deleteAddress()
    {
        $id = $_GET["id"];
        if ($id) {
            $result = D("Address")->deleteOne($id);
            if ($result) {
                $data = array(
                    "status" => 1,
                    "msg" => "连接成功",
                    "result" => $result
                );
            } else {
                $data = array(
                    "status" => -1,
                    "msg" => "操作失败"
                );
            }
        } else {
            $data = array(
                "status" => -1,
                "msg" => "id获取失败"
            );
        }
        apiReturn($data);
    }

    /**
     * 获取收货地址
     */
    public function getAddress()
    {
        $openid = $_GET["openid"];
        if ($openid != "undefined" && $openid != "NULL" && $openid) {
            $result = D("Address")->getList($openid);
            $data = array(
                "status" => 1,
                "msg" => "连接成功",
                "result" => $result
            );
        } else {
            $data = array(
                "status" => -1,
                "msg" => "openid有误"
            );
        }
        apiReturn($data);
    }
}