<?php
/**
 * ----------------------------------------------------------------------------
 * 行到水穷处，坐看云起时
 * 开发软件，找贵阳云起信息科技，官网地址:https://www.56q7.com/
 * ----------------------------------------------------------------------------
 * Author: 老成
 * email：85556713@qq.com
 */
declare (strict_types = 1);

namespace app\admin\controller;

use app\common\model\Admin;
use app\common\model\delivery\Text;
use app\common\model\MoneyLog;
use app\common\model\StockDetail;
use app\common\model\StockConfig;
use app\common\model\ShopDelivery;
use app\common\controller\Backend;
use think\annotation\route\Route;
use think\facade\Db;

/**
 * 控制台
 */
class Dashboard extends Backend
{
    public function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 查看
     */
    #[Route('GET','dashboard/index')]
    public function index(StockConfig $stockConfig, StockDetail $stockDetail, MoneyLog $moneyLog, ShopDelivery $shopDelivery)
    {
        if($this->request->isAjax()){
            //顶部统计
            $moneyLog = $moneyLog::getCountMonthBalance();
            $panel=[
                rand(100,1000),
                rand(100,1000),
                $moneyLog['balance'], // 当前结余（所有收入-支出）
                $shopDelivery::getPendingSettlementAmount(),  //待结算金额
            ];
            $variety = [];
            $box = [];
            $material = [];
            $list = $stockConfig->where('status', $stockConfig::StatusNormal)->field('id,type_id,title')->orderRaw("field('variety_name','packing_box','material')")->select();
            foreach ($list as $item) {
                  switch ($item['type_id']) {
                        case $stockConfig::VarietyName:
                              $variety[] = [
                                    'name' => $item->title,
                                    'num'  => $stockDetail->where('key', $item['id'])->value('num')
                              ];
                              break;
                        case $stockConfig::PackingBox:
                             $box[] = [
                              'name' => $item->title,
                              'num'  => $stockDetail->where('key', $item['id'])->value('num')
                             ];
                             break;
                        case $stockConfig::Material:
                             $material[] = [
                              'name' => $item->title,
                              'num'  => $stockDetail->where('key', $item['id'])->value('num')
                             ];
                             break;
                  }
            }
            $this->success('',compact('panel','variety','box','material'));
        }
        return $this->fetch();
    }

    #[Route('GET','dashboard/platform1')]
    public function platform1()
    {
        return $this->fetch();
    }

    #[Route('GET','dashboard/platform2')]
    public function platform2()
    {
        return $this->fetch();
    }
}
