<?php

defined('InShopNC') or exit('Access Invalid!');
class toolsControl extends SystemControl
{
    public function __construct()
    {
        parent::__construct();
    }

    private function show_menu($menu_key) {
        $menu_array = array(
            'oms_order_push'=>array('menu_type'=>'link','menu_name'=>'oms订单推送','menu_url'=>'./index.php?act=tools&op=index'),
        );
        $menu_array[$menu_key]['menu_type'] = 'text';
        Tpl::output('menu',$menu_array);
    }

    public function indexOp(){
        $this->oms_order_pushOp();
    }

    public function oms_order_pushOp()
    {
        if(chksubmit())
        {
            $order_number = trim($_GET['order_number']);
            if(empty($order_number)) {
                $this->show_menu('oms_order_push');
                Tpl::showpage('oms_order_push');
            }
            else
            {
                $mod_order = Model('order');
                $orders = $mod_order->getOrderList(['order_sn' => $order_number]);
                if(empty($orders)) {
                    showMessage(L('nc_common_save_fail'), urlAdmin('tools', 'oms_order_push'));
                }
                else
                {
                    $order = $orders[0];

                    $pay_sn   = $order['pay_sn'];
                    $trade_no = $order['trade_no'];
                    if(empty($pay_sn) || empty($trade_no)) {
                        showMessage(L('nc_common_save_fail'), urlAdmin('tools', 'oms_order_push'));
                        return;
                    }
                    $logic_delivery = Logic('delivery');
                    $ret = $logic_delivery->putOrder($pay_sn, $trade_no);
                    if($ret == true) {
                        showMessage(L('nc_common_save_succ'), urlAdmin('tools', 'oms_order_push'));
                    } else {
                        showMessage(L('nc_common_save_fail'), urlAdmin('tools', 'oms_order_push'));
                    }
                }
            }
        }
        else
        {
            $this->show_menu('oms_order_push');
            Tpl::showpage('oms_order_push');
        }
    }
}