<?php

require_once(dirname(dirname(__FILE__)) . '/app.php');
need_login();

if($_POST && !isset($_POST['search'])){
    $express['real_name'] = trim($_POST['real_name']);
    $express['mobile'] = trim($_POST['mobile']);
    $express['zipcode'] = trim($_POST['zipcode']);
    $express['province'] = trim($_POST['province']);
    $express['address'] = trim($_POST['address']);
    $express['remark'] = trim($_POST['remark']);
    $express['phone'] = trim($_POST['phone']);
    $express['express'] = $_POST['express'];

    $err = "";
    
    if($express['province']=='_NULL_' || empty($express['province'])) $err="信息填写有误！";
    if(!empty($err)){
        Session::Get('success',true);
        Session::Set('error',$err);
        redirect(WEB_ROOT."/cart/order.php");
    }
    
    
    $provider = OCart::getProvider();
    if(count($express['express'])<>count($provider)){
        Session::Get('success',true);
        Session::Set('error','信息不完整请重新填写！');
        Utility::Redirect(WEB_ROOT."/cart/order.php");
    }else{
        OOrder::createFromCart($express);
        OCart::clear();
        Session::Set('success',Session::Get('success',true).'订单成功创建！');
        Utility::Redirect(WEB_ROOT."/order/index.php");
    }
}


if($_GET){
    if(isset($_GET['state'])){
        $condition['state'] = $_GET['state'];
        $condition1 = "and order_info.state='".$_GET['state']."' ";
    }
}


$condition['user_id'] = $login_user_id;
if(isset($_POST['order_number']) && !empty($_POST['order_number'])){
    $condition['order_number'] = $_POST['order_number'];
    $condition1 .= "and order_info.order_number='".$_POST['order_number']."' ";
}
if(isset($_POST['real_name']) && !empty($_POST['real_name'])){
    $condition['realname'] = $_POST['real_name'];
    $condition1 .= "and order_info.realname='".$_POST['real_name']."' ";
}

$count = Table::Count('order_info', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, 20);

$sql = "select provider.name,provider.brand,provider.channel,order_info.*,fare.name as express_name
        from provider,order_info,fare
        where order_info.user_id=".$login_user_id." and provider.id=order_info.provider_id and order_info.express_id=fare.id ".$condition1."
        ORDER BY order_info.create_time DESC, order_info.pay_time DESC
        LIMIT ".$offset.",".$pagesize;
$order_info = DB::GetQueryResult($sql,false);

$state = $INI['sys']['order'];
if(count($order_info)==0){
    Session::Set('error','没有任何订单信息！');
}

include template('order_index');
?>
