<?php
namespace Api\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class DishManageController extends Controller
{
    /**
     * 套餐编辑
     */
    public function changeDishMeterial()
    {
        $dish_id = $_GET['dish_id'];    // 套餐ID
        $name = $_GET['name'];
        $describe = $_GET['describe'];
        $thumb = $_GET['thumb'];
        $goods = json_decode($_GET['goods']);
        $caipin = json_decode($_GET['caipin']);
        // 检测商品数组是否正常
        $flagGoods = true;
        $countGoods = 0;
        foreach ($goods as $kg => $vg) {
            if (!$vg->id) {
                $flagGoods = false;
                break;
            }
            if ($vg->num) {
                $countGoods++;
            }
        }
        unset($kg, $vg);
        // 检测菜品数组是否正常
        $flagCaipin = true;
        foreach ($caipin as $kc => $vc) {
            if (!$vc->name || !$vc->thumb) {
                $flagCaipin = false;
                break;
            }
        }
        unset($kc, $vc);
        if (!$dish_id || $dish_id == "undefined" || $dish_id == "null" || !is_numeric($dish_id)) {
            $data = array("status" => -1, "msg" => "套餐ID有误");
        } elseif (!$name || $name == "undefined" || $name == "null") {
            $data = array("status" => -1, "msg" => "名称有误");
        } elseif (!$describe || $describe == "undefined" || $describe == "null") {
            $data = array("status" => -1, "msg" => "介绍有误");
        } elseif ($thumb == "undefined" || $thumb == "null") {
            $data = array("status" => -1, "msg" => "缩略图有误");
        } elseif (!$goods || !is_array($goods) || !$goods[0] || !$flagGoods) {
            $data = array("status" => -1, "msg" => "食材有误");
        } elseif (!$caipin || !is_array($caipin) || !$caipin[0] || !$flagCaipin) {
            $data = array("status" => -1, "msg" => "菜品有误");
        } elseif ($countGoods == 0) {
            $data = array("status" => -1, "msg" => "没有提交食材");
        } else {
            // 修改 dish 记录
            $saveData = array(
                "name" => $name,
                "describe" => $describe
            );
            if ($thumb) {
                $saveData['thumb'] = str_replace("\"", "", $thumb);
            }
            M("dish")->where("`id`={$dish_id}")->save($saveData);
            // 不需要重算套餐价格, 套餐价格为即时统计
            // 删除该套餐的商品副表
            M("dishGood")->where("`dish_id`={$dish_id}")->delete();
            // 新增 商品副表
            foreach ($goods as $key => $value) {
                $good_id = $value->id;   // 商品ID
                $num = $value->num;
                if ($good_id > 0 && $num > 0) {
                    $addData = array(
                        "dish_id" => $dish_id,
                        "good_id" => $good_id,
                        "num" => $num
                    );
                    M("dishGood")->add($addData);
                }
            }
            unset($key, $value);
            // 删除该套餐的菜品副表
            M("dishCaipin")->where("`dish_id`={$dish_id}")->delete();
            // 生成 菜品副表
            foreach ($caipin as $key => $value) {
                $name = $value->name;
                $thumb = $value->thumb;
                $addData = array(
                    "dish_id" => $dish_id,
                    "name" => $name,
                    "thumb" => $thumb
                );
                M("dishCaipin")->add($addData);
            }
            unset($key, $value);
            $data = array("status" => 1, "msg" => "操作成功");
        }
        apiReturn($data);
    }

    /**
     * 删除套餐
     */
    public function dishDelete()
    {
        $id = $_GET['id'];    // 套餐ID
        if (!$id || $id == "undefined" || $id == "null" || !is_numeric($id)) {
            $data = array("status" => -1, "msg" => "套餐ID有误");
        } else {
            // 删除 套餐记录
            $result = M("dish")->where("`id`={$id}")->delete();
            if ($result) {
                // 删除 副表记录
                M("dishGood")->where("`dish_id`={$id}")->delete();
                $data = array("status" => 1, "msg" => "操作成功");
            } else {
                $data = array("status" => -1, "msg" => "系统繁忙");
            }
        }
        apiReturn($data);
    }

    /**
     * 底部栏进入宴席页
     */
    public function dishPage()
    {
        $village_id = $_GET['village_id'];    // 村ID
        if (!$village_id || $village_id == "undefined" || $village_id == "null" || !is_numeric($village_id)) {
            $data = array("status" => -1, "msg" => "村ID有误");
        } else {
            // 查询本村套餐
            $resource = M("dish")
                ->where("`village_id`={$village_id}")
                ->field("`id`,`name`,`describe`,`thumb`")
                ->order("`sort` ASC")
                ->select();
            $list = array();
            foreach ($resource as $key => $value) {
                // 查询套餐总价
                $res = M("dishGood")
                    ->join("dg LEFT JOIN `am_goods` g ON dg.`good_id`=g.`id`")
                    ->where("dg.`dish_id`={$value['id']}")
                    ->field("g.`goods_price` AS `price`,dg.`num`")
                    ->select();
                $price = 0;
                foreach ($res as $kr => $vr) {
                    $price += strval($vr['price']) * $vr['num'];
                }
                // 根据套餐查询副表
                $dish_good = M("dishGood")
                    ->join("dg LEFT JOIN `am_goods` g ON dg.`good_id`=g.`id`")
                    ->where("dg.`dish_id`={$value['id']}")
                    ->field("dg.`id`,dg.`dish_id`,dg.`good_id`,g.`thumb`,g.`goods_price` AS `price`,dg.`num`,g.`goods_name` AS `name`")
                    ->order("dg.`sort` ASC")
                    ->select();
                foreach ($dish_good as $kdg => $vdg) {
                    $dish_good[$kdg]['thumb'] = "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . str_replace("\"", "", $vdg['thumb']);
                }
                unset($kdg, $vdg);
                if (!$dish_good) {
                    $dish_good = array();
                }
                // 入栈
                array_push(
                    $list, 
                    array(
                        "id" => $value['id'],
                        "name" => $value['name'],
                        "describe" => $value['describe'],
                        "thumb" => "http://" . $_SERVER['HTTP_HOST'] . __ROOT__ . "/Uploads/" . str_replace("\"", "", $value['thumb']),
                        "price" => $price,
                        "dish_good" => $dish_good
                    )
                );
            }
            unset($key, $value);
            $data = array("status" => 1, "result" => $list);
        }
        apiReturn($data);
    }
}