<?php


namespace App\Http\Controllers\Admin;


use App\Jobs\ClearCache;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class SystemStatusInfoController
{
    // 客服功能
    public function customer_service(Request $request)
    {
        if ($request->isMethod('post')) {
            $post = $request->post();

            if (is_array($post)) {
                foreach ($post as $key => $value) {

//                    if ($key == 'WhatsApp') {
//                        DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
//                            ->where('StatusName', $key)
//                            ->update(['StatusString' => $value, 'StatusValue' => $post['WhatsApp_ID']]);
//
//                        DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
//                            ->where('StatusName', $key)
//                            ->update(['StatusString' => $value, 'StatusValue' => $post['WhatsApp_ID']]);
//                    } else {
                    if(strstr($value,'{')) {
                        try {
                            $value=json_decode($value,true);
                            $def=$value['default'];
                            $value['default']=$def;
                            $value=json_encode($value);
                        } catch (\Exception $e) {
                            return apiReturnFail("修改失败，格式有误");
                        }
                    }
                        DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
                            ->where('StatusName', $key)
                            ->update(['StatusString' => $value]);
//                    }
                }
            }
            ClearCache::dispatch();
            Redis::del('customer_service');
            Redis::del('customer_service_n');
            return apiReturnSuc();
        }
        $list = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
            ->where('StatusName', 'Email')
            ->orWhere('StatusName', 'WhatsApp')
            ->orWhere('StatusName', 'WhatsApp_ID')
            ->orWhere('StatusName', 'Telegram')
            ->get();
        $Channels = session('all_channels');
        $title="客服信息管理";
        return view('admin.system_status_info.customer_service', compact('list','Channels','title'));
    }
    public function high_customer_service(Request $request)
    {
        if ($request->isMethod('post')) {
            $post = $request->post();

            if (is_array($post)) {
                foreach ($post as $key => $value) {

                        DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
                            ->where('StatusName', $key)
                            ->update(['StatusString' => $value]);
                }
            }
            ClearCache::dispatch();
            Redis::del('high_customer_service');
            return apiReturnSuc();
        }
        $list = DB::connection('write')->table('QPAccountsDB.dbo.SystemStatusInfo')
            ->orWhere('StatusName', 'High_WhatsApp')
            ->orWhere('StatusName', 'High_WhatsApp_ID')
            ->get();
        $Channels = session('all_channels');
        $title="高级客服信息管理";
        return view('admin.system_status_info.customer_service', compact('list','Channels','title'));
    }
}
