<?php

namespace Admin\Controller;

use Think\Controller;

/**
 * 卡密管理（控制器）
 *
 * @author sunxiaodong
 */
class KamiController extends CheckController{
    public function index(){
        $gid = I('gid');
        
        if($gid!="")
        {
            $where="g_id=$gid";
        }
        else{
            $where="";
        }
        
        $count = M('kami')->where($where)->count(); // 查询满足要求的总记录数

        $page = new \Think\Page($count, 15); //每页显示5条数据
        $page->setConfig('prev', '上一页');

        $page->setConfig('next', '下一页');

        $page->setConfig('last', '末页');

        $page->setConfig('first', '首页');

        $page->setConfig('theme', '%UP_PAGE% %LINK_PAGE% %DOWN_PAGE%');

        $startno = $page->firstRow; //第一列
//        $nowpage = $page->nowPage; //当前页

        $pagesize = $page->listRows; //页面大小

        $list = M('kami')->where($where)->order('status=0 desc,id desc')->limit("$startno,$pagesize")->select();

        $pageinfo = $page->show(); // 分页显示输出

        $this->assign('page', $pageinfo);
            
        
        
//        $list = M('kami')->where($where)->select();
        $flist = M('assortment')->select(); //查询商品分类
        $this->assign("flist", $flist);
        $on = I('column');
        $this->assign('list',$list);
        $this->assign("on", $on);
        $this->display();
    }
    
    //添加卡密
    
    public function add(){
        if(IS_POST)
        {
            $connt = 0;
            $course['g_id'] = I('post.g_id');
            $gid = $course['g_id'];

            $cdkarr = explode("\n", $_POST['cdk']);
            //var_dump($cdkarr);die;
            foreach ($cdkarr as $key => $val) 
             {
                $count++;
                $course['cdk'] = $val;
                M('kami')->add($course);
            }
            $glist = M('good')->find($gid);
            $kuncun = $glist['kucun'];
            $newkucun =$kuncun+$count;
            M('good')->where("id='$gid'")->setField('kucun',$newkucun);
            echo "<script>alert('添加成功');parent.location.href='/index.php/Admin/Kami/index?column=kmgl'; </script>"; 
        }
        $flist = M('assortment')->select(); //查询商品分类
        $this->assign("flist", $flist);
        $on = I('column');
        $this->assign("on", $on);
        $this->display();
    }

    //删除单个卡密
    public function delete($id)
    {
        $klist = M('kami')->where("id=$id")->find();
        $gid = $klist['g_id'];
        $status =  $klist['status'];
        if(M('kami')->delete($id))
        {
            if($status==0)
            {
                  M('good')->where("id=$gid")->setDec("kucun",1);
            echo "<script>alert('删除成功');parent.location.href='/index.php/Admin/Kami/index?column=kmgl'; </script>"; 
            }
            else
            {
                echo "<script>alert('删除成功');parent.location.href='/index.php/Admin/Kami/index?column=kmgl'; </script>"; 
            }
            
          
        }
          
    }
    
    //ajax 获取商品名称
    
    public function ajaxname()
    {
        $name = $_POST['name'];
        $list = M('good')->where("pid='$name' AND fakaleixin = '卡密发卡'")->select();
        $this->ajaxReturn($list);
    }

    public function del(){
       $kami = D("kami"); //获取当期模块的操作对象
       $id = $_POST['id'];  //判断id是数组还是一个数值
       if(is_array($id)){
         $where = 'id in('.implode(',',$id).')';
       }else{
        $where = 'id='.$id;
       }  //dump($where);
       $list=$kami->where($where)->delete();
       if($list!==false) {
          $this->success("成功删除{$list}条！", U("Admin/kami/index"));
       }else{
         $this->error('删除失败！');
       } 
    }
}
