<?php

namespace App\Http\Controllers;

use http\Header;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yz;

class ViewController extends Controller
{
    public function site()
    {
        $domain = request()->getHost();
        $site = DB::table('site_organization')
            ->where('domain', 'like', "%\"$domain\"%")
            ->where('status', 1)
            ->where('is_del', 0)
            ->first();
        if (!$site) return false;
        return $site;
    }

    public function public_config()
    {
        $request = request();
        $search = $request->get('search') ?? '';
        $configs=DB::table('configs')->whereIn('label',['客服电话','客服在线时间'])->get()->toArray();
        $c_arr=[];
        foreach ($configs as $key=>$value){
                $c_arr=   array_merge($c_arr,[$value->label=>$value->value]);
        }
        return [
            'configs'=>$c_arr,
            'search_value' => $search,
            'search_tags' => ['女性', '男性', '入职', '父母', '高端']
        ];
    }

    public function combo_list(Request $request)
    {
        $site = self::site();
        if (!$site) return '站点暂时关闭';
        $hospital_ids = json_decode($site->hospital, true);
        $type = $request->get('type') ?? '';
        $combo_type = $request->get('combo_type') ?? '';
        $price = $request->get('price') ?? '';
        $sex = $request->get('sex') ?? '';
        $order = $request->get('order') ?? 'zh';
        $search = $request->get('search') ?? '';

        $combos = DB::table('combos')
            ->whereIn('hospital_id', $hospital_ids)
            ->where(function ($query) use ($type) {
                if ($type != '') $query->where('tags', 'like', "%\"$type\"%");
            })
            ->where(function ($query) use ($combo_type) {
                if ($combo_type != '') $query->where('tags', 'like', "%\"$combo_type\"%");
            })
            ->where(function ($query) use ($price) {
                if ($price != '') {
                    $price_arr = explode('-', $price);
                    if (count($price_arr) == 2) {
                        $query->whereBetween('price', [$price_arr[0], $price_arr[1]]);
                    } else {
                        $price_arr = explode('u', $price);
                        $query->where('price', '>=', $price_arr[0]);
                    }
                }
            })
            ->where(function ($query) use ($sex) {
                if ($sex != '') $query->whereIn('sex', [0, $sex]);
            })
            ->where(function ($query) use ($search) {
                if ($search != '') $query->where('tags', 'like', "%\"$search\"%")
                    ->orWhere('name', 'like', "%$search%");
            })
            ->orderBy($order == 'zh' ? 'id' : 'price', $order == 'zh' ? 'desc' : 'asc')
            ->paginate(20)
            ->withQueryString()
            ->toArray();
        $combo_count = DB::table('combos')
            ->whereIn('hospital_id', $hospital_ids)
            ->where(function ($query) use ($type) {
                if ($type != '') $query->where('tags', 'like', "%\"$type\"%");
            })
            ->where(function ($query) use ($combo_type) {
                if ($combo_type != '') $query->where('tags', 'like', "%\"$combo_type\"%");
            })
            ->where(function ($query) use ($price) {
                if ($price != '') {
                    $price_arr = explode('-', $price);
                    if (count($price_arr) == 2) {
                        $query->whereBetween('price', [$price_arr[0], $price_arr[1]]);
                    } else {
                        $price_arr = explode('u', $price);
                        $query->where('price', '>=', $price_arr[0]);
                    }
                }
            })
            ->where(function ($query) use ($sex) {
                if ($sex != '') $query->whereIn('sex', [0, $sex]);
            })
            ->where(function ($query) use ($search) {
                if ($search != '') $query->where('tags', 'like', "%\"$search\"%")
                    ->orWhere('name', 'like', "%$search%");
            })
            ->count();

        $search_arr = [[
            'label' => '体检类型',
            'value' => 'type',
            'options' => [
                ['label' => '不限', 'value' => ''],
                ['label' => '中老年', 'value' => '中老年'],
                ['label' => '青年女性', 'value' => '青年女性'],
                ['label' => '基础套餐', 'value' => '基础套餐'],
                ['label' => '入职体检', 'value' => '入职体检'],
            ]
        ], [
            'label' => '体检套餐',
            'value' => 'combo_type',
            'options' => [
                ['label' => '不限', 'value' => ''],
                ['label' => '肝脏疾病', 'value' => '肝脏疾病'],
                ['label' => '糖尿病', 'value' => '糖尿病'],
                ['label' => '泌尿系统疾病', 'value' => '泌尿系统疾病'],
                ['label' => '高血脂', 'value' => '高血脂'],
                ['label' => '高血糖', 'value' => '高血糖'],
                ['label' => '消化系统疾病', 'value' => '消化系统疾病'],
                ['label' => '前列腺疾病', 'value' => '前列腺疾病'],
                ['label' => '妇科疾病', 'value' => '妇科疾病'],
                ['label' => '甲状腺疾病', 'value' => '甲状腺疾病'],
                ['label' => '心血管疾病', 'value' => '心血管疾病'],
                ['label' => '心脏病', 'value' => '心脏病'],
                ['label' => '骨质疏松', 'value' => '骨质疏松'],
                ['label' => '腰颈肩病', 'value' => '腰颈肩病'],
                ['label' => '风湿病', 'value' => '风湿病'],
                ['label' => '肺部疾病', 'value' => '肺部疾病'],
            ]
        ], [
            'label' => '价格区间',
            'value' => 'price',
            'options' => [
                ['label' => '不限', 'value' => ''],
                ['label' => '100-300', 'value' => '100-300'],
                ['label' => '300-500', 'value' => '300-500'],
                ['label' => '500-800', 'value' => '500-800'],
                ['label' => '800以上', 'value' => '800u'],
            ]
        ], [
            'label' => '性别',
            'value' => 'sex',
            'options' => [
                ['label' => '不限', 'value' => ''],
                ['label' => '男', 'value' => '1'],
                ['label' => '女', 'value' => '2'],
            ]
        ]];
        foreach ($combos['data'] as $combo) {
            $combo->tags_arr = json_decode($combo->tags, true);
        }
//        Yz::debug(['a' => $combos]);
        return view('combo_list.combo_list', [
            'public_config' => self::public_config(),
            'combos' => $combos,
            'combo_count' => $combo_count,
            'search_arr' => $search_arr,
            'search_choose' => [
                'type' => $type,
                'combo_type' => $combo_type,
                'price' => $price,
                'sex' => $sex,
                'order' => $order,
            ]
        ]);
    }

    public function home()
    {
        $site = self::site();
        if (!$site) return '站点暂时关闭';
        $hospital_ids = json_decode($site->hospital, true);
        $combos = DB::table('combos')
            ->whereIn('hospital_id', $hospital_ids)
            ->limit(8)
            ->orderBy('id', 'desc')
            ->get();
        foreach ($combos as $combo) {
            $combo->tags_arr = json_decode($combo->tags, true);
        }
        $hospital_list = DB::table('hospital')
            ->whereIn('id', $hospital_ids)
            ->limit(8)
            ->orderBy('id', 'desc')
            ->get();
        foreach ($hospital_list as $hospital) {
            $hospital->tags_arr = json_decode($hospital->tags, true);
        }

        //获取配置信息
        $configs=DB::table('configs')->whereIn('label',['首页轮播'])->get();
        $c_arr=[];
        foreach ($configs as $key=>$value){
            if($value->label=='首页轮播'){
                $c_arr=   array_merge($c_arr,[$value->label=>json_decode($value->value, true)]);
            }else{
                $c_arr=   array_merge($c_arr,[$value->label=>$value->value]);
               // $c_arr[]=[$value->label=>$value->value];
            }
        }
        //首页导航
        $navs=DB::table('web_nav')->where(['is_del'=>0])->get();
        $nav_list=['navs'=>$navs];

        //合作单位和机构
        $link_units=DB::table('link_units')->where(['status'=>1,'is_del'=>0])->get();
        $link_units=['link_units'=>$link_units];
        $arr= [
            'public_config' => self::public_config(),
            'combos' => $combos,
            'hospitals' => $hospital_list,
        ];
        $arr=array_merge($c_arr,$arr,$nav_list,$link_units);
        return view('home.home', $arr);
    }

    public function combo(Request $request)
    {
        $site = self::site();
        if (!$site) return '站点暂时关闭';
        $hospital_ids = json_decode($site->hospital, true);
        $combo_id = $request->get('id');
        $combo = DB::table('combos')
            ->where('id', $combo_id)
            ->whereIn('hospital_id', $hospital_ids)
            ->first();
        $combo->items_data = json_decode($combo->items, true);
        $hospital = DB::table('hospital')
            ->where('id', $combo->hospital_id)
            ->first();
        $url = $hospital->mp_jump . '?combo=' . $combo->combo_id . '&hospital=' . $hospital->code;
//        Yz::debug([
//            'a' => $hospital
//        ]);
        return view('combo.combo', [
            'public_config' => self::public_config(),
            'qrcode' => $url,
            'combo' => $combo,
            'hospital' => $hospital,
        ]);
    }
}
