<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------


namespace app\adminapi\controller\master_worker;


use app\adminapi\controller\BaseAdminController;
use app\adminapi\lists\master_worker\EngineerBillLists;
use app\adminapi\logic\export\ExportLogic;
use app\adminapi\logic\financial\MasterSettlementDetailsLogic;
use app\adminapi\logic\master_worker\EngineerBillLogic;
use app\adminapi\logic\master_worker\EngineerSettlementLogic;
use app\common\service\ExcelExportService;
use excel\ExcelWriter;
use phpseclib3\Common\Functions\Strings;
use think\facade\Db;
use think\facade\Log;

/**
 * EngineerSettlement控制器
 * Class EngineerSettlementController
 * @package app\adminapi\controller
 */
class EngineerBillController extends BaseAdminController
{
    /**
     * @notes 获取列表
     * @return \think\response\Json
     * @author likeadmin
     * @date 2024/11/15 17:21
     */
    public function lists()
    {
        // 导入数据
        $res = EngineerSettlementLogic::insertSettlementDetails();
        if($res === false){
            return $this->fail('导入失败');
        }
        return $this->dataLists(new EngineerBillLists());
    }

    public function download()
    {
        /*$firstDay = date('Y-m-01 00:00:00', strtotime('first day of last month'));
        $lastDay = date('Y-m-t 23:59:59', strtotime('last day of last month'));
        $filename = date('Y-m-d',strtotime($firstDay))."-至-".date('Y-m-d',strtotime($lastDay))."最后结算余额";
        $result = ExportLogic::add([
            'download_type' => 1,
            'params' => ['firstDay'=>$firstDay,'lastDay'=>$lastDay],
            'name' => $filename,
            'admin_id' => $this->adminId,
        ]);
        if (false === $result) {
            return $this->fail(ExportLogic::getError());
        }*/

        $params = $this->request->get();
        $deadline_time = strtotime($params['deadline_time'])+86400;
        $settlement_type = $params['settlement_type'];

        $allData = $this->dataLists(new EngineerBillLists())->getData()['data']['lists'];
        if(empty($allData)) return $this->fail('无数据');

        $type = [
            1=>'周(7天)',
            2=>'半月',
            3=>'整月'
        ];
        $filename = "截止到".$params['deadline_time'].$type[$settlement_type].'型结算单';
        $result = ExportLogic::add([
            'download_type' => 1,
            'params' => ['settlement_type'=>$settlement_type,'deadline_time'=>$deadline_time],
            'name' => $filename,
            'admin_id' => $this->adminId,
        ]);
        if (false === $result) {
            return $this->fail(ExportLogic::getError());
        }
        Log::info('导出参数：'.json_encode([$params,$result]));
        // 暂时 - 立即生成导出文件
        (new ExcelExportService)->download($result);
        return $this->success('添加成功-'.$result, [], 1, 1);
    }
    public function submitForm()
    {
        $params = $this->request->post();
        $params['adminId'] = $this->adminId;
        $result = EngineerBillLogic::submitForm($params);
        if ($result === false){
            return $this->fail(EngineerBillLogic::getError());
        }
        return $this->success('打款成功', [], 1, 1);
    }

}