<?php
/**
 * [LJCMS] (C)2012-2099 LJdev,Inc.
 * <E-Mail：aspcms@qq.com>
 * Url http://www.liangjing.org
 *     http://www.8CMS.com
 * Update 2014.04.03
*/

class control extends salebase {
        public function action_run() {
                $search=$_REQUEST['search'];
                $keyword=$_REQUEST['keyword'];
                $model = parent::model('customer', 'sa');
                $searchsql = " ";
                if($search=='kehu_name'){
                     $searchsql .= " AND lower(kehu_name) LIKE '%".strtolower($keyword)."%'";
                }elseif ($search=='kehu_number') {
                        $searchsql .= " AND lower(kehu_number) LIKE '%".strtolower($keyword)."%'";
                }
                list($total, $data) = $model->getList(array('page'=>$this->page, 'pagesize'=>$this->pagesize, 'searchsql'=>$searchsql));
                
                foreach($data as $key=>$value){
                    $ying_money=0;
                    $data[$key]['ying_money']=$ying_money;
                    $data[$key]['shi_money']=$ying_money-$value['kehu_money'];
                }
                $url = XRequest::getPhpSelf();
                $url .= '?c=Customer';
                $var_array = array(
                    'page'=>$this->page,
                    'nextpage'=>$this->page+1,
                    'prepage'=>$this->page-1,
                    'pagecount'=>ceil($total/$this->pagesize),
                    'pagesize'=>$this->pagesize,
                    'total'=>$total,
                    'showpage'=>XPage::index($total, $this->pagesize, $this->page, $url, 10),
                    'list'=>$data,
                    'keyword'=>$keyword,
                    'search'=>$search,
                );
               unset($model);
               TPL::assign($var_array);
	       TPL::display($this->cptpl.'customer/index.tpl');
    }
    
        public function action_add(){
            $id=$_REQUEST['id'];
            $model = parent::model('customer', 'sa');
            $opmode="add";
            if($id){
                $opmode="edit";
                $vo=$model->get_data($id);
                $number=$vo['kehu_number'];
            } else {
                $next_id=$model->popup($id);
                $number="KH".sprintf("%04d", $next_id);
            }
             $var_array = array(
                    'opmode'=>$opmode,
                    'vo'=>$vo,
                    'number'=>$number
                );            
            TPL::assign($var_array);          
            TPL::display($this->cptpl.'customer/add.tpl');
    }
        public function action_save(){
            $arr=$_POST;
            $id=$arr['id'];
            $model = parent::model('customer', 'sa');
            if($arr['opmode']=='add'){
                $result = $model->doadd($arr);
            }else{
                $result = $model->doEdit($id, $arr);
            }
            if (true === $result) {
                $data['code']=1;
                $data['msg']="编辑成功";
            }
            else {
                $data['msg']="编辑失败";
            }
            echo json_encode($data);
        }
        public function action_del(){
                $ids = XRequest::getArray('id');
                $ids = substr($ids,0,strlen($ids)-1); ;
                $array_id = explode(",", $ids);
                if(empty($array_id)){
                     $data['msg']='操作失败';
                     echo json_encode($data);
                die;
                }
                $model = parent::model('customer', 'sa');
                foreach($array_id as $v){
                   $res=$model->del($v);
                }
	
                if($res){
                     $data['code']=1;
                     $data['msg']='删除成功';
                } else {
                     $data['msg']='操作失败';
                }
                 echo json_encode($data);
                die;
	}
}