<?php
namespace Smi\Controller;

use Think\Controller;

header("Content-Type:text/html;CharSet=UTF-8");
class CarouselController extends BaseController
{
    /**
     * 商城首页轮播图 列表浏览
     */
    public function index()
    {
        auth_check("carousel_read");
        $list = M("carousel")
            ->join("c LEFT JOIN `am_goods` g ON c.`good_id`=g.`id`")
            ->field("c.*,g.`goods_name`")
            ->where("c.`village_id`=0")
            ->select();
        $this->assign("list", $list);
        $this->display("index");die;
    }

    /**
     * 商城添加轮播图
     */
    public function add()
    {
        auth_check("carousel_opt");
        if (IS_POST) {
            $result = D("Carousel")->saveForm();
            if ($result) {
                alertMsg("添加成功", U("index"));
            } else {
                $this->redirect("index");die;
            }
        } else {
            $this->assign("act", 1);
            // 商品数组
            $goods = M("goods")->where("`is_sale`=2")->field("`id`,`goods_name`")->order("`sort` ASC")->select();
            $this->assign("goods", $goods);
            $this->assign("legend", "添加轮播图");
            $this->display("add");die;
        }
    }

    /**
     * 商城编辑轮播图
     */
    public function edit()
    {
        auth_check("carousel_opt");
        if (IS_POST) {
            $result = D("Carousel")->saveForm();
            if ($result) {
                alertMsg("编辑成功", U("index"));
            } else {
                $this->redirect("index");die;
            }
        } else {
            // 获取记录信息
            if (I("id")) {
                $info = M("carousel")->where("`id`=" . I("id"))->find();
                $this->assign("info", $info);
            }
            // 商品数组
            $goods = M("goods")->where("`is_sale`=2")->field("`id`,`goods_name`")->order("`sort` ASC")->select();
            $this->assign("goods", $goods);
            $this->assign("legend", "编辑轮播图");
            $this->assign("act", 2);
            $this->display("add");die;
        }
    }

    /**
     * 商城删除轮播图
     */
    public function del()
    {
        auth_check("carousel_opt");
        //接收 ID
        $id = I("id");
        if (!$id) {$this->error("系统故障，无法接收 ID");die;}
        //定义待删除图片
        $del_img = M("carousel")->where("`id`={$id}")->getField("img");
        $bool = M("carousel")->delete($id);
        if ($bool) {
            //删除成功，删除原图
            @unlink("./Uploads/" . $del_img);
        }
        $this->redirect("index");die;
    }

    /**
     * 城市地区轮播图 列表
     */
    public function listCity()
    {
        auth_check("carousel_read");
        $list = M("carouselCity")->select();
        $this->assign("list", $list);
        $this->display("listCity");die;
    }

    /**
     * 添加 城市地区轮播图
     */
    public function addCity()
    {
        auth_check("carousel_opt");
        if (IS_POST) {
            $result = D("CarouselCity")->saveForm();
            if ($result) {
                alertMsg("添加成功", U("listCity"));
            } else {
                $this->redirect("listCity");die;
            }
        } else {
            $this->assign("act", 1);
            $this->assign("legend", "添加轮播图");
            $this->display("addCity");die;
        }
    }

    /**
     * 编辑 城市地区轮播图
     */
    public function editCity()
    {
        auth_check("carousel_opt");
        if (IS_POST) {
            $result = D("CarouselCity")->saveForm();
            if ($result) {
                alertMsg("编辑成功", U("listCity"));
            } else {
                $this->redirect("listCity");die;
            }
        } else {
            // 获取记录信息
            if (I("id")) {
                $info = M("carouselCity")->where("`id`=" . I("id"))->find();
                $this->assign("info", $info);
            }
            $this->assign("legend", "编辑轮播图");
            $this->assign("act", 2);
            $this->display("addCity");die;
        }
    }

    /**
     * 删除 城市地区轮播图
     */
    public function delCity()
    {
        auth_check("carousel_opt");
        //接收 ID
        $id = I("id");
        if (!$id) {$this->error("系统故障，无法接收 ID");die;}
        //定义待删除图片
        $del_img = M("carouselCity")->where("`id`={$id}")->getField("img");
        $bool = M("carouselCity")->delete($id);
        if ($bool) {
            //删除成功，删除原图
            @unlink("./Uploads/" . $del_img);
        }
        $this->redirect("listCity");die;
    }
}