<?php
namespace app\api\controller;

use think\facade\Log;
use app\common\logic\PaymentLogic;
use app\api\logic\GroupActivityLogic;
use app\api\validate\GroupOrderValidate;
use app\api\lists\group_activity\UserOrderLists;
use app\api\lists\property\PropertyGroupOrderLists;
use app\api\lists\property\PropertyServiceWorkLists;
use app\api\lists\property\PropertyGroupActivityLists;
use app\adminapi\logic\group_activity\GroupActivityCategoryLogic;


/**
 * 拼团活动控制器
 * Class UserController
 * @package app\api\controller
 */
class GroupActivityController extends BaseApiController
{
    public array $notNeedLogin = [];


    /**
     * 活动分类列表
     */
    public function categoryList()
    {
        $id = (int)$this->request->param('id');
        $block_key = (int)$this->request->param('block_key');
        $result = GroupActivityLogic::categoryList($id,$block_key);
        return $this->data($result);
    }

    /**
     * 活动详情
     */
    public function detail()
    {
        $id = (int)$this->request->param('id');
        $result = GroupActivityLogic::detail($id,$this->userId);
        return $this->data($result);
    }

    /**
     * 拼团订单详情
     */
    public function orderDetail(){
        $order_id = (int)$this->request->param('order_id');
        $result = GroupActivityLogic::orderDetail($order_id,$this->userId,$this->request->domain());
        return $this->data($result);
    }

    /**
     * 用户订单详情
     */
    public function userOrderDetail(){
        $order_id = (int)$this->request->param('order_id');
        $result = GroupActivityLogic::userOrderDetail($order_id,$this->userId);
        return $this->data($result);
    }

    /**
     * 用户的订单列表
     */
    public function orderList(){
        return $this->dataLists(new UserOrderLists());
    }

    /**
     * 提交订单
     * @return \think\response\Json
     */
    public function submitOrder()
    {
        $params = (new GroupOrderValidate())->post()->goCheck('add', [
            'user_id' => $this->userId,
            'terminal' => $this->userInfo['terminal'],
            'user_info' => $this->userInfo
        ]);
        $result = GroupActivityLogic::submitOrder($params);
        if (false === $result) {
            return $this->fail(GroupActivityLogic::getError());
        }
        return $this->data($result);
    }

    /**
     * @notes 预支付
     * @return \think\response\Json
     */
    public function prepay()
    {
        $params = (new GroupOrderValidate())->post()->goCheck('pay');
        //订单信息
        $order = PaymentLogic::getPayGroupOrderInfo($params);
        if (false === $order) {
            return $this->fail(PaymentLogic::getError());
        }
        //支付流程
        $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
        $result = PaymentLogic::pay($params['pay_way'], 'group', $order, 1, $redirectUrl);
        if (false === $result) {
            return $this->fail(PaymentLogic::getError());
        }
        $result['sn'] = $order['sn'];
        Log::write('group_prepay：'.json_encode($result, JSON_UNESCAPED_UNICODE));
        return $this->success('', $result);
    }

    /**
     * @notes 获取支付状态
     * @return \think\response\Json
     */
    public function payStatus()
    {
        $params = (new GroupOrderValidate())->post()->goCheck('status',[
            'from' => 'group',
            'user_id' => $this->userId,
        ]);
        $result = PaymentLogic::getPayStatus($params);
        if ($result === false) {
            return $this->fail(PaymentLogic::getError());
        }
        return $this->data($result);
    }

    /**
     * 取消订单
     * @return \think\response\Json
     */
    public function cancelOrder()
    {
        $params = (new GroupOrderValidate())->post()->goCheck('cancel', [
            'user_id' => $this->userId
        ]);
        $result = GroupActivityLogic::cancelOrder($params);
        if (false === $result) {
            return $this->fail(GroupActivityLogic::getError());
        }
        return $this->success('取消成功', [], 1, 1);
    }

    /**
     * 申请退款
     * @return \think\response\Json
     */
    public function refundOrder()
    {
        $params = (new GroupOrderValidate())->post()->goCheck('refund', [
            'user_id' => $this->userId
        ]);
        $result = GroupActivityLogic::refundOrder($params);
        list($flag, $msg) = $result;
        if(false === $flag) {
            return $this->fail($msg);
        }

        if (false === $result) {
            return $this->fail(GroupActivityLogic::getError());
        }
        return $this->success('退款成功', [], 1, 1);
    }

    public function deleteOrder()
    {
        $params = (new GroupOrderValidate())->post()->goCheck('refund',[
            'user_id' => $this->userId
        ]);
        $result = GroupActivityLogic::deleteOrder($params);
        if (false === $result) {
            return $this->fail(GroupActivityLogic::getError());
        }
        return $this->success('删除成功', [], 1, 1);
    }

    /**
     * 获取拼团订单分享二维码
     */
    public function getQRCode()
    {
        $params = (new GroupOrderValidate())->post()->goCheck('qrcode');
        return $this->success('',['qrcode'=>GroupActivityLogic::getQRCode($params, $this->request->domain())], 1, 1);
    }

     /**
     * 获取拼团活动首页二维码
     */
    public function getIndexQRCode()
    {
        $id = (int)$this->request->param('id');
        return $this->success('',['qrcode'=>GroupActivityCategoryLogic::getQRCode(['id' => $id], $this->request->domain())], 1, 1);
    }

    /**
     * 代理人拼团订单列表
     */
    public function propertyOrderLists()
    {
        return $this->dataLists(new PropertyGroupOrderLists());
    }

    /**
     * 代理人完结的工单列表
     */
    public function propertyServiceWorkLists()
    {
        return $this->dataLists(new PropertyServiceWorkLists());
    }

    /**
     * 代理人团购活动列表
     */
    public function propertyGroupActivityLists()
    {
        return $this->dataLists(new PropertyGroupActivityLists());
    }
    
}