<?php
/**
 * 微信小程序支付模块
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaoq <570035208@qq.com>
 */

namespace Wxsmall\Model;
use Think\Model;
class OrderInfoModel extends Model{
    /**
     * 获取订单页面
     * @param array $param 筛选参数
     * @return array 返回信息
     */
    public function index($param){
        $return = array('status'=>3,'msg'=>'获取信息失败');
        if(empty($param['uid'])){
            return $return;
        }
        // 是否开始发票
        $is_invoice = getconfig('OPEN_INVOICE');
        $return['is_invoice']=$is_invoice;
        // --------end----------
        $map=array();
        $map['id']=$param['uid'];
        $member_info=M("Member")->where($map)->field(true)->find();
        if($member_info['is_temp']==1){
            $return['status']=3;
            $return['msg']='您还没有领取会员卡，不能下单！';
            $return['data']='nom';
            return $return;
        }
        $return['member_info']=$member_info;
        $ids=$param['cart_ids'];
        $type=$param['type'];
        $shop_id=$param['HCshop'];
        $goods_id=$param['goods_id'];
        $addres_id=$param['delivery_id'];
        $map = array();
        if($type==6){
            $result = D('Common/OrderInfo')->order_list($ids,$param,$type,$addres_id);
        }else{
            $result = D('Common/OrderInfo')->order_list($ids,$goods_id,$type,$addres_id);
        }
        if($result['status']!=1){
            $return['status']=3;
            $return['msg']=$result['msg'];
            return $return;
        }else{
            if($result['data']['status']!=1){
                $return['status']=3;
                $return['msg']=$result['data']['msg'];
                return $return;
            }
        }
        if($result['service_time']['to_time']){
			$add_time = NOW_TIME + ($result['service_time']['to_time']*60);
		}else{
			$add_time = NOW_TIME;
        }
        //收货地址
        $address=array();
        foreach($result['address_list'] as $key=>$val){
            if($addres_id){
                if($val['id']==$addres_id){
                    $address=$val;
                }
            }else{
                if($val['id']==$result['addr_check_id']){
                    $address=$val;
                }
            }
        }
        $i = 0;
		$liji_time = NOW_TIME + ($result['service_time']['to_time']*60);
        $week_time_arr = $result['service_time']['week_time_arr'];
        $week_time_arr_format = array();
        foreach($week_time_arr as $k=>$v){
            $week_time_arr_format[$i]['k'] = $k;
            $week_time_arr_format[$i]['n'] = time_format($k,'Y-m-d').'周'.$v;
            $i++;
        }
        $i = 0;
        $time_arr = $result['service_time']['time_arr'];
        $time_arr_format = $time_arr_tmp = array();
      /*  $time_arr_format[$i]['s'] = 1;
        $time_arr_format[$i]['k'] = $liji_time;
        $time_arr_format[$i]['n'] = '立即配送，大约'.$result['service_time']['to_time'].'分钟';*/
        $i = 0;
        foreach($time_arr as $k=>$v){
            $time_arr_tmp[$i]['k'] = $k;
            $time_arr_tmp[$i]['n'] = $v;
            $i++;
        }
        $i = 0;
        foreach($time_arr as $k=>$v){
            if($add_time<=$k){
                $time_arr_format[$i]['k'] = $k;
                $time_arr_format[$i]['n'] = $v;
                $i++;
            }
        }

        $result['service_time_format'][] = $week_time_arr_format;
        $result['service_time_format'][] = $time_arr_format;
        $result['service_time_format'][] = $time_arr_tmp;
        if($result){
            $return['status']=1;
            foreach($result['data']['list'] as $key=>$val){
                foreach ($val['promot_list'] as $k => $va) {
                    foreach ($va['goods_list'] as $kk => $vk) {
                        $result['data']['list'][$key]['promot_list'][$k]['goods_list'][$kk]['img'] = thumb($vk['img'], 289, 289);
                    }
                    // ---3.8 zjn 赠品图片不显示----
                    if($va['gift_list']){
                        $result['data']['list'][$key]['promot_list'][$k]['gift_list']['img']=thumb($va['gift_list']['img'],289, 289);
                    }
                    // -----end-------
                }
            }
            $return['data']=$result;
            $coupons = get_online_bonus($result['data']['list'][$shop_id]['shop_info']['shop_total_price'],$param['uid'],$ids);
            if($coupons && $coupons['list']) {
                foreach ($coupons['list'] as $key => $val) {
                    if($val['type']==1){
                        $coupons['list'][$key]['start_time']=time_format($val['begin_timestamp']);
                        $coupons['list'][$key]['end_time']='至'.time_format($val['end_timestamp']);
                    }else{
                        $coupons['list'][$key]['start_time']='领取后'.$val['fixed_term'].'天后生效';
                        $list['info'][$key]['end_time']=',有效期'.$val['fixed_begin_term'].'天';
                    }
                    if($val['rank_id']=='CASH'){
                        $coupons['list'][$key]['money']=intval($val['reduce_cost']);
                    }elseif($val['rank_id']=='DISCOUNT'){
                        $money=(100-$val['discount'])/10;
                        $coupons['list'][$key]['money']=$val['discount'].'折';
                    }
                    if($val['is_show']==0){
                        $coupons['list'][$key]['shop_name']='全部门店可用';
                    }else{
                        $map=array();
                        $map=array('in',$val['is_show']);
                        $shop_name=M("Shops")->where($map)->getField('name');
                        $coupons['list'][$key]['shop_name']=implode(',',$shop_name);
                    }
                    $coupons['list'][$key]['least_cost']=intval($val['least_cost']);
                    $color=D("Wxsmall/Coupons")->get_cpupon_color();
                    $coupons['list'][$key]['color']=$color[$val['color']];
                }
            }
            $return['coupons']=$coupons;
            return $return;
        }else{
            $return['msg']=$result['msg'];
            return $return;
        }
        return $return;
    }
}