<?php
// +----------------------------------------------------------------------
// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
// +----------------------------------------------------------------------
// | Author: 萤火科技 <admin@yiovo.com>
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\api\controller;

use app\api\model\store\Shop as ShopModel;
use think\response\Json;

/**
 * 门店列表
 * Class Shop
 * @package app\api\controller
 */
class Shop extends Controller
{
    /**
     * 门店列表
     * @return Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function list(): Json
    {
        $model = new ShopModel;
        $list = $model->getList($this->request->param());
        return $this->renderSuccess(compact('list'));
    }

    /**
     * 门店详情
     * @param int $shopId
     * @return Json
     */
    public function detail(int $shopId): Json
    {
        $detail = ShopModel::detail($shopId, ['logoImage']);
        return $this->renderSuccess(compact('detail'));
    }

    public function stopTimes(): Json
    {
        $data = [];
        $shopId = intval($this->request->post('shop_id'));
        if (!$shopId) {
            return $this->renderError('缺少必要参数');
        }
        $detail = ShopModel::detail($shopId);
        if (!empty($detail['shop_hours'])) {
            $shop_hours = explode('-', $detail['shop_hours']);
            if (count($shop_hours) == 2) {
                $array = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
                for ($i = 0; $i < 3; $i++) {
                    $start_time = date('Y-m-d', strtotime('+' . $i . ' day', time())) . ' ' . $shop_hours[0];
                    $end_time = date('Y-m-d', strtotime('+' . $i . ' day', time())) . ' ' . $shop_hours[1];
                    $timeOne = strtotime($start_time);
                    $timeTwo = strtotime($end_time);
                    $times = [];
                    while ($timeOne < $timeTwo) {
                        $status = 0;
                        if ($timeOne > time()) {
                            $status = 1;
                        }
                        $times[] = [
                            'value' => date('H:i', $timeOne),
                            'status' => $status,
                        ];
                        $timeOne = $timeOne + 30 * 60;
                    }
                    $data[] = [
                        'name' => $array[date("w", strtotime($start_time))],
                        'list' => $times,
                    ];
                }
            }
        }
        return $this->renderSuccess($data);
    }
}