<?php
/**
 * 消息管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class MessageController extends BaseController
{
    public function index() {
        $postdata=I('param.');
        $this->assign($postdata);
		$map = array();
        if($postdata['time_frame'] && $postdata['time_end']){
            $map['addtime'] = array(array('gt',strtotime($postdata['time_frame'])),array('lt',strtotime($postdata['time_end'])));
        }
        if($postdata['status'] == 2){
            $map['status'] = 0;
        }else if($postdata['status'] == 1){
			$map['status'] = 1;
		}
        $map['type']=$postdata['type'];
        if(!empty($param_map))$map=empty($map)?$param_map:array_merge($map,$param_map);
        if(!empty($map['type']))$this->get_type($map['type']);
        else $this->get_type(0);
        $list = $this->lists(CONTROLLER_NAME,$map);
        $this->assign('list', $list);
        $this->display();
    }
    public function _before_add(){
        $this->get_type(0,'add');
        $this->send_type();
    }
    /**
     * Ajax搜索商品
     */
    public function ajaxgetgoods()
    {
        $keyword = I('keyword');
        $map = array();
        $map['shop_id'] = 0;
        $map['is_delete'] = 0;
        $map['publish_wait'] = 1;
        if (trim($keyword) != '') $map['goods_name'] = array('like', '%' . $keyword . '%');
        $list_goods = M("Goods")->field('goods_name,id,shop_price')->where($map)->select();
        $data_goods = "<option value=''>请选择商品</option>";
        foreach ($list_goods as $val_goods) {
            $data_goods .= '<option value="' . $val_goods['id'] . '" data="' . $val_goods['shop_price'] . '">' . $val_goods['goods_name'] . '</option>';
        }
        $result['list'] = $data_goods;
        $this->ajaxReturn($result);
    }
    public function insert()
    {
        $postdata = I('post.');
        $model = D(CONTROLLER_NAME);
        $map = array();
        if($postdata['type'] == 1){//会员
            $map['status']=array('GT',0);
            $member = M('Member')->where($map)->field('id')->select();
            foreach ($member as $k => $v) {
                $postdata['rank_select_right'][] = $v['id'];
            }
        }else{//仓库
            $map['status']=1;
            $shop = M('ShopsAccount')->where($map)->field('id')->select();
            foreach ($shop as $k => $v) {
                $postdata['rank_select_right'][] = $v['id'];
            }
        }
        $uidarr=$postdata['rank_select_right'];
        $basedata['from'] = $postdata['fromuid'];
        $basedata['title'] = $postdata['title'];
        $basedata['content'] = $postdata['content'];
        $basedata['type'] = $postdata['type'];
        $basedata['addtime'] = $postdata['addtime'];
        if(empty($postdata['send_type'])) $this->error('请选择发送类型');
        
       
        
        
        foreach ($uidarr as $key => $v) {
            $adddata[$key] = $basedata;
            $adddata[$key]['touid']=$v;

            foreach ($postdata['send_type'] as $val) {
                 //
               if($val==1){
                    if($postdata['type']==3){
                        $adddata[$key]['to'] = idtoname($v,'Suppliers','email');
                    }else {
                        $adddata[$key]['to'] = idtoname($v,'Member','email');
                    }
                    $result=send_method($val,$adddata[$key]);
                }elseif($val==2){
                    if($postdata['type']==2){
                        $map = array();
                        $map['shop_id'] = $v;
                        $map['pid'] = 0;
                        $adddata[$key]['to'] = M('ShopsAccount')->where($map)->getField('mobile');
                    }else if($postdata['type']==3){
                        $adddata[$key]['to'] = idtoname($v,'Suppliers','phone');
                    }else {
                        $adddata[$key]['to'] = idtoname($v,'Member','mobile');
                    }
                    $result=send_method($val,$adddata[$key]);
                }elseif($val==3){
                   $adddata[$key]['to'] = $v;
                   $result=send_method($val,$adddata[$key]);
               }
            }
        }
        
        
        if($uidarr && in_array(5,$postdata['send_type'])){
            $pushdata['to'] = $uidarr[0];
            $pushdata['push_type']='app';
            $pushdata['content']='您有新的系统消息!';
            send_method(5,$pushdata);
            $result['status']=1;
        }
        
        if (!empty($result)) {
            if ($_POST['isajax']) $this->assign('dialog', '1');
            $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U(CONTROLLER_NAME . '/index');
            $this->assign('jumpUrl', $jumpUrl);
            $this->success(L('ADD_OK'));
        } else {
            $this->error(L('ADD_ERROR') . ': ' . $model->getDbError());
        }
    }

    // 发送信息
    public function send_information(){
        $postdata = I('post.');
        // $post['fromuid'] = $postdata['fromuid'];
        $post['type'] = 1;
        $post['send_type'] = explode(',',$postdata['send_type']);
        $post['title'] = $postdata['title'];
        $post['content'] = $postdata['content'];
        $post['rank_select'] = $postdata['rank_select'];
        $post['rank_select_right'] = explode(',',$postdata['rank_select_right']);     
        $post['id'] = $postdata['id'];
        $post['addtime'] = $postdata['addtime'];
        
        $model = D(CONTROLLER_NAME);
        $uidarr=$post['rank_select_right'];
        if(!$uidarr)$this->error('请选择收信人！');
        // $basedata['from'] = $post['fromuid'];
        $basedata['title'] = $post['title'];
        $basedata['content'] = $post['content'];
        $basedata['type'] = $post['type'];
        $basedata['addtime'] = $post['addtime'];

        if(empty($post['send_type'])) $this->error('请选择通知类型');
        foreach ($uidarr as $key => $v) {
            $adddata[$key] = $basedata;
            $adddata[$key]['touid']=$v;
            foreach ($post['send_type'] as $val) {
                 //
               if($val==1){
                    if($post['type']==3){
                        $adddata[$key]['to'] = idtoname($v,'Suppliers','email');
                    }else {
                        $adddata[$key]['to'] = idtoname($v,'Member','email');
                    }
                    $result=send_method($val,$adddata[$key]);
                }elseif($val==2){
                    if($post['type']==2){
                        $map = array();
                        $map['shop_id'] = $v;
                        $map['pid'] = 0;
                        $adddata[$key]['to'] = M('ShopsAccount')->where($map)->getField('mobile');
                    }else if($post['type']==3){
                        $adddata[$key]['to'] = idtoname($v,'Suppliers','phone');
                    }else {
                        $adddata[$key]['to'] = idtoname($v,'Member','mobile');
                    }

                    $result=send_method($val,$adddata[$key]);
                }else{
                    $adddata[$key]['to'] = $v;
                    $result=send_method($val,$adddata[$key]);
                }
            }
        }

        if (!empty($result)) {
            if ($_POST['isajax']) $this->assign('dialog', '1');
            $jumpUrl = $_POST['forward'] ? $_POST['forward'] : U(CONTROLLER_NAME . '/index');
            $this->assign('jumpUrl', $jumpUrl);
            $this->success(L('ADD_OK'));
        } else {
            $this->error(L('ADD_ERROR') . ': ' . $model->getDbError());
        }
    }

    /**
     * 得到消息类型并传到页面
     * @param $type
     * @param $t    如果是发送消息,$t='add'
     */
    public function get_type($type,$t='')
    {
        $type_array=array();
        $type_array['1']='会员';
        $type_array['2']='仓库';
        // $type_array['3']='供货商';
        $this->assign('type_array', $type_array);
        if(empty($t)) {
            $select_type = '';
            foreach ($type_array as $k => $v) {
                $select_type .= '<option value="' . $k . '"';
                if ($type == $k) $select_type .= 'selected="selected"';
                $select_type .= '>' . $v . '</option>';
            }
            $this->assign('select_type', $select_type);
        }
    }
    /**
      *得到发送类型
      */
    private function send_type(){
        $map['pid']=2;
        $map['status']=1;
        $send_list=M('Type')->where($map)->field('id,name')->select();
        $send_type = array();
        if (!empty($send_list)) {
            foreach ($send_list as $v) {
                $send_type[$v['id']] = $v['name'];
            }
        }
        $this->assign('send_type', $send_type);
    }
    //收信人搜索
    public function searchusers()
    {
        $c = I('get.');
        if($c['type']==1){
            if (!empty($c['keyword'])) {
                $user = $c['keyword'];
                if (is_numeric($user)) {
                    $map['id|mobile'] = array(intval($user), array('like', '%' . $user . '%'), '_multi' => true);
                } else if ($user) {
                    $map['username'] = array('like', '%' . (string)$user . '%');
                }
            }
            $list = M('Member')->where($map)->field('id,username')->select();
            $result = '';
            foreach ($list as $value) {
                $result .= "<option value='" . $value['id'] . "'>" . $value['username'] . "</option>";
            }
        }
        if($c['type']==2){
            if (!empty($c['keyword'])) {
                $user = $c['keyword'];
                if (is_numeric($user)) {
                    $map['id|phone'] = array(intval($user), array('like', '%' . $user . '%'), '_multi' => true);
                } else if ($user) {
                    $map['name'] = array('like', '%' . (string)$user . '%');
                }
            }
            $map['pid'] = 0;
            $list = M('Shops')->where($map)->field('id,name')->select();
            $result = '';
            foreach ($list as $value) {
                $result .= "<option value='" . $value['id'] . "'>" . $value['name'] . "</option>";
            }
        }
        if($c['type']==3){
            if (!empty($c['keyword'])) {
                $user = $c['keyword'];
                if (is_numeric($user)) {
                    $map['id|phone'] = array(intval($user), array('like', '%' . $user . '%'), '_multi' => true);
                } else if ($user) {
                    $map['name'] = array('like', '%' . (string)$user . '%');
                }
            }
            $list = M('Suppliers')->where($map)->field('id,name')->select();
            $result = '';
            foreach ($list as $value) {
                $result .= "<option value='" . $value['id'] . "'>" . $value['name'] . "</option>";
            }
        }
        echo $result;
        exit;
    }
}