<?php
namespace Smi\Controller;
use Think\Controller;
header("Content-type:text/html;charset=utf-8");
class CateController extends BaseController 
{
	protected $business_id;
	protected $root;

    public function __construct()
    {
        parent::__construct();

        $this->business_id = session('admin_id');
        $this->root = dirname(dirname(dirname(__DIR__)));
    }

    /**
     * 轮播图列表
     */
    public function index()
    {   
        $list = M('cate')->where(array('admin_id' => $this->business_id))->order('sort asc,addtime desc')->select();
        if(empty($list)){

            $title = array(' New Item Recommend','Offered Package','Signature Recommended','Limited Time Discount');
            foreach ($title as $key=>$value) {
                $add['admin_id']=session('admin_id');
                $add['title']=$value;
                $add['status']=1;
                $add['sort']=$key;
                $add['addtime']=time();
                M('cate')->add($add);
            }
            $list = M('cate')->where(array('admin_id' => $this->business_id))->order('sort asc,addtime desc')->select();
        }
        $this->assign('list', $list);
    	$this->display();
    }

    /**
     * 添加轮播图
     */
    public function add()
    {
    	$data = I('post.');

    	if (!empty($data)) {
    		$add = array(
    				'admin_id' => $this->business_id,
    				
    				'title' => $data['title'],
    				'status' => empty($data['status']) ? 1 : $data['status'],
    				'sort' => $data['sort'],
    				'addtime' => time(),
    			); 
    		if (empty($_FILES['img']['name'])) {
    			$this->error('Please upload pictures');die;
    		} else {
    			$upload = ToolsController::upload('cate/');
				if ($upload['status'] == -1) {
					$this->error('Image upload failed');die;
				} else {
					$add['img'] = $upload['file_path'];
				}
    		}


           /* $uploads = ToolsController::upload_vedio('vedio/');
                if ($uploads['status'] == -1) {
                    $this->error('视频上传失败');die;
                } else {
                    $add['vedio'] = $uploads['file_path'];
                }*/



    		$result = M('cate')->add($add);

    		

    		if ($result) {
    			$this->success('Successfully Saved');die;
    		} else {
    			
    			$this->error('Save failed');die;
    		}
    	}

    	$this->assign('op', 'Add');
    	$this->display();
    }

	/**
     * 编辑轮播图
     */
    public function edit($id)
    {	
    	$data = I('post.');
    	if (!empty($data)) {
            $info = M('cate')->where(array('id' => $data['id']))->find();
    		$save = array(
    				'title' => $data['title'],
                    'status' => $data['status'],
    				'sort' => $data['sort']
    			);

    		if (!empty($_FILES['img']['name'])) {
				$upload = ToolsController::upload('cate/');
				if ($upload['status'] == -1) {
					$this->error('Image upload failed');die;
				} else {
					$save['img'] = $upload['file_path'];
				}


			}

            


			$result = M('cate')->where(array('id' => $data['id']))->save($save);

			if ($result) {
				if (!empty($_FILES['img']['name'])) {
					unlink($this->root.'/Uploads/'.$info['img']);
				}

                

				$this->success('Successfully Edited');die;	
			} else {
				if (!empty($_FILES['img']['name']) && $upload['status'] == 1) {
					unlink($this->root.'/Uploads/'.$upload['file_path']);
				}

                 
				$this->error('update successfully');die;	
			}
    	}
        $info = M('cate')->where(array('id' => $id))->find();
        $info['img'] = __ROOT__.'/Uploads/'.$info['img'];
        //$info['vedio'] = __ROOT__.'/Uploads/'.$info['vedio'];
    	$this->assign('op', 'Edit');
    	$this->assign('info', $info);
    	$this->display('add');
    } 

    /**
     * 删除轮播图
     */ 
    public function del($id)
    {   
        $where = array('id' => $id);
        
        $info = M('cate')->where($where)->find();
        

        $result = M('cate')->where($where)->delete();
        if ($result) {
            unlink($this->root.'/Uploads/'.$info['img']);
           
            $this->success('Successfully Deleted');die;
        } else {
            $this->error('Delete failed');die;
        }
    }


     public function kaiguan()
        {   
            $data = I('post.');
            if(!empty($data)){
                $save['status']=$data['status'];
                $result = M('chart_config')->where(array('id' => $data['id']))->save($save);
                if($result){
                    $this->success('Successfully Edited');die;
                }else{
                     $this->error('Edit failure');die;
                }

            }
            $config = M('chart_config')->where('admin_id='.session('admin_id'))->find();
            $this->assign('config', $config);
            $this->display();
        }
}