<?php

namespace app\api\controller;

use think\facade\Lang;
use think\facade\Db;

/**
 * ============================================================================
 * 
 * ============================================================================
 * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司，并保留所有权利。
 * 网站地址: https://www.valimart.net/
 * ----------------------------------------------------------------------------
 * 
 * ============================================================================
 * 虚拟订单控制器
 */
class Membervrorder extends MobileMember {

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/memberorder.lang.php');
    }

    /**
     * @api {POST} api/Membervrorder/order_list 订单列表
     * @apiVersion 1.0.0
     * @apiGroup Membervrorder
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} page 当前页数
     * @apiParam {Int} state_type 订单状态 state_new待付款 state_pay待使用
     * @apiParam {String} order_key 订单编号
     * @apiParam {Int} per_page 每页数量
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.order_list  订单列表 （返回字段参考vrorder表）
     * @apiSuccess {Object[]} result.order_list.code_list 未兑换的兑换码列表 （返回字段参考vrordercode表）
     * @apiSuccess {Int} result.order_list.if_cancel  是否可取消 true是false否
     * @apiSuccess {Int} result.order_list.if_pay  是否可支付 true是false否
     * @apiSuccess {Int} result.order_list.if_refund  是否可退款 true是false否
     * @apiSuccess {Int} result.order_list.if_evaluation  是否可评价 true是false否
     * @apiSuccess {String} result.order_list.goods_image_url  商品图片完整地址
     * @apiSuccess {Int} result.order_list.ownshop  是否自营店 true是false否
     * @apiSuccess {Int} result.order_list.if_refund_cancel  是否可全部退款 true是false否
     * @apiSuccess {Int} result.page_total  总页数
     * @apiSuccess {Boolean} result.hasmore  是否有更多 true是false否
     */
    public function order_list() {

        $ownShopIds = model('store')->getOwnShopIds();

        $vrorder_model = model('vrorder');

        $condition = array();
        $condition[] = array('buyer_id', '=', $this->member_info['member_id']);
        if (preg_match('/^\d{10,20}$/', input('post.order_key'))) {
            $condition[] = array('order_sn', '=', input('post.order_key'));
        } elseif (input('post.order_key') != '') {
            $condition[] = array('goods_name', 'like', '%' . input('post.order_key') . '%');
        }
        if (input('post.state_type') != '') {
            $condition[] = array('order_state', '=', str_replace(array('state_new', 'state_pay'), array(ORDER_STATE_NEW, ORDER_STATE_PAY), input('post.state_type')));
        }
        $order_list = $vrorder_model->getVrorderList($condition, $this->pagesize, '*', 'order_id desc');
        //没有使用的兑换码列表
        $order_list = $vrorder_model->getCodeRefundList($order_list);
        foreach ($order_list as $key => $order) {
            //显示取消订单
            $order_list[$key]['if_cancel'] = $vrorder_model->getVrorderOperateState('buyer_cancel', $order);

            //显示支付
            $order_list[$key]['if_pay'] = $vrorder_model->getVrorderOperateState('payment', $order);

            //显示退款
            $order_list[$key]['if_refund'] = $vrorder_model->getVrorderOperateState('refund', $order);

            //显示评价
            $order_list[$key]['if_evaluation'] = $vrorder_model->getVrorderOperateState('evaluation', $order);

            $order_list[$key]['goods_image_url'] = goods_cthumb($order['goods_image'], 240, $order['store_id']);

            $order_list[$key]['ownshop'] = in_array($order['store_id'], $ownShopIds);
        }
        $result = array_merge(array('order_list' => $order_list), mobile_page($vrorder_model->page_info));
        ds_json_encode(10000, '', $result);
    }

    /**
     * @api {POST} api/Membervrorder/order_info 订单详情
     * @apiVersion 1.0.0
     * @apiGroup Membervrorder
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} order_id 订单ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.order_info  订单列表 （返回字段参考vrorder表）
     * @apiSuccess {Object[]} result.order_info.code_list 未兑换的兑换码列表 （返回字段参考vrordercode表）
     * @apiSuccess {Int} result.order_info.if_cancel  是否可取消 true是false否
     * @apiSuccess {Int} result.order_info.if_resend  是否可发送兑换码 true是false否
     * @apiSuccess {Int} result.order_info.if_refund  是否可退款 true是false否
     * @apiSuccess {Int} result.order_info.if_evaluation  是否可评价 true是false否
     * @apiSuccess {String} result.order_info.goods_image_url  商品图片完整地址
     * @apiSuccess {Int} result.order_info.ownshop  是否自营店 true是false否
     * @apiSuccess {Int} result.order_info.if_refund_cancel  是否可全部退款 true是false否
     */
    public function order_info() {
        $order_id = intval(input('param.order_id'));
        if ($order_id <= 0) {
            ds_json_encode(10001, lang('param_error'));
        }
        $vrorder_model = model('vrorder');
        $condition = array();
        $condition[] = array('order_id','=',$order_id);
        $condition[] = array('buyer_id','=',$this->member_info['member_id']);
        $order_info = $vrorder_model->getVrorderInfo($condition);
        if (empty($order_info) || $order_info['delete_state'] == ORDER_DEL_STATE_DROP) {
            ds_json_encode(10001, lang('member_order_none_exist'));
        }
        $order_list = array();
        $order_list[$order_id] = $order_info;
        $order_list = $vrorder_model->getCodeRefundList($order_list); //没有使用的兑换码列表
        $order_info = $order_list[$order_id];
        //显示取消订单
        $order_info['if_cancel'] = $vrorder_model->getVrorderOperateState('buyer_cancel', $order_info);

        //显示评价
        $order_info['if_evaluation'] = $vrorder_model->getVrorderOperateState('evaluation', $order_info);

        //显示退款
        $order_info['if_refund'] = $vrorder_model->getVrorderOperateState('refund', $order_info);

        $order_info['goods_image_url'] = goods_cthumb($order_info['goods_image'], 240, $order_info['store_id']);

        $ownShopIds = model('store')->getOwnShopIds();
        $order_info['ownshop'] = in_array($order_info['store_id'], $ownShopIds);

        $order_info['vr_indate'] = $order_info['vr_indate'] ? date('Y-m-d', $order_info['vr_indate']) : '';
        $order_info['add_time'] = date('Y-m-d H:i:s', $order_info['add_time']);
        $order_info['payment_time'] = $order_info['payment_time'] ? date('Y-m-d H:i:s', $order_info['payment_time']) : '';
        $order_info['finnshed_time'] = $order_info['finnshed_time'] ? date('Y-m-d H:i:s', $order_info['finnshed_time']) : '';

        $order_info['if_resend'] = $order_info['order_state'] == ORDER_STATE_PAY ? true : false;
        //取兑换码列表
        $vr_code_list = $vrorder_model->getShowVrordercodeList(array('order_id' => $order_info['order_id']));
        $order_info['code_list'] = $vr_code_list ? $vr_code_list : array();
        if($order_info['order_state']==ORDER_STATE_SUCCESS){
            if($order_info['virtual_type']==3){
                $order_info['vc_file_url']=goods_resource($order_info['virtual_content']);
            }else if($order_info['virtual_type']==1){
                $order_info['virtual_content']=explode('\r\n',$order_info['virtual_content']);
            }
        }else{
            $order_info['virtual_content']='';
        }
        ds_json_encode(10000, '', array('order_info' => $order_info));
    }

    /**
     * @api {POST} api/Membervrorder/order_cancel 取消订单
     * @apiVersion 1.0.0
     * @apiGroup Membervrorder
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} order_id 订单ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function order_cancel() {
        $vrorder_model = model('vrorder');
        $condition = array();
        $condition[] = array('order_id','=',intval(input('post.order_id')));
        $condition[] = array('buyer_id','=',$this->member_info['member_id']);
        $order_info = $vrorder_model->getVrorderInfo($condition);

        $if_allow = $vrorder_model->getVrorderOperateState('buyer_cancel', $order_info);
        if (!$if_allow) {
            ds_json_encode(10001, lang('have_right_operate'));
        }

        $logic_vrorder = model('vrorder', 'logic');
        $result = $logic_vrorder->changeOrderStateCancel($order_info, 'buyer', lang('other_reason'));

        if (!$result['code']) {
            ds_json_encode(10001, $result['msg']);
        } else {
            ds_json_encode(10000, '');
        }
    }

    /**
     * 发送兑换码到手机
     */
    public function resend() {
        if (!preg_match('/^[\d]{11}$/', input('post.buyer_phone'))) {
            ds_json_encode(10001, lang('please_fill_phone_number_correctly'));
        }
        $order_id = intval(input('post.order_id'));
        if ($order_id <= 0) {
            ds_json_encode(10001, lang('param_error'));
        }

        $vrorder_model = model('vrorder');

        $condition = array();
        $condition[] = array('order_id','=',$order_id);
        $condition[] = array('buyer_id','=',$this->member_info['member_id']);
        $order_info = $vrorder_model->getVrorderInfo($condition);
        if (empty($order_info) && $order_info['order_state'] != ORDER_STATE_PAY) {
            ds_json_encode(10001, lang('error_order_information'));
        }
        if ($order_info['vr_send_times'] >= 5) {
            ds_json_encode(10001, lang('you_send_too_many_times'));
        }

        //发送兑换码到手机
        $param = array(
            'order_id' => $order_id, 'buyer_id' => $this->member_info['member_id'],
            'buyer_phone' => input('post.buyer_phone'), 'goods_name' => $order_info['goods_name']
        );
        $vrorder_model->sendVrCode($param);
        $vrorder_model->editVrorder(array('vr_send_times' => Db::raw('vr_send_times+1')), array('order_id' => $order_id));
        ds_json_encode(10000, '');
    }

}
