<?php

namespace Admin\Controller;

use Think\Controller;

/**
 * 福利广告控制器
 * @author 鹿文学
 */
class BoonController extends ThinkController {
    
    const model_name = 'Boon';

    /**
     * 福利广告列表
		 * @author 鹿文学
     */
    public function lists(){
				
				if(!empty($_REQUEST['title'])) {$extend['title']=$_REQUEST['title'];}
				
        
				$this->m_title = '福利广告';
				$this->m_url = 'Boon/lists';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'Merchandise/lists','status'=>1])->find());
				
				$extend['status'] = 1;
				
        parent::lists(self::model_name,$_GET['p'],$extend);
				
    }
		
		/**
     * 添加福利广告
		 * @author 鹿文学
     */
		public function add() {
			if(IS_POST) {
				
				$notice = D(self::model_name);
				
				if(!empty($_POST['start_time']) && !empty($_POST['end_time'])) {
					if(strtotime($_POST['start_time'])>=strtotime($_POST['end_time'])) {						
						$this->error('开始时间必须小于结束时间');
					}
				}
				
				if(empty($_POST['start_time'])) {
					$this->error('开始时间不能为空');
				}
				
				!empty($_POST['start_time']) || $_POST['start_time'] = date('Y-m-d H:i');
				
				if($notice->update($_POST)) {
					$this->success('新增成功',U('lists'));
				} else {
					$this->error($notice->getError());
				}
				
			} else {
				
				$this->meta_title = '新增福利广告';
						
				$this->m_title = '福利广告';
				$this->m_url = 'Boon/lists';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'Notice/lists','status'=>1])->find());
				
				
				$this->display();
			}
		}
		
		/**
     * 编辑福利广告
		 * @author 鹿文学
     */
		public function edit() {
			if(IS_POST) {
				$notice = D(self::model_name);
				if(!empty($_POST['start_time']) && !empty($_POST['end_time'])) {
					if(strtotime($_POST['start_time'])>=strtotime($_POST['end_time'])) {						
						$this->error('开始时间必须小于结束时间');
					}
				}
				
				if(empty($_POST['start_time'])) {
					$this->error('开始时间不能为空');
				}
				
				if($notice->update($_POST)) {
					$this->success('编辑成功',U('lists'));
				} else {
					$this->error($notice->getError());
				}
				
				
			} else {
				$map['id'] = $_REQUEST['id'];
				$data = D(self::model_name)->where($map)->find();
//                                if(!empty($data['cover'])) {
//                                    $data['cover_data'] = explode(',', $data['cover']);
//                                }
                                if(!empty($data['cover'])) {
                                        $valueArr = explode(',', $data['cover']);
                                        $str = '';
                                        foreach ($valueArr as $key1 => $value1) {
                                            $a = get_cover($value1, 'path');
                                            $str .= $a.',';
                                        }
                                        $data['valuedata'] = rtrim($str, ',');
                                }
				$this->assign('data',$data);
				
				$this->meta_title = '编辑福利广告';
						
				$this->m_title = '福利广告';
				$this->m_url = 'Boon/lists';
				$this->assign('commonset',M('Kuaijieicon')->where(['url'=>'Notice/lists','status'=>1])->find());
				
				
				$this->display();
			}
		}
		
		/**
     * 删除福利广告
		 * @param array/integer  $ids  要删除的福利广告编号
		 * @author 鹿文学
     */
		public function del($ids=null){
        $model = M('Model')->getByName(self::model_name); /*通过Model名称获取Model完整信息*/
        parent::del($model["id"],$ids);
    }
		
		
		/**
     * 更改福利广告优先级
		 * @param   integer  $id  		福利广告编号
		 * @param   integer  $value 	优先级数值
		 * @author 鹿文学
     */
		public function change_value($id,$value) {
			if(IS_POST) {
				if(!is_numeric($id) || $id<1) {$this->ajaxReturn(['status'=>0,'info'=>'数据有误'],'json');}
				if(!is_numeric($value) || $value<1) {$this->ajaxReturn(['status'=>0,'info'=>'请输入正整数'],'json');}
				
				$result = D(self::model_name)->where(['id'=>$id])->setField('sort',$value);		
				if($result) {
					$this->ajaxReturn(['status'=>1,'info'=>'排序修改成功'],'json');
				} else {
					$this->ajaxReturn(['status'=>0,'info'=>'排序修改失败'],'json');
				}
				
			} else {
				$this->ajaxReturn(['status'=>0,'info'=>'请求有误'],'json');
			}
		}

		
    

}