<?php
/**
 * @package merchant
 * @Author: Qinii
 * @Date: 2020/5/27
 */

namespace app\controller\api\movie;

use app\common\repositories\movie\CinemaRepository;
use app\common\repositories\movie\CinemaSchedRepository;
use app\common\repositories\movie\FilmRepository;
use app\common\repositories\movie\MovieRepository;
use app\common\repositories\system\groupData\GroupDataRepository;
use app\controller\merchant\user\User;
use crmeb\basic\BaseController;
use crmeb\services\ApiResponseService;
use think\App;

class Movie extends BaseController
{
    /**
     * @var repository
     */
    protected $repository;
    protected $userInfo;
    private $source;
    private $merId;

    /**
     * StoreProduct constructor.
     * @param App $app
     * @param repository $repository
     */
    public function __construct(App $app, MovieRepository $repository)
    {
        parent::__construct($app);
        $this->repository = $repository;
    }

    /**
     * 获取城市列表
     *
     * @return mixed
     */
    public function getMovieCity()
    {
        return app('json')->success($this->repository->getLocalMovieCity());
    }

    /**
     * 获取城市分区列表
     *
     * @return mixed
     */
    public function getArea()
    {
        $city_sign = $this->request->param('city_sign', '');
        if (empty($city_sign)) return app('json')->fail('请选择城市');

        return app('json')->success($this->repository->getMovieArea($city_sign));
    }

    /**
     * 获取影院列表
     *
     * @return mixed
     */
    public function getCinema()
    {
        [$page, $limit] = $this->getPage();
        $cinema_name = $this->request->param('cinema_name', '');// 影院名称
        $longitude = $this->request->param('longitude', '');// 经度
        $latitude = $this->request->param('latitude', '');// 纬度
        if (empty($longitude)) return app('json')->fail('请授权应用开启定位');
        if (empty($latitude)) return app('json')->fail('请授权应用开启定位');
        $where = !empty($cinema_name) ? ['cinema_name' => $cinema_name] : [];

        /** @var CinemaRepository $cinemaRepository */
        $cinemaRepository = app()->make(CinemaRepository::class);
        return app('json')->success($cinemaRepository->getLocalCinema($page, $limit, $longitude, $latitude, $where));
    }

    /**
     * 获取影片列表
     *
     * @return mixed
     */
    public function getFilm()
    {
        [$page, $limit] = $this->getPage();
        $type = $this->request->param('type', 0);//热映：0：正在热映，1：即将上映
        $film_name = $this->request->param('film_name', '');

        /** @var FilmRepository $filmRepository */
        $filmRepository = app()->make(FilmRepository::class);
        return app('json')->success($filmRepository->getFilm($page, $limit, $type, $film_name));
    }

    /**
     * 获取影片场次日期列表
     *
     * @return mixed
     */
    public function getFilmSchedDate()
    {
        $film_sign = $this->request->param('film_sign', '');
        if (empty($film_sign)) return app('json')->fail('请选择要观看的影片');

        /** @var CinemaSchedRepository $cinemaSchedRepository */
        $cinemaSchedRepository = app()->make(CinemaSchedRepository::class);
        return app('json')->success($cinemaSchedRepository->getFilmSchedDate($film_sign));
    }

    /**
     * 根据影片-获取影院列表
     *
     * @return mixed
     */
    public function getFilmSched()
    {
        [$page, $limit] = $this->getPage();
        $film_sign = $this->request->param('film_sign', '');
        $longitude = $this->request->param('longitude', '');// 经度
        $latitude = $this->request->param('latitude', '');// 纬度
        $city_sign = $this->request->param('city_sign', '');

        if (empty($film_sign)) return app('json')->fail('请选择要观看的影片');
        if (empty($longitude) || empty($latitude)) return app('json')->fail('请授权应用开启定位');
        if (empty($city_sign)) return app('json')->fail('请选择城市');

        /** @var CinemaSchedRepository $cinemaSchedRepository */
        $cinemaSchedRepository = app()->make(CinemaSchedRepository::class);
        return app('json')->success($cinemaSchedRepository->getFilmSched($page, $limit, $film_sign, $longitude, $latitude, $city_sign));
    }

    /**
     * 根据影片、影院、排片日期-获取场次列表
     *
     * @return mixed
     */
    public function getCinemaFilmSched()
    {
        try {
//            [$page, $limit] = $this->getPage();
            $cinema_sign = $this->request->param('cinema_sign', '');
            $film_sign = $this->request->param('film_sign', '');
//        $longitude = $this->request->param('longitude', '');// 经度
//        $latitude = $this->request->param('latitude', '');// 纬度
//        $show_date = $this->request->param('show_date', '');

            if (empty($cinema_sign)) return app('json')->fail('请选择观影影院');
            if (empty($film_sign)) return app('json')->fail('请选择观影影片');
//        if (empty($longitude) || empty($latitude)) return app('json')->fail('请授权应用开启定位');
//        if (empty($show_date)) return app('json')->fail('请选择观影日期');

            /** @var CinemaSchedRepository $cinemaSchedRepository */
            $cinemaSchedRepository = app()->make(CinemaSchedRepository::class);
            return app('json')->success($cinemaSchedRepository->getCinemaFilmSchedNew($film_sign, $cinema_sign));
        } catch (\Exception $exception) {
            return app('json')->fail($exception->getMessage());
        }
    }

    /**
     * 获取场次座位
     *
     * @return mixed
     */
    public function getSchedSeat()
    {
        $relation_id = $this->request->param('relation_id', '');
        if (empty($relation_id)) return app('json')->fail('请选择观影场次');
        $sched_sign = $this->request->param('sched_sign', '');
        if (empty($sched_sign)) return app('json')->fail('请选择观影场次');

        return app('json')->success($this->repository->getSchedSeat($relation_id, $sched_sign));
    }

    /**
     * 锁座订单
     *
     * @return mixed
     */
    public function submitOrder()
    {
        $params['relation_id'] = $this->request->param('relation_id', '');// 场次ID
        $params['sched_sign'] = $this->request->param('sched_sign', '');// 场次编码
        $params['channel_sn'] = $this->request->param('channel_sn', '');// 实际价格
        $params['sched_seat'] = $this->request->param('sched_seat', '');// 座位编码
        $params['hall_sign'] = $this->request->param('hall_sign', '');// 影厅编码
        $params['hall_name'] = $this->request->param('hall_name', '');// 影厅名称
        $params['show_date'] = $this->request->param('show_date', '');// 观影日期
        $params['show_time'] = $this->request->param('show_time', '');// 观影时间
        $params['film_sign'] = $this->request->param('film_sign', '');// 影片编码
        $params['cinema_sign'] = $this->request->param('cinema_sign', '');// 影院编码
        $params['phone'] = $this->request->param('phone', '');// 联系电话

        if (empty($params['cinema_sign']) || empty($params['hall_sign']) || empty($params['hall_name'])) return app('json')->fail('请选择观影影院');
        if (empty($params['film_sign'])) return app('json')->fail('请选择观影影片');
        if (empty($params['show_date']) || empty($params['show_time'])) return app('json')->fail('请选择观影时间');
        if (empty($params['relation_id']) || empty($params['sched_sign']) || empty($params['channel_sn'])) return app('json')->fail('请选择观影场次');
        if (empty($params['sched_seat'])) return app('json')->fail('请选择观影座位');

        try {
            $order = $this->repository->submitOrder($this->request->userInfo(), $params);
            if (!empty($order)) {
                return app('json')->success(['order_sn' => $order['order_sn'], 'oid' => $order['order_id'], 'type' => 'wx']);
            } else {
                return app('json')->fail('锁座下单失败');
            }
        } catch (\Exception $exception) {
            return app('json')->fail($exception->getMessage());
        }
    }

    /**
     * 确认订单
     *
     * @return mixed
     */
    public function confirmOrder()
    {
        $order_id = $this->request->param('oid', '');
        $code = $this->request->param('code', '');
        $pay_type = $this->request->param('type', '');
        if (empty($order_id)) return app('json')->fail('订单不存在');
        if (empty($pay_type) || !in_array(strtolower($pay_type), ['wx', 'ali'])) return app('json')->fail('请选择支付方式');

        try {
            return app('json')->success($this->repository->confirmOrder($this->request->userInfo(), $order_id, $pay_type, $code));
        } catch (\Exception $exception) {
            return app('json')->fail($exception->getMessage());
        }
    }

    /**
     * 订单列表
     *
     * @return mixed
     */
    public function getOrderList()
    {
        [$page, $limit] = $this->getPage();
        $type = $this->request->param('type', -1);//订单状态：-1：全部，0 待支付 1 已支付 2已超时 3已取消 4已成功

        return app('json')->success($this->repository->getOrderList($this->request->userInfo(), $page, $limit, $type));
    }

    /**
     * 查询订单信息
     *
     * @return mixed
     */
    public function getOrderInfo()
    {
        $order_sn = $this->request->param('order_sn', '');
        if (empty($order_sn)) return app('json')->fail('订单不存在');

        return app('json')->success($this->repository->getOrderInfo($this->request->userInfo(), $order_sn));
    }
}
