<?php

namespace app\home\controller;
use think\facade\View;

use think\facade\Lang;

/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class Memberpointorder extends BaseMember
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'home/lang/'.config('lang.default_lang').'/memberpoints.lang.php');
    }


    public function index()
    {
        //兑换信息列表
        $condition = array();
        $condition[] = array('point_buyerid','=',session('member_id'));

        $pointorder_model = model('pointorder');
        $order_list = $pointorder_model->getPointorderList($condition, '*', 10, 0, 'point_orderid desc');
        $order_idarr = array();
        $order_listnew = array();
        if (is_array($order_list) && count($order_list) > 0) {
            foreach ($order_list as $k => $v) {
                $order_listnew[$v['point_orderid']] = $v;
                $order_idarr[] = $v['point_orderid'];
            }
        }

        //查询兑换商品
        if (is_array($order_idarr) && count($order_idarr) > 0) {
            $prod_list = $pointorder_model->getPointordergoodsList(array(array('pointog_orderid','in',$order_idarr)));
            if (is_array($prod_list) && count($prod_list) > 0) {
                foreach ($prod_list as $v) {
                    if (isset($order_listnew[$v['pointog_orderid']])) {
                        $order_listnew[$v['pointog_orderid']]['prodlist'][] = $v;
                    }
                }
            }
        }

        //信息输出
        View::assign('order_list', $order_listnew);
        View::assign('show_page', $pointorder_model->page_info->render());
        /* 设置买家当前菜单 */
        $this->setMemberCurMenu('member_pointorder');
        /* 设置买家当前栏目 */
        $this->setMemberCurItem('orderlist');
        return View::fetch($this->template_dir.'index');
    }
    

    /**
     *    取消兑换
     */
    public function cancel_order()
    {
        $pointorder_model = model('pointorder');
        //取消订单
        $data = $pointorder_model->cancelPointorder(input('param.order_id'), session('member_id'));
        if ($data['state']) {
            ds_json_encode(10000,lang('member_pointorder_cancel_success'));
        } else {
            ds_json_encode(10001,$data['msg']);
        }
    }

    /**
     * 确认收货
     */
    public function receiving_order()
    {
        $data = model('pointorder')->receivingPointorder(input('param.order_id'));
        if ($data['state']) {
            ds_json_encode(10000,lang('member_pointorder_confirmreceiving_success'));
        } else {
            ds_json_encode(10001,$data['msg']);
        }
    }

    /**
     * 兑换信息详细
     */
    public function order_info()
    {
        $order_id = intval(input('order_id'));
        if ($order_id <= 0) {
            $this->error(lang('member_pointorder_parameter_error'));
        }
        $pointorder_model = model('pointorder');
        //查询兑换订单信息
        $where = array();
        $where[] = array('point_orderid','=',$order_id);
        $where[] = array('point_buyerid','=',session('member_id'));
        $order_info = $pointorder_model->getPointorderInfo($where);
        if (!$order_info) {
            $this->error(lang('member_pointorder_record_error'));
        }
        //获取订单状态
        $pointorderstate_arr = $pointorder_model->getPointorderStateBySign();
        View::assign('pointorderstate_arr', $pointorderstate_arr);

        //查询兑换订单收货人地址
        $orderaddress_info = $pointorder_model->getPointorderAddressInfo(array('pointoa_orderid' => $order_id));
        View::assign('orderaddress_info', $orderaddress_info);

        //兑换商品信息
        $prod_list = $pointorder_model->getPointordergoodsList(array('pointog_orderid' => $order_id));
        View::assign('prod_list', $prod_list);

        //物流公司信息
        if ($order_info['point_shipping_ecode'] != '') {
            $data = model('express')->getExpressInfoByECode($order_info['point_shipping_ecode']);
            if ($data['state']) {
                $express_info = $data['data']['express_info'];
            }
            View::assign('express_info', $express_info);
        }

        View::assign('order_info', $order_info);
        View::assign('left_show', 'order_view');
        
        
        /* 设置买家当前菜单 */
        $this->setMemberCurMenu('member_pointorder');
        /* 设置买家当前栏目 */
        $this->setMemberCurItem('order_info');
        return View::fetch($this->template_dir.'member_pointorder_info');
    }

    /**
     * 用户中心右边，小导航
     *
     * @param string $menu_type 导航类型
     * @param string $menu_key 当前导航的menu_key
     * @return
     */
    public function getMemberItemList() {
        $menu_array = array(
            array(
                'name' => 'orderlist',
                'text' => lang('member_pointorder_list_title'),
                'url' => (string)url('Memberpointorder/index')
            )
        );
        if (request()->action() == 'order_info') {
            $menu_array = array(
                array(
                    'name' => 'orderinfo',
                    'text' => lang('ds_member_path_pointorder_info'),
                    'url'=>'javascript:void(0)'
                )
            );
        }
        return $menu_array;
    }

}