<?php
namespace Api\Controller;

use Think\Controller;
use Think\Exception;
use Think\Log;

header("Content-Type:text/html;CharSet=UTF-8");
class KaiguanController extends Controller
{	
	//分类总开关2018-7-2
	public function catConfig()
	{
        $business_id=I('business_id');
		$list = M('cat_config')->where(array('admin_id' => $business_id))->find();
        $status=$list['status'];
		//if (empty($list)) $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        if(empty($list)){
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => 1]);
        }else{
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $status]);
        }

		
	}

    //导航总开关2018-7-2
    public function navConfig()
    {
        $business_id=I('business_id');
        $list = M('nav_config')->where(array('admin_id' => $business_id))->find();
        $status=$list['status'];
        //if (empty($list)) $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        if(empty($list)){
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => 1]);
        }else{
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $status]);
        }

        
    }


    //轮播总开关2018-7-2
    public function chartConfig()
    {
        $business_id=I('business_id');
        $list = M('chart_config')->where(array('admin_id' => $business_id))->find();
        $status=$list['status'];
        //if (empty($list)) $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        if(empty($list)){
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => 1]);
        }else{
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $status]);
        }

        
    }


    //获取导航列表
    public function getNav()
    {
        $business_id=I('business_id');
        $list = M('nav')->where('admin_id='.$business_id)->select();
       
        if (empty($list)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
        }

        
    }




    //获取轮播图列表
    public function getChart()
    {
        $business_id=I('business_id');
        $list = M('chart')->where('admin_id='.$business_id.' and status=1')->select();
       
        if (empty($list)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
        }

        
    }



    //获取优惠劵图片列表
    public function getPreimg()
    {
        $business_id=I('business_id');
        $list = M('preimg')->where('admin_id='.$business_id.' and status=1')->order('create_time desc')->limit(2)->select();
       
        if (empty($list)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
        }

        
    }


    //获取视频
    public function getVedio()
    {
        $business_id=I('business_id');
        $list = M('vedio')->where('admin_id='.$business_id.' and status=1')->limit(1)->find();
       
        if (empty($list)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
        }

        
    }

    //获取商品分类
    public function getCat()
    {
        $business_id=I('business_id');
        //$list = M('goods_cat')->where('admin_id='.$this->data['business_id'].' and status=0 and home_show=1')->order('seat_no')->select();
         $list = M('cate')->where('admin_id='.$business_id.' and status=1')->order('sort asc')->select();
       
        if (empty($list)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
        }

        
    }



    //获取消费模式
    public function getConsum()
    {
         $business_id=I('business_id');
        $list = M('preimg')->where('admin_id='.$business_id.' and status=1')->select();
       
        if (empty($list)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
        }

        
    }





     /**
     * 支付
     * @param int $order_id  订单编号
     * @return array
     */
    public function payMoney(){
        $user_id=I('user_id');
        $money=I('money');
        $business_id=I('business_id');
       $order_sn = str_replace(' ', '', str_replace(':', '', str_replace('-', '', date('Y-m-d H:i:s').rand(100000, 999999))));// 生成订单编号
       $business_info = M('admin')->where(array('id' => $business_id))->field('delivery, boxes, business_name, is_on_waimai, up_to_send, dada_origin_shop_id')->limit(1)->select()[0];// 商户信息
       $add_data = [
                        
                        
                        'create_time'    =>  time(),
                        'status'         =>  1,
                        'order_sn'       =>  $order_sn,
                        'user_id'            =>   $user_id,
                        'admin_id'       =>  $business_id,
                       
                        
                        'money'    =>  $money,
                        'admin_title'    =>  $business_info['business_name'],
                       
                       
                       
                    ];
                if($order_id = M('zhi_order')->add($add_data)){
                    $order=M('zhi_order')->where('id='.$order_id)->find();
                    $user=M('users')->where('id='.$user_id)->find();
                    $openid=$user['openid'];
                    $out_trade_no=$order['order_sn'];
                    $admin_id=$order['admin_id'];
                    
                    $parent_id = M('admin')->where(array('id' => $business_id))->getField('parent_id');
                    if (!empty($parent_id)) {
                        $pay = & load_wechat('Pay',$parent_id);  
                    } else {
                        $pay = & load_wechat('Pay',$business_id);  
                    }
                    $notify_url='https://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'], '/')+1)."paysuccess/a_id/".$admin_id;

                    $result = $pay->getPrepayId($openid, $order['admin_title'], $out_trade_no, $money*100, $notify_url, $trade_type = "JSAPI");
                     if($result===FALSE){
                    // 接口失败的处理
                        echo json_encode(array('code' => '404', 'info' => '订单生成失败'.$pay->errMsg));die;
                        // return json(['msg' => '支付失败'], 200);
                    }else{
                        $time="".time();
                        $prepay_id='prepay_id='.$result['prepay_id'];
                        // 多门店处理,如果parent_id不等于0，获取父级id的微信配置参数
                        $parent_id = M('admin')->where(array('id' => $business_id))->getField('parent_id');
                        if (!empty($parent_id)) {
                            $result_ = M('wechat_config')->where('admin_id='.$parent_id)->field('partnerkey')->find();  
                        } else {
                          $result_ = M('wechat_config')->where('admin_id='.$business_id)->field('partnerkey')->find();  
                        }
                        $post="appId=".$result['appid']."&nonceStr=".$result['nonce_str']."&package=".$prepay_id."&signType=MD5&timeStamp=".$time."&key=".$result_['partnerkey'];
                        $key=md5($post);
                        $prepay_id='prepay_id='.$result['prepay_id'];
                        $result=['appId'=>$result['appid'],'timeStamp'=>$time,'nonceStr'=>$result['nonce_str'],'package'=>$prepay_id,'signType'=>'MD5','paySign'=>$key];
                       // M('order')->where("order_sn=".$order['order_sn'])->save(['result' => json_encode($result)]);
                        echo json_encode($result, 200);
                    }



                }



    }


     


    public function paysuccess($a_id){
        
        
        // 实例支付接口
        $pay = & load_wechat('Pay', $a_id);

        // 获取支付通知
        $notifyInfo = $pay->getNotify();
        // Log::record('测试日志信息');
        Log::record($notifyInfo);

        // 支付通知数据获取失败
        if($notifyInfo===FALSE){
               
            // 接口失败的处理
            Log::record($pay->errMsg);
        }else{
            //支付通知数据获取成功
            if ($notifyInfo['result_code'] == 'SUCCESS' && $notifyInfo['return_code'] == 'SUCCESS') {
                // 支付状态完全成功，可以更新订单的支付状态了
                $trade_no=$notifyInfo['out_trade_no'];
                $transaction_id=$notifyInfo['transaction_id'];
                //$this->paychange($trade_no,$transaction_id);
                //$data['transaction_id']=$transaction_id;
                //$data['order_sn']=$trade_no;
                $data['pay_time']=time();
                $data['status']=2;
                
               
                M('zhi_order')->where('order_sn='.$trade_no)->save($data);


                // @todo
                // 返回XML状态，至于XML数据可以自己生成，成功状态是必需要返回的。
                // <xml>
                //    return_code><![CDATA[SUCCESS]]></return_code>
                //    return_msg><![CDATA[OK]]></return_msg>
                // </xml>
                return xml(['return_code' => 'SUCCESS', 'return_msg' => 'DEAL WITH SUCCESS']);
            }
        }
    }



    public function test(){
        $notify_url='https://'.$_SERVER['HTTP_HOST'].substr($_SERVER['PHP_SELF'],0,strrpos($_SERVER['PHP_SELF'], '/')+1)."paysuccess/a_id/".$admin_id;
        print_r($notify_url);die;
     }



     public function getOrder(){
        $business_id=I('business_id');
        $user_id=I('user_id');
        $list = M('zhi_order')->where('admin_id='.$business_id.' and user_id='.$user_id)->order('create_time desc')->select();

       
        if (empty($list)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
        }

     }



     public function delOrder(){
        $id=I('id');
        $row = M('zhi_order')->where('id='.$id)->delete();
        
       
        if (empty($row)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '删除失败']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '删除成功']);
        }

     }



     public function getNotice(){
        $business_id=I('business_id');
        //$user_id=I('user_id');
        $list = M('notice')->where('admin_id='.$business_id)->order('create_time desc')->select();

       
        if (empty($list)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
        }

     }



     public function footimg(){
        $business_id=I('business_id');
        //$user_id=I('user_id');
        $list = M('footimg')->where('admin_id='.$business_id)->order('create_time desc')->find();

       
        if (empty($list)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
        }

     }



     public function facutime(){
        $business_id=I('business_id');
        //$user_id=I('user_id');
        $list = M('facutime')->where('admin_id='.$business_id.' and status=1')->find();

       
        if (empty($list)) {
           $this->ajaxReturn(['status' => -1, 'msg' => '没有数据']);
        } else {
            $this->ajaxReturn(['status' => 1, 'msg' => '已获取数据', 'data' => $list]);
        }

     }


     public function wanttoEmail(){
        $url="https://jisu.shenmikj.com/canyin/index.php/Smi/index";
        $realname=I('realname');//姓名
        $phone=I('phone');//手机
        $nickname=I('nickname');//微信昵称
       
       
        //发送邮箱
        // 32241931@qq.com
        $toemail = '32241931@qq.com';
        //$toemail = '605072624@qq.com';
        $name = '有客户想要做小程序';
        $subject = '小程序咨询';
        $content= "有客户想要做小程序。小程序后台地址：{$url}，咨询人姓名：{$realname}，手机：{$phone}，微信昵称：{$nickname}";
         //print_r($content);die;
        $row=send_mail($toemail,$name,$subject,$content);
        if($row){
            $this->ajaxReturn(['status' => 1, 'msg' => '邮件发送成功']);
        }else{
            $this->ajaxReturn(['status' => -1, 'msg' => '邮件发送失败']);
        }
        
    }




    









    
	

}