<?php
/**
 * ============================================================================
 * 版权所有 福建荣升信息技术有限公司，保留所有权利。
 * 网站地址: http://www.fjrsweb.cn；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
*/
header('Access-Control-Allow-Origin:*'); 

dd_session_start();

$key=$webset['key'];
unset($data['val']);  //不清楚 哪里引的

$act1=$_REQUEST['act1'];

if ($act1=="menu"){
	$data['role_id']=$dduser['role'];
    $menu=new menu($data,$duoduo);
 	$res=$menu->get_menu();
 	$menudata['homeInfo']['title']="会员中心";
 	if ($dduser['role']==YG)
 	    $menudata['homeInfo']['href']=u("income","total");
 	else
 		$menudata['homeInfo']['href']=u("user","index");
    $menudata['logoInfo']['title']="shopee";
	$menudata['logoInfo']['image']=$webset['logo'];
	$menudata['menuInfo']=$res['data'];
	echo json_encode($menudata,JSON_UNESCAPED_UNICODE);exit();
		
}

if ($act1=="login"){ 
	app("user")->login();
}
/**
 * [$act1 不同角色显示员工，一种管理员，一种合伙人]
 * @var [type]
 */
  if ($act1=="user_list"){
	   $list=app("user")->uses_role_list($dduser['id']);
	   foreach ($list as $key => $value) {
	   		$list[$key]['rolename']=db()->get_field("role",$value['role']);
	   	}	
	   json(0,"",$list,count($list));
	}
 //改变字段值
  if ($act1=="changefield"){
  	  $res=db()->changefield($_REQUEST);  
  }

  if ($act1=="get_stock_price"){
  	   $price=0;
  	   if (!empty($_COOKIE['sdata'])){
           $data['price']=app("order_goods")->cookieStockPrice();
           json(0,"",$data);
       }else{

     	   json(1,"异常找不到cookie");
        }
  }
  if ($act1=="gt"){
  	  $id=$_REQUEST['id'];
  	  $data=db()->select("incometype","gt","id=$id");
  	  json(0,"",$data);
  }
  if ($act1=="list"){
	
	  $pagesize=4;
	 $northeast=json_decode($_REQUEST['northeast'],true);
	 $southwest=json_decode($_REQUEST['southwest'],true);
	 $latitude1=$southwest['latitude'];
	 $latitude2=$northeast['latitude'];
	 $longitude1=$southwest['longitude'];
	 $longitude2=$northeast['longitude'];
	 $where="latitude>=$latitude1 and latitude<=$latitude2 and longitude>=$longitude1 and longitude<=$longitude2";
	  $type=$_REQUEST['type'];
	  if (!empty($type)){
	  		 $where.=" and type=$type";
	  }
	  $page = empty($_POST['page'])?'1':intval($_POST['page']);
	  $page2=($page-1)*$pagesize;
	  $sql="select id,title,address,count(id) from xia_company where $where group by title order by sort desc,id desc limit $page2,$pagesize";
	  $list=$duoduo->sql_to_array($sql);
	  //$rows=$duoduo->select_all('company',"id,title,address,longitude,latitude","$where  order by sort desc,id desc limit $page2,$pagesize");
	  $sql="select id from xia_company where $where group by title";
	  $list_total=$duoduo->sql_to_array($sql);
	  $total=count($list_total);
	  $result['list']=$list;
	  $result['total']=$total;
	  $result['pagesize']=$pagesize;
	  echo dd_json_encode($result);exit(); 
  }
  
  if ($act1=="order_import"){
 
	if(empty($_FILES["file"]))
	  json(1,"系统异常");


	$file_name=upload("file",$saved_upload_name);

	//$file_name="demo/order.xlsx";
	app("order")->import($file_name);
	
 }
   if ($act1=="income_import"){
 
	if(empty($_FILES["file"]))
	  json(1,"系统异常");


	$file_name=upload("file",$saved_upload_name);

	//$file_name="demo/income.xlsx";
	app("income")->import($file_name);
	
 }
 if ($act1=="password"){
	 
	   if (empty($_POST['ddpwd']) or empty($_POST['pwd_confirm'])  or empty($_POST['old_pwd'])){
				 	$result['msg']="密码不能为空";
	                echo dd_json_encode($result);exit();
				}
			if ($_POST['ddpwd'] != $_POST['pwd_confirm']) { // 两次密码对比
				
					$result['msg']="两次输入的密码不一致";
	                echo dd_json_encode($result);exit();
				
			} 
			if (reg_password($_POST['ddpwd']) == 0) { // 密码格式
				//$re = json_encode(array('s' => 0, 'id' => 3));
				//dd_exit($re);
					$result['msg']="密码格式错误";
	                echo dd_json_encode($result);exit();
			} 
	
			if ($duoduo -> check_oldpass($_POST['old_pwd'], $dduser['id']) == 'false') {
				//$re = json_encode(array('s' => 0, 'id' => 4));
				//dd_exit($re);
				$result['msg']="旧密码错误";
	             echo dd_json_encode($result);exit();
			} 

			$field_arr['ddpassword'] = md5($_POST['ddpwd']);
			$duoduo -> update('user', $field_arr, 'id=' . $dduser['id']);
			user_login($dduser['id'], $field_arr['ddpassword'], $userlogininfo['ddsavetime']); //重置登陆状态

			
			$result['msg']="修改成功";
			$result['code']=1;
	        echo dd_json_encode($result);exit();
	}
	
	 if ($act1=="project_list"){
	     $where="1=1";
	   
	   $list=$duoduo->select_all("project","*","1=1 order by sort desc, id desc");
	   if (count($list)>0){
		   $result['code']=0;
		   $result['count']=count($list);
		   $result['msg']="";
		   $result['data']=$list;  
	   }else{
		    $result['code']=0;
			$result['msg']="空数据";
			$result['data']=array();  
	   }

	    echo json_encode($result,JSON_UNESCAPED_UNICODE);exit();
	  
	}
if ($act1=="webset"){
    del_magic_quotes_gpc($_POST,1);
	$diff_arr=array('sub','from');
	$_POST=logout_key($_POST, $diff_arr);

	foreach($_POST as $k=>$v){
		if(is_array($v)){
			$post_arr = $duoduo->webset_part($k,$v);
			$v=$post_arr[$k];
		}
		else{
			$v=trim($v);
		}
		$duoduo->set_webset($k,$v);
	}
	$duoduo->webset(); //配置缓存
    $result['code']=1;
	$result['msg']="修改成功";
	echo json_encode($result,JSON_UNESCAPED_UNICODE);exit();
	
}
if ($act1=="echartsPies"){
   $rows=$duoduo->select_all("project","*","pid=0 order by sort desc,id desc");
   $where="1=1";
   if ($dduser['role']==6){
		
		$uids=$duoduo->select_all("user","id","pid=".$dduser['id']);
		foreach ($uids as $vo){
		        $temp[]=$vo['id'];	
		} 
	   $uids_str= implode(",",$temp);
	   $where.=" and uid in ($uids_str)";
	}
	if ($dduser['role']==7){
		$where.=" and uid=".$dduser['id'];
	}

	if ($_REQUEST['sdate']){
	    $datawhere=" and adddate>='".$_REQUEST['sdate']."'";
	}
   	foreach ($rows as $k=>$vo){
		$legend_data[]=$vo['title'];

		$series_data[$k]['value']=$duoduo->select("ht","sum(price)","$where and project_fid=".$vo['id'].$datawhere);
		$series_data[$k]['name']=$vo['title'];
		
	}
	$result['code']=1;
	$result['legend_data']=$legend_data;
	$result['series_data']=$series_data;
	echo json_encode($result,JSON_UNESCAPED_UNICODE);exit();
}

if ($act1=="recycle"){
   //删除先进回收站
   $ids=explode(",",$_REQUEST['ids']);

   	foreach ($ids as $id){
		 $duoduo->update("ht",array("del"=>2),"id=$id");
	} 	
	$data['code']=1;
	echo dd_json_encode($data);
}
if ($act1=="norecycle"){
   //删除先进回收站
   $ids=explode(",",$_REQUEST['ids']);

   	foreach ($ids as $id){
		 $duoduo->update("ht",array("del"=>1),"id=$id");
	} 	
	$data['code']=1;
	echo dd_json_encode($data);
}
if ($act1=="set"){
	
	$set['bdesc']=$webset['bdesc'];
	$set['desc']=$webset['desc'];
	$types=$duoduo->select_all("arr","id,title","cid=26 order by sort desc ,id desc");
	$obj['id']=0;
	$obj['title']="全部";
	array_unshift($types,$obj);
	
	foreach ($types as $k=>$vo){
		$types[$k]['title']=get_str($vo['title'],4);
		
	}
	$result['types']=$types;
	$result['webset']=$set;
	echo dd_json_encode($result);exit();
	
	
}

if ($act1=="search_c"){

	$q=$_REQUEST['q'];
	$rows=$duoduo->select_all("ht","distinct title,address,charge,charge_phone","title like '%$q%' limit 0,10");
    $html="";
	foreach ($rows as $key => $value) {
		$html.="<p  class='scompany' charge='".$value['charge']."' charge_phone='".$value['charge_phone']."'  address='".$value['address']."''>".$value['title']."</p>";
	}
	$result['html']=$html;
	echo dd_json_encode($result);exit();
}