<?php

namespace App\Admin\Controllers;

use App\Admin\Metrics\Examples;
use App\Http\Controllers\Controller;
use App\Models\Covenant;
use App\Models\Lawyer;
use Dcat\Admin\Http\Controllers\Dashboard;
use Dcat\Admin\Layout\Column;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Tab;
use Dcat\Admin\Grid;
use App\Models\Lawyer as ModelsLawyer;
use App\Models\CovenantReceivePayment as ModelCovenantReceivePayment;
use App\Admin\Repositories\LawyerReceivePaymentCollect;
use App\Admin\Repositories\LawyerInvoicedPaymentCollect;

class ReceivePaymentController extends Controller
{
    public function index(Content $content)
    {
        // 创建第一个数据列表 已收款汇总表
        $grid1 = Grid::make(new LawyerReceivePaymentCollect(), function (Grid $grid) {
            $lawyerID = $grid->model()->filter()->input('no') ?? ''; //律师ID
            $nowyear = date("Y");
            $year = $grid->model()->filter()->input('year') ?? date('Y');
            $monthNum = ($year && $year < $nowyear)  ? 12 : date('m');

            for ($i = 1; $i <= $monthNum; $i++) {
                $grid->column($i, $i . '月');
            }

            $grid->disableActions();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disablePagination();

            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->like('year', '年份')->width(3)->default(date("Y"))->ignore();
            });

            //文字信息
            $grid->tools(function (Grid\Tools $tools) use ($grid, $lawyerID) {
                $fullname = $number = '';
                if ($lawyerID) {
                    $obj = ModelsLawyer::find($lawyerID);
                    $fullname = $obj->name;
                    $number = $obj->number;
                }
                $card_info = "&nbsp;&nbsp;律师编号:{$number}&nbsp;&nbsp;&nbsp;律师姓名:{$fullname}";

                $tools->append($card_info);
            });

            $grid->export()->disableExportAll();
            $grid->export()->disableExportSelectedRow();
            $grid->export()->rows(function ($rows) {
                return $rows;
            });
        });


        // 创建第二个数据列表
        $grid2 = Grid::make(new LawyerInvoicedPaymentCollect(), function (Grid $grid) {
            $lawyerID = $grid->model()->filter()->input('no') ?? ''; //律师ID
            $nowyear = date("Y");
            $year = $grid->model()->filter()->input('year') ?? date('Y');
            $monthNum = ($year && $year < $nowyear)  ? 12 : date('m');

            for ($i = 1; $i <= $monthNum; $i++) {
                $grid->column($i, $i . '月');
            }

            $grid->disableActions();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableRowSelector();
            $grid->disablePagination();

            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->like('year', '年份')->width(3)->default(date("Y"))->ignore();
            });

            //文字信息
            $grid->tools(function (Grid\Tools $tools) use ($grid, $lawyerID) {
                $fullname = $number = '';
                if ($lawyerID) {
                    $obj = ModelsLawyer::find($lawyerID);
                    $fullname = $obj->name;
                    $number = $obj->number;
                }
                $card_info = "&nbsp;&nbsp;律师编号:{$number}&nbsp;&nbsp;&nbsp;律师姓名:{$fullname}";

                $tools->append($card_info);
            });

            $grid->export()->disableExportAll();
            $grid->export()->disableExportSelectedRow();
            $grid->export()->rows(function ($rows) {
                return $rows;
            });
        });


        // 使用Tab面板展示两个数据列表
        $tab = Tab::make()->add('已收款汇总表', $grid1)->add('已开票汇总表', $grid2);

        return $content->body($tab);
    }
}