<?php

namespace App\Http\Controllers;

use App\Http\Services\RoleService;
use App\Http\Services\SupplierAccountService;
use App\Http\Services\SupplierContactService;
use App\Http\Services\SupplierService;
use App\Http\Services\SupplierSubAccountService;
use App\Http\Services\ViewCheckService;
use App\Model\IntracodeModel;
use App\Model\SupplierAccountModel;
use App\Model\SupplierChannelModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class SupplierAccountController extends Controller
{
    public function info(Request $request, $id = '')
    {
        if ($request->path() == '/') {
            $path = 'web/index';
        } else {
            $path = $request->path();
        }
        $this->data = [
            'menus' => $request->menus,
            'header' => $request->user->header,
            'username' => $request->user->email,
            'user_email' => $request->user->email,
            'uri' => '/' . $path,
            'id' => $id
        ];
        return $this->$id($request);
    }

    public function __call($name, $arr)
    {
        $data['errinfo'] = '访问路径错误';
        return view('errors.error', $data);
    }


    //操作日志列表
    public function SupplierAccountList($request)
    {
        $this->data['title'] = '芯链账号列表';
        return $this->view('芯链账号列表');
    }

    public function AddSupplierAccount($request)
    {
        //获取所有启用的供应商编码,而且类型是包括芯链
        $model = new SupplierChannelModel();
        $suppliers = $model->where('is_type', 0)
            ->whereRaw('stockup_type like "%5%"')
            ->where('status', SupplierChannelModel::STATUS_PASSED)
            ->select(['supplier_name', 'supplier_code'])->get();
        $supplierCodes = [];
        foreach ($suppliers as $supplier) {
            $supplier = $supplier->toArray();
            $supplierCodes[$supplier['supplier_code']] = $supplier['supplier_name'] . '(' . $supplier['supplier_code'] . ')';
        }
        $this->data['supplierCodes'] = $supplierCodes;
        $this->data['title'] = '添加芯链账号';
        return $this->view('添加芯链账号');
    }

    public function UpdateSupplierAccount($request)
    {
        $id = $request->get('id');
        $model = new SupplierAccountModel();
        $account = $model->where('id', $id)->first()->toArray();
        //获取所有启用的供应商编码,而且类型是包括芯链
        $model = new SupplierChannelModel();
        $suppliers = $model->where('is_type', 0)
            ->whereRaw('stockup_type like "%5%"')
            ->where('status', SupplierChannelModel::STATUS_PASSED)
            ->select(['supplier_name', 'supplier_code'])->get();
        $supplierCodes = [];
        foreach ($suppliers as $supplier) {
            $supplier = $supplier->toArray();
            $supplierCodes[$supplier['supplier_code']] = $supplier['supplier_name'] . '(' . $supplier['supplier_code'] . ')';
        }
        $this->data['supplierCodes'] = $supplierCodes;
        $this->data['title'] = '编辑芯链账号';
        $this->data['account'] = $account;
        return $this->view('编辑芯链账号');
    }

    //子账号列表
    public function SupplierSubAccountList($request)
    {
        $accountId = $request->input('account_id');
        if (!$accountId) {
            return '账号id不能为空';
        }
        $supplierId = SupplierAccountModel::where('id', $accountId)->value('supplier_id');
        $supplier = (new SupplierService())->getSupplier($supplierId);
        $supplierAccount = (new SupplierAccountService())->getSupplierAccountBySupplierId($supplierId);
        $this->data['supplier'] = $supplier;
        $this->data['supplierAccount'] = $supplierAccount;
        $this->data['title'] = '子账号管理';
        return $this->view('子账号管理');
    }

    //新增修改子账号
    public function SaveSupplierSubAccount($request)
    {
        $accountId = $request->input('account_id');
        $sbatId = $request->input('sbat_id');
        $ynatId = $request->input('ynat_id');
        if (!empty($sbatId)) {
            $account = (new SupplierSubAccountService())->getSupplierSubAccount($accountId, $sbatId);
            $this->data['account'] = $account;
        }
        $menuPerms = (new SupplierSubAccountService())->getSupplierAccountPermList($accountId, $sbatId);
        //区分主账号还是子账号保存消息权限
        if ($sbatId) {
            $messagePerms = (new SupplierSubAccountService())->getSupplierAccountMessagePermList($accountId, $ynatId);
        } else {
            $messagePerms = (new SupplierSubAccountService())->getSupplierAccountMessagePermList($accountId, $ynatId);
        }
        $this->data['menuPerms'] = $menuPerms;
        $this->data['messagePerms'] = $messagePerms;
        $this->data['title'] = '子账号编辑';
        return $this->view('子账号编辑');
    }

    //修改主账号的消息权限
    //新增修改子账号
    public function SaveSupplierAccountMessagePerm($request)
    {
        $accountId = $request->input('account_id');
        $messagePerms = (new SupplierSubAccountService())->getSupplierAccountMessagePermList($accountId, $accountId);
        $this->data['messagePerms'] = $messagePerms;
        $this->data['title'] = '子账号编辑';
        return $this->view('子账号编辑');
    }
}