<?php

namespace App\Http\Controllers\Admin;

use App\Exports\EarningGradeLogsExport;
use App\Exports\EarningGradeUsersExport;
use App\Http\Controllers\Controller;
use App\Http\Response\ResponseJson;
use App\Models\User;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Maatwebsite\Excel\Facades\Excel;

class EarningController extends Controller
{
    use ResponseJson;

    public $client;
    public function __construct()
    {
        $client = new Client();
        $this->client = $client;
    }

    public function grades(Request $request)
    {
        $url = config('app.shop_url')."admin/earning/grades";
        $headers = [
            'Key' => config('app.shop_key'), // 自定义头部参数（示例）
        ];
        $response = $this->client->get($url, [
            'headers'=>$headers,
            'json'=>$request->all(),
        ]);
        $body = $response->getBody()->getContents(); // 获取响应内容
        $res = json_decode($body, true);
        if($res['code'] == 1) return $this->failure($res['message']);

        $data = $res['data'] ?? [];
        return $this->success('ok', $data);
    }

    public function storeGrades(Request $request)
    {
        $url = config('app.shop_url')."admin/earning/grades";
        $headers = [
            'Key' => config('app.shop_key'), // 自定义头部参数（示例）
        ];
        $response = $this->client->post($url, [
            'headers'=>$headers,
            'json'=>$request->all(),
        ]);
        $body = $response->getBody()->getContents(); // 获取响应内容
        $res = json_decode($body, true);
        if($res['code'] == 1) return $this->failure($res['message']);
        $data = $res['data'] ?? [];
        return $this->success('ok', $data);
    }

    public function updateGrade(Request $request, $grade_id)
    {
        $url = config('app.shop_url')."admin/earning/grades/$grade_id";
        Log::info("修改分佣url: $url");
        $headers = [
            'Key' => config('app.shop_key'), // 自定义头部参数（示例）
        ];
        $response = $this->client->put($url, [
            'headers'=>$headers,
            'json'=>$request->all(),
        ]);
        $body = $response->getBody()->getContents(); // 获取响应内容
        $res = json_decode($body, true);
        if($res['code'] == 1) return $this->failure($res['message']);
        $data = $res['data'] ?? [];
        return $this->success('ok', $data);
    }

    public function deleteGrade(Request $request, $grade_id)
    {
        $url = config('app.shop_url')."admin/earning/grades/$grade_id";
        $headers = [
            'Key' => config('app.shop_key'), // 自定义头部参数（示例）
        ];
        $response = $this->client->delete($url, [
            'headers'=>$headers,
            'json'=>$request->all(),
        ]);
        $body = $response->getBody()->getContents(); // 获取响应内容
        $res = json_decode($body, true);
        if($res['code'] == 1) return $this->failure($res['message']);
        $data = $res['data'] ?? [];
        return $this->success('ok', $data);
    }

    public function storeGradeUsers(Request $request, $grade_id)
    {
        $url = config('app.shop_url')."admin/earning/grades/$grade_id/users";
        $headers = [
            'Key' => config('app.shop_key'), // 自定义头部参数（示例）
        ];
        $data = $request->all();
        if (!isset($data['user_id']) || empty($data['user_id'])) throw new \Exception("缺少参数user_id");
        $data['accid'] = User::SAAS_PREFIX.$data['user_id'];
        $response = $this->client->post($url, [
            'headers'=>$headers,
            'json'=>$data,
        ]);
        $body = $response->getBody()->getContents(); // 获取响应内容
        $res = json_decode($body, true);
        if($res['code'] == 1) return $this->failure($res['message']);
        $data = $res['data'] ?? [];
        return $this->success('ok', $data);
    }

    public function deleteGradeUsers(Request $request, $grade_id)
    {
        $url = config('app.shop_url')."admin/earning/grades/$grade_id/users";
        $headers = [
            'Key' => config('app.shop_key'), // 自定义头部参数（示例）
        ];
        $data = $request->all();
        if (!isset($data['user_id']) || empty($data['user_id'])) throw new \Exception("缺少参数user_id");
        $data['accid'] = User::SAAS_PREFIX.$data['user_id'];
        $response = $this->client->delete($url, [
            'headers'=>$headers,
            'json'=>$data,
        ]);
        $body = $response->getBody()->getContents(); // 获取响应内容
        $res = json_decode($body, true);
        if($res['code'] == 1) return $this->failure($res['message']);
        $data = $res['data'] ?? [];
        return $this->success('ok', $data);
    }

    public function gradeUsers(Request $request, $grade_id)
    {
        $url = config('app.shop_url')."admin/earning/grades/$grade_id/users";
        $headers = [
            'Key' => config('app.shop_key'), // 自定义头部参数（示例）
        ];
        $response = $this->client->get($url, [
            'headers'=>$headers,
            'json'=>$request->all(),
        ]);
        $body = $response->getBody()->getContents(); // 获取响应内容
        $res = json_decode($body, true);
        if($res['code'] == 1) return $this->failure($res['message']);
        $data = $res['data'] ?? [];
        return $this->success('ok', $data);
    }

    public function gradeUsersExport(Request $request, $grade_id)
    {
        $url = config('app.shop_url')."admin/earning/grades/$grade_id/users";
        $headers = [
            'Key' => config('app.shop_key'), // 自定义头部参数（示例）
        ];
        $data = $request->all();
        $data['nopage'] = 1;
        $response = $this->client->get($url, [
            'headers'=>$headers,
            'json'=>$data,
        ]);
        $body = $response->getBody()->getContents(); // 获取响应内容
        $res = json_decode($body, true);
        if($res['code'] == 1) return $this->failure($res['message']);
        $data = $res['data'] ?? [];
        return Excel::download(new EarningGradeUsersExport($data), $request->start_time."-".$request->end_time."分佣记录.xlsx");
    }

    public function logs(Request $request)
    {
        $url = config('app.shop_url')."admin/earning/logs";
        $headers = [
            'Key' => config('app.shop_key'), // 自定义头部参数（示例）
        ];
        $response = $this->client->get($url, [
            'headers'=>$headers,
            'json'=>$request->all(),
        ]);
        $body = $response->getBody()->getContents(); // 获取响应内容
        $res = json_decode($body, true);
        if($res['code'] == 1) return $this->failure($res['message']);
        $data = $res['data'] ?? [];
        return $this->success('ok', $data);
    }

    public function exportLogs(Request $request)
    {
        $url = config('app.shop_url')."admin/earning/logs";
        $headers = [
            'Key' => config('app.shop_key'), // 自定义头部参数（示例）
        ];
        $data = $request->all();
        $data['nopage'] = 1;
        $response = $this->client->get($url, [
            'headers'=>$headers,
            'json'=> $data,
        ]);
        $body = $response->getBody()->getContents(); // 获取响应内容
        $res = json_decode($body, true);
        if($res['code'] == 1) return $this->failure($res['message']);
        $data = $res['data'] ?? [];
        return Excel::download(new EarningGradeLogsExport($data), $request->start_time."-".$request->end_time."分佣记录.xlsx");
    }
}
