<?php

namespace App\Http\Controllers\Wap;

use App\Http\Models\FlowInfoTpl;
use App\Http\Models\FlowPlan;

use App\Http\Models\Role;
use App\Http\Models\Site;
use App\Http\Models\User;
use App\Http\Models\FlowStage;
use App\Http\Controllers\Controller;


class FlowController extends Controller
{

    //流程报告
    public function index($siteIdBase64)
    {
        $siteId = base64_decode($siteIdBase64);

        $site = Site::query()->select(['id', 'cn_title'])->where(['id' => $siteId])->first();
        if (!$site) {
            return abort(404);
        }

        $roleScope = array_keys(FlowInfoTpl::RoleScope);
        unset($roleScope[1]); //删除客户
        $mapUsers = User::query()->select(['id', 'role_id', 'nickname'])->whereIn('role_id', $roleScope)->get()->keyBy('id')->toArray();

        $site = Site::query()->select(['cn_title'])->find($siteId);

        $mapUsers['-1'] = ['id' => -1, 'role_id' => -1, 'nickname' => $site->cn_title ?? '站点名称'];

        $stageList = FlowStage::query()->where(['site_id' => $siteId])
            ->with('infoList')->orderBy('sort')->get();

        $week = date('YW');
        $nowPlanList = FlowPlan::query()->where(['site_id' => $siteId, 'week' => $week])->get();

        $nextWeek = date('YW', strtotime('+1 week'));
        $nextPlanList = FlowPlan::query()->where(['site_id' => $siteId, 'week' => $nextWeek])->get();

        $historyPlanList = FlowPlan::query()->where([['site_id', '=', $siteId], ['week', '<', $week]])->get();

        foreach ($nowPlanList as $key => $value) {
            $nowPlanList[$key]->usernameList = $this->getUserInfo($value, $siteId);
        }

        foreach ($nextPlanList as $key => $value) {
            $nextPlanList[$key]->usernameList = $this->getUserInfo($value, $siteId);
        }

        foreach ($historyPlanList as $key => $value) {
            $historyPlanList[$key]->usernameList = $this->getUserInfo($value, $siteId);
        }

        return view('wap.flow', [
            'site' => $site,
            'stageList' => $stageList,
            'mapUsers' => $mapUsers,
            'nowPlanList' => $nowPlanList ?? [],
            'nextPlanList' => $nextPlanList ?? [],
            'historyPlanList' => $historyPlanList ?? [],
        ]);
    }

    public function getUserInfo($user, $siteId)
    {
        $list = [];
        $userList = $this->getUserList($user->user_ids, $siteId);
        if (!empty($userList)) {
            foreach ($userList as $kk => $vv) {
                $list[] = $vv;
            }
        }

        if (!empty($user->username)) {
            $list[] = $user->username;
        }

        return $list;
    }

    /**
     * 用户列表
     * @param $userIds
     * @param $siteId
     * @return array
     */
    protected function getUserList($userIds, $siteId)
    {
        $userList = [];
        $condition = explode(',', $userIds);
        $roleList = Role::query()->pluck('name', 'id')->toArray() ?? [];

        foreach ($condition as $key => $value) {
            if ($value == '-1') {
                $account = Site::query()->where('id', $siteId)->value('cn_title');
                if (!empty($account)) {
                    $userList[] = $account . '【客户】';
                }
                unset($condition[$key]);
            }
        }

        foreach ($condition as $key => $value) {
            $userInfo = User::query()->where('id', $value)->first();
            if (!empty($userInfo)) {
                $roleId = $userInfo->role_id;
                $nickname = $userInfo->nickname;
                $nickname = $nickname . '【' . $roleList[$roleId] . '】';
                $userList[] = $nickname;
            }
        }

        return $userList;
    }

}