<?php
namespace Erpadmin\Controller;
use Think\Controller;
class AdController extends ErpadminbaseController {

	public function adPosition()
	{
		if (I('is_ajax')) {
            $field = I('field');//排序字段名
            $order = I('order');//排序方式 asc desc
            $limit = I('limit',30,'int');//每页条数
            $limit = $limit > 150 ? 150 : $limit;

            $where = " where a.id > 0";
            $keyword = I('keyword');
            if ($keyword) {
                $where .= " and a.adpTitle like '%{$keyword}%'";
            }
            
        	$Model = new \Think\Model;
            $count_sql = "SELECT COUNT(*) FROM __AD_POSITION__ a JOIN __ADMIN_USER__ b ON a.addUid = b.id LEFT JOIN __AD__ as c ON a.ad_id = c.id {$where}";
            $adp_count_arr = $Model->query($count_sql);
            $adp_count = $adp_count_arr[0]['COUNT(*)'];

            $Page = new \Think\Page($adp_count,$limit);

            $select_sql = "SELECT a.*,b.nickname,c.adTitle FROM __AD_POSITION__ a JOIN __ADMIN_USER__ b ON a.addUid = b.id LEFT JOIN __AD__ as c ON a.ad_id = c.id {$where} limit ".$Page->firstRow.",".$Page->listRows;
            $adp_select = $Model->query($select_sql);
        	foreach ($adp_select as $key => $value) {
        		$adp_select[$key]['addTime_d'] = $value['addTime']? date('Y-m-d',$value['addTime']):'';
        		$adp_select[$key]['endTime_d'] = $value['endTime']? date('Y-m-d',$value['endTime']):'';
        	}
            $return = array(
                'code'=>0,
                'msg'=>'',
                'count'=>$adp_count,
                'data'=>$adp_select
            );
            $this->ajaxReturn($return);
        } else {
        	$this->display('adPosition');
        }
	}

	public function index()
	{
		if (I('is_ajax')) {
            $field = I('field');//排序字段名
            $order = I('order');//排序方式 asc desc
            $limit = I('limit',30,'int');//每页条数
            $limit = $limit > 150 ? 150 : $limit;

            

        	$where = " where a.id > 0";
            $keyword = I('keyword');
            if ($keyword) {
                $where .= " and a.adTitle like '%{$keyword}%'";
            }
            
        	$Model = new \Think\Model;
            $count_sql = "SELECT COUNT(*) FROM __AD__ a JOIN __ADMIN_USER__ b ON a.addUid = b.id {$where}";
            $ad_count_arr = $Model->query($count_sql);
            $ad_count = $ad_count_arr[0]['COUNT(*)'];

            $Page = new \Think\Page($ad_count,$limit);

            $select_sql = "SELECT a.*,b.nickname FROM __AD__ a JOIN __ADMIN_USER__ b ON a.addUid = b.id {$where} limit ".$Page->firstRow.",".$Page->listRows;
            $ad_select = $Model->query($select_sql);

        	foreach ($ad_select as $key => $value) {
        		$ad_select[$key]['addTime_d'] = $value['addTime']? date('Y-m-d',$value['addTime']):'';
        		$ad_select[$key]['endTime_d'] = $value['endTime']? date('Y-m-d',$value['endTime']):'';
        	}
            $return = array(
                'code'=>0,
                'msg'=>'',
                'count'=>$ad_count,
                'data'=>$ad_select
            );
            $this->ajaxReturn($return);
        } else {
        	$this->display();
        }
	}
}