<?php
/**
 * 呼叫中心控制器
 */
namespace Admin\Controller;
class CallLogController extends BaseController{
    public function _initialize(){
        parent::_initialize();
        $check=check_use_auth('Call');
        if(empty($check['status'])){
            $this->error($check['msg'],$check['url']);return;
        }
    }
    public function index(){
        $param = I('param.');
        $map = array();
        if ($param['shop_id']) {
            $map['shop_id'] = $param['shop_id'];
        }
        if ($param['call_type']) {
            $map['call_type'] = $param['call_type'];
        }
        if ($param['shop_name']) {
            $where = array();
            $where['name'] = array('like', '%' . $param['shop_name'] . '%');
            $shop_ids = M('Shops')->where($where)->getField('id', true);
            if (!empty($shop_ids))
                $map['shop_id'] = array('in', $shop_ids);
            else
                $map['shop_id'] = 0;
        }
        if ($param['username']) {
            $where = array();
            $where['username|nickname|mobile'] = array('like', '%' . $param['username'] .'%');
            $uids = M('Member')->where($where)->getField('id', true);
            if (!empty($uids))
                $map['uid'] = array('in', $uids);
            else
                $map['uid'] = 0;
        }
        if (!empty($param['caller'])) {
            $map['caller'] = array('like', '%' . $param['caller'] . '%');
        }
        if (!empty($param['calles'])) {
            $map['calles'] = array('like', '%' . $param['calles'] . '%');
        }
        if (!empty($param['begin_time']))
            $map['addtime'][] = array('egt', strtotime($param['begin_time']));
        if (!empty($param['end_time']))
            $map['addtime'][] = array('elt', strtotime($param['end_time']));
        $list = $this->lists('CallLog', $map, 'id desc');
        foreach ($list as $key => $value) {
            if ($value['uid']) {
                $list[$key]['nickname'] = show_user_name($value['uid']);
            }
            if ($value['shop_id']) {
                $list[$key]['shop_name'] = idtoname($value['shop_id'], 'Shops', 'name');
            }
        }
        $map = array();
        $map['status'] = 1;
        $shop_list = M('Shops')->where($map)->select();
        $this->assign('list', $list);
        $this->assign('shop_list', $shop_list);
        $this->assign($param);
        $this->display();
    }
    public function sitting(){
        $post=I('post.');
        if($post['do_submit']){
            if(empty($post['name'])){
                $this->error('请填写工作台名称');
            }
            $configname='CALL_CENTER_NAME';
            $map=array();
            $map['name']=$configname;
            $check=M('Config')->where($map)->count();
            $data=array();
            $data['update_time']=NOW_TIME;
            $data['status']=1;
            $data['value']=trim($post['name']);
            if($check){
                $result=M('Config')->where($map)->save($data);
            }else{
            $data['name']=$configname;
                $data['type']=1;
                $data['title']='工作台名称';
                $data['group']=0;
                $data['create_time']=NOW_TIME;
                $result=M('Config')->add($data);
            }
            if($result!=false){
                S('DB_CONFIG_DATA', null);
                $this->success('设置成功');
            }else{
                $this->error('设置失败');
            }
        }else{
           $this->display(); 
        }
    }
    public function _before_delete(){
        $id = I('id');
        $this->id = $id;
    }
}
?>