<?php

namespace app\admin\controller\order;

use app\admin\service\order\AppointmentService;
use app\model\Appointment;
use app\model\Goods;
use support\Db;
use support\Request;
use support\Response;

class AppointmentController
{
    /**
     * @Desc 列表
     * @Author Gorden
     * @Date 2024/3/29 10:44
     *
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function select(Request $request): Response
    {

        return AppointmentService::select($request);
    }

    public function confirm($id): Response
    {
        return AppointmentService::confirm($id);
    }

    /**
     * 到店
     */
    public function arrival(Request $request)
    {
        return AppointmentService::arrival($request);
    }

    public function done(Request $request)
    {
        return AppointmentService::done($request);
    }

    public function add(Request $request)
    {
        return AppointmentService::add($request);
    }

    public function update(Request $request)
    {
        return AppointmentService::update($request);
    }
    
    public function cancelApply(Request $request)
    {
        return AppointmentService::cancelApply($request);
    }

    public function getTimes(Request $request)
    {
        return AppointmentService::getTimes($request);
    }

    
    public function myAppointments(Request $request)
    {
        $page = $request->get('page',1);
        $pageSize = $request->get('pageSize',10);
        $memberId = $request->get('member_id', '');
        $datetime = $request->get('order_addtimes',[]);
        $goodsName = $request->get('goods_name','');
        if (!$memberId) {
            return json_fail('查询错误');
        }

        $goodsIds = [];
        if (!empty($goodsName)){
            $goodsIds = Goods::where('goods_name','like','%'.$goodsName.'%')->pluck('goods_id')->toArray();
        }
        // 预约单
        $appointments = Db::table('appointment')
//            ->leftJoin('member', 'member.member_id', '=', 'appointment.join_appointment_member_id')
//            ->leftJoin('member_cert', 'member_cert.join_cert_member_id', '=', 'appointment.join_appointment_member_id')
//            ->leftJoin('member_info', 'member_info.join_info_member_id', '=', 'appointment.join_appointment_member_id')
            ->leftJoin('goods', 'goods.goods_id', '=', 'appointment.join_appointment_goods_id')
//            ->leftJoin('order', 'order.order_id', '=', 'appointment.join_appointment_order_id')
            ->leftJoin('member_benefit', 'member_benefit.member_benefit_id', '=', 'appointment.join_appointment_member_benefit_id')
            ->leftJoin('goods_sku', 'goods_sku.goods_sku_id', '=', 'appointment.join_appointment_goods_sku_id')
            ->where('join_appointment_member_id', $memberId)
            ->whereIn('appointment_status', ['WAITING', 'PENDING', 'DOING', 'DONE', 'CANCEL', 'REVOKE'])
            ->select('appointment.*',
//            'member.member_mobile',
//            'member_cert.member_cert_name',
//            'member_info.member_info_nickname',
                'goods.goods_name', 'goods.goods_sales_price',
                'goods_sku.goods_sku_specs_json',
//            'order.order_name', 'goods_sku.goods_sku_specs_json', 'order.order_amount_total', 'order.order_amount_pay',
//            'order.order_category', 'order.order_status_system', 'order.order_status_payment', 'order.order_status_storage',
//            'order.order_addtimes',
                'member_benefit.member_benefit_name'
            )->when(!empty($goodsIds),function($query) use ($goodsIds){
                $query->whereIn('join_appointment_goods_id',$goodsIds);
            })
            ->when(!empty($datetime), function($query) use ($datetime){
                $datetime[0] = strtotime($datetime[0]);
                $datetime[1] = strtotime($datetime[1]);
                $query->whereBetween('appointment_addtimes',$datetime);
            });
        $total = $appointments->count();
        $rows = $appointments->orderBy('appointment.appointment_addtimes', 'desc')
            ->forPage($page,$pageSize)
            ->get();
        foreach ($rows as &$row) {
            $row->appointment_addtimes = date('Y-m-d H:i:s', $row->appointment_addtimes);
            if ($row->appointment_category == 'NOFEE') {
                $row->member_benefit_name = "后付费预约";
            }
            if (!empty($row->goods_sku_specs_json)){
                $row->goods_sku_specs_json = json_decode($row->goods_sku_specs_json);
            }
            if (!empty($row->appointment_apply_json)){
                $applyJson = json_decode($row->appointment_apply_json,true);
                $row->appointment_apply_json = $applyJson;
                $row->person = $applyJson['person'] ?? '';
                $row->appointment_times = $applyJson['times'] ?? '';
            }
        }

        return json_success('', compact('rows', 'page', 'pageSize', 'total'));
    }
}