<?php

namespace App\Http\Controllers;

use App\Http\Services\RoleService;
use App\Http\Services\SupplierContactService;
use App\Http\Services\SupplierService;
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('编辑供应商账号');
    }

}