<?php

namespace Admin\Controller;

use Think\Controller;

/**
 * 商品管理（控制器）
 *
 * @author sunxiaodong
 */
class GoodsController extends CheckController {

    public function index() {
 
           $t = I('t');
           $f = I('fl');
           $where['pid'] = $f; 
           $where['_logic'] = 'AND';
           $where['title'] =  array("like","%$t%");//写查询条件
          
           
           if(empty($t) && empty($f))
           {
               $where="";
           }


        $on = I('column');
        $list = M('good')->where($where)->select();
        $flist = M('assortment')->select(); //查询商品分类
        $this->assign("flist", $flist);
        $this->assign("on", $on);
        $this->assign("t",$t);
        $this->assign("f",$f);
        $this->assign("list", $list);
        $this->display();
    }

    //添加商品
    public function add() {
        if (IS_POST) {
            $data = I('post.');
            $data['add_time'] = date("Y-m-d H:i:s");
            if (M('good')->add($data)) {
                echo "<script>alert('添加成功');parent.location.href='/index.php/Admin/Goods/index?column=spgl'; </script>";
            } else {
                $this->error("添加失败");
            }
        }
        $flist = M('assortment')->select(); //查询商品分类
        $on = I('column');
        $this->assign("flist", $flist);
        $this->assign('on', $on);
        $this->display();
    }

    //删除商品
    public function delete($id) {
        if (M('good')->delete($id)) {
            echo "<script>alert('删除成功 !');parent.location.href='/index.php/Admin/Goods/index?column=spgl';</script>";
        } else {
            $this->error("删除失败");
        }
    }
    //删除改商品的全部卡密
    
    public function deletekami($id){
       if(M("kami")->where("g_id=$id")->delete())
       {
            M("good")->where("id=$id")->setField('kucun',0);
            echo "<script>alert('删除成功 !');parent.location.href='/index.php/Admin/Goods/index?column=spgl';</script>";
        } else {
            $this->error("删除失败");
       }
    }

    //查看 （修改）商品

    public function update($id) {
        if(IS_POST)
        {
            $data = I('post.');
            if(M('good')->where("id=$id")->save($data))
            {
                echo "<script>alert('修改成功');parent.location.href='/index.php/Admin/Goods/index?column=spgl'; </script>";
            }
            else
            {
                $this->error("修改失败！");
                
            }
        }
        $flist = M('assortment')->select(); //查询商品分类
        $this->assign("flist", $flist);
        $list = M('good')->find($id);
        $on = I('column');
        $this->assign('list', $list);
        $this->assign('on', $on);
        $this->display();
    }

}
