<?php
/**
 *
 * ============================================================================
 * * 版权所有 蜘蛛出行 * *
 * 网站地址: http://www.zhizhuchuxing.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author By: 娄梦宁
 * PhpStorm LoginController.php
 * Create By 2017/2/22 15:08 $
 */


namespace fx\controllers\prod;

use common\Controller\FxWebController;
use common\models\OrderMain;
use common\models\ProdMain;
use common\util\OrderUtil;
use common\util\prod\LineProdService;
use common\util\Util;
use fx\util\FxUtil;
use Yii;

class ProdController extends FxWebController
{
    /**
     * Des:登录处理
     * Name: actionDoLogin
     * @return string
     * @author 倪宗锋
     */
    public function actionHome()
    {
        return $this->redirect('/web/fx/reserve_prod.html');
    }

    /**
     * Function Description:首页产品列表
     * Function Name: actionGetProdList
     *
     * @return string
     *
     * @author 娄梦宁
     */
    public function actionGetProdList()
    {
        $where = trim(Yii::$app->request->post('where', ''));
        $point = $this->_post('point', '');
        $result = OrderUtil::getAllProd($where, $point);
        return json_encode($result);
    }

    /**
     * Function Description:产品详情数据获取
     * Function Name: actionGetProdDetail
     *
     * @return string
     *
     * @author 娄梦宁
     */
    public function actionGetProdDetail()
    {
        $pro_cate_id = Yii::$app->request->post('pro_cate_id', '');
        $result = OrderUtil::getDetail($pro_cate_id);
        return json_encode($result);
    }


    /**
     * Des:获取主线路库存日历
     * Name: actionGetLineSaleDate
     * @return string
     * @author 倪宗锋
     */
    public function actionGetBusSaleDate()
    {
        $date = $this->_post('date', '');//时间
        $pro_cate_id = Yii::$app->request->post('pro_cate_id', '');
        $if_back = Yii::$app->request->post('if_back', 0);
        $result = OrderUtil::getBusSaleDate($pro_cate_id, $if_back,$date);
        return json_encode($result);
    }

    /**
     * Function Description:切换日期查询实时余票
     * Function Name: actionGetBusStock
     *
     * @return string
     *
     * @author 娄梦宁
     */
    public
    function actionGetBusStock()
    {
        $date = Yii::$app->request->post('date', '');
        $pro_cate_id = Yii::$app->request->post('pro_cate_id', '');
        $if_back = Yii::$app->request->post('if_back', 0);
        $result = OrderUtil::getBusStock($date, $pro_cate_id, $if_back);
        return json_encode($result);
    }

    /**
     * Des:获取主线路库存日历
     * Name: actionGetLineSaleDate
     * @return string
     * @author 倪宗锋
     */
    public function actionGetLineSaleDate()
    {
        $date = $this->_post('date', '');//时间
        $pro_cate_id = Yii::$app->request->post('pro_cate_id', '');
        $if_back = Yii::$app->request->post('if_back', 0);
        $result = LineProdService::getLineSaleDate($pro_cate_id, $if_back,$date);
        return json_encode($result);
    }

    /**
     * Des:获取主线路产品品类按日期查询实时余票
     * Name: actionGetLineBusStock
     * @return string
     * @author 倪宗锋
     */
    public
    function actionGetLineBusStock()
    {
        $date = Yii::$app->request->post('date', '');
        $pro_cate_id = Yii::$app->request->post('pro_cate_id', '');
        $if_back = Yii::$app->request->post('if_back', 0);
        $result = LineProdService::getLineBusStock($date, $pro_cate_id, $if_back);
        return json_encode($result);
    }

    /**
     * Des:获取查询产品的票种
     * Name: actionGetProdArr
     * @return string
     * @author 倪宗锋
     */
    public
    function actionGetProdArr()
    {
        $pro_cate_id = Yii::$app->request->post('pro_cate_id', '');
        $prodMain = new ProdMain();
        $result = $prodMain->getProdArr($pro_cate_id);
        return Util::returnJsSu('', ['prod_arr' => $result]);
    }


    /**
     * Des:下单
     * Name: actionMakeOrder
     * @return string
     * @author 倪宗锋
     */
    public
    function actionMakeOrder()
    {
        /**获取订单信息******/
        $param_arr = [
            'days' => $this->_post('days', 1),//历经几天
            'pro_cate_id' => Yii::$app->request->post('pro_cate_id', ''),//去的产品品类ID
            'start_date' => Yii::$app->request->post('start_date', ''),//出发日期
            'start_time' => Yii::$app->request->post('start_time', '00:00:00'),//去程出发时间
            'run_id' => Yii::$app->request->post('run_id', ''),//去除班次ID
            'prod_arr' => $this->_post('prod_arr', ''),//去的产品列表
            'contacts_name' => Yii::$app->request->post('contacts_name', ''),//联系人名称
            'contacts_phone' => Yii::$app->request->post('contacts_phone', ''),//联系人手机
            'contacts_ID' => Yii::$app->request->post('contacts_ID', ''),//联系人身份证
            'pay_type' => Yii::$app->request->post('pay_type', ''),//支付方式
            'fx_uid' => FxUtil::$uid,
            'pay_method' => $this->_post('pay_method', 1),//1、扫一扫 2、直接支付
            'if_back' => Yii::$app->request->post('if_back', 0),//是否有返程
            'is_line' => $this->_post('is_line', false),
            /**获取返回订单信息******/
            'back_pro_cate_id' => Yii::$app->request->post('back_pro_cate_id', ''),//返程品类ID
            'back_start_date' => Yii::$app->request->post('back_start_date', ''),//返程日期
            'back_start_time' => Yii::$app->request->post('back_start_time', '00:00:00'),//返程出发时间
            'back_run_id' => Yii::$app->request->post('back_run_id', ''),//返程班次ID
            'back_prod_arr' => $this->_post('back_prod_arr', ''),//去的产品列表
            'is_buy_insurance'=>Yii::$app->request->post('is_buy_insurance', '0'),//是否购买保险，0：不购买，1：购买
            'insurance_list'=>Yii::$app->request->post('insurance_list','[]'),//保险受保人数组
            'passanger_arr'=>Yii::$app->request->post('passanger_arr','[]'),//出行人数组
            'system' => 1
        ];

        $result = OrderUtil::makeOrder($param_arr);
        return json_encode($result);
    }

    /**
     * Function Description:查询订单状态
     * Function Name: actionCheckOrder
     *
     * @return string
     *
     * @author 娄梦宁
     */
    public
    function actionCheckOrder()
    {
        $order_id = Yii::$app->request->post('order_id', '');
        $result = OrderUtil::getOrderStatus($order_id);
        return json_encode($result);
    }

    /**
     * Des: 取消订单
     * Name: actionCancelOrder
     * @return string
     * @author 倪宗锋
     */
    public
    function actionCancelOrder()
    {
        $order_id = $this->_post('order_id', '');
        if (!Util::checkPattern('intVal', $order_id)) {
            return Util::returnJsEr('参数错误！');
        }
        $oderMain = new OrderMain();
        $orderInfo = $oderMain->getOrderInfo($order_id);
        if ($orderInfo['fx_uid'] != FxUtil::$uid) {
            return Util::returnJsEr('非法请求');
        }
        $return = OrderUtil::applyCancelOrder($order_id);
        if ($return['flag']) {
            return Util::returnJsSu();
        }
        return Util::returnJsEr($return['msg']);
    }

}