<?php
namespace Smi\Controller;

class PreController extends BaseController
{	
	/**
	 * [index 优惠券列表]
	 * @AuthorHTL
	 * @DateTime  2018-02-23T11:31:20+0800
	 * @return    [type]                   [description]
	 */
	public function index()
	{
		$list = M('pre')->where(array('admin_id' => session('admin_id')))->select();
		$this->assign('list', $list);
		$this->display();
	}

	/**
	 * [add 添加优惠券]
	 * @AuthorHTL
	 * @DateTime  2018-02-23T11:31:39+0800
	 */
	public function add()
	{
		$data = I('post.');

		if (!empty($data)) {
			$add = array(
					'admin_id' => session('admin_id'),
					'pre_money' => $data['pre_money'],
					'full_money' => $data['full_money'],
					'validity_day' => $data['validity_day'],
					'nums' => $data['nums'],
					'num' => $data['num'],
					'create_time' => time(),
					'update_time' => time(),
				);
			$result = M('pre')->add($add);

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

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

	/**
	 * [edit 编辑优惠券]
	 * @AuthorHTL
	 * @DateTime  2018-02-23T11:32:00+0800
	 * @return    [type]                   [description]
	 */
	public function edit($id)
	{	
		$data = I('post.');

		if (!empty($data)) {
			$save = array(
					'pre_money' => $data['pre_money'],
					'full_money' => $data['full_money'],
					'validity_day' => $data['validity_day'],
					'nums' => $data['nums'],
					'num' => $data['num'],
					'update_time' => time(),
				);
			$result = M('pre')->where(array('id' => $data['id']))->save($save);

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

		$info = M('pre')->where(array('id' => $id))->find();

		$this->assign('info', $info);
		$this->assign('op', 'Edit coupons');
		$this->display('add');
	}

	/**
	 * [delete 删除优惠券]
	 * @AuthorHTL
	 * @DateTime  2018-02-23T11:32:18+0800
	 * @return    [type]                   [description]
	 */
	public function delete($id)
	{
		$info = M('pre')->where(array('id' => $id))->find();

		if ($info['admin_id'] != session('admin_id')) {
			$this->error('Illegal operation');die;
		}	

		$result = M('pre')->where(array('id' => $id))->delete();
		if ($result) {
			$this->success('Successfully Deleted');die;
		} else {
			$this->error('Delete failed');die;
		}
	}

	/**
	 * [set_red 设置为红包]
	 * @AuthorHTL
	 * @DateTime  2018-03-15T17:08:01+0800
	 */
	public function set_red()
	{
		$data = I('post.');
	
		if (empty($data['id'])) $this->ajaxReturn(['status' => -1, 'msg' => 'Id is empty']);
		
		$business_id = session('admin_id');

		$is_has_red = M('pre')->where(array('admin_id' => $business_id, 'is_red' => 1))->find();
		
		if ($is_has_red) {
			$this->ajaxReturn(['status' => -1, 'msg' => 'You cannot set more than one red envelope']);
		} else {
			$result = M('pre')->where(array('id' => $data['id']))->setField('is_red', 1);
			if ($result) {
				$this->ajaxReturn(['status' => 1, 'msg' => 'Successfully Setup As Red Packet']);
			} else {
				$this->ajaxReturn(['status' => -1, 'msg' => 'Failed to set red envelope']);
			}
		}
	}


	//2018-6-29
	public function preimg()
	{
		$list = M('preimg')->where(array('admin_id' => session('admin_id')))->select();
        $this->assign('list', $list);
    	$this->display();
	}

	//2018-6-29
	public function addpreimg()
	{
		$data = I('post.');

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

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

    		

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

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

	//2018-6-29
	 public function editpreimg($id)
    {	
    	$data = I('post.');
    	if (!empty($data)) {
            $info = M('preimg')->where(array('id' => $data['id']))->find();
    		$save = array(
    				
                    'status' => $data['status']
    				
    			);

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

			$result = M('preimg')->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 failed');die;	
			}
    	}
        $info = M('preimg')->where(array('id' => $id))->find();
        $info['img'] = __ROOT__.'/Uploads/'.$info['img'];
    	$this->assign('op', 'edit');
    	$this->assign('info', $info);
    	$this->display('addpreimg');
    } 


    //2018-6-29
    public function delpreimg($id)
    {   
        $where = array('id' => $id);
        
        $info = M('preimg')->where($where)->find();
        

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