<?php

namespace App\Http\Controllers\Admin\Sms;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\SmsResult;
use App\Models\DTO\SearchData;

class ResultController extends Controller
{
    private SmsResult $smsResultModel;

    public function __construct(SmsResult $smsResult)
    {
        $this->smsResultModel = $smsResult;
    }

    /**
     * SMS > 전송 결과
     * @method GET
     * @see /admin/sms/result
     */
    public function index(Request $request)
    {
        $params = SearchData::fromRequest($request);
        $smsResultData = $this->smsResultModel->data($params);

        if ($smsResultData->rows > 0) {
            $num = listNum($smsResultData->total, $params->page, $params->perPage);
            foreach ($smsResultData->list as $i => $row) {
                $row->num = $num--;
                $row->callNum = ($row->call_num ?: '-');
                $row->totalCount = number_format($row->total_count);
                $row->successCount = number_format($row->success_count);
                $row->failedCount = number_format($row->failed_count);
                $row->reserveAt = dateBr($row->reserve_at, '-');
                $row->createdAt = dateBr($row->created_at);

                $smsResultData->list[$i] = $row;
            }
        }

        return view('admin.sms.result.index', [
            'smsResultData' => $smsResultData,
            'params' => $params
        ]);
    }

    /**
     * SMS > 전송 결과 삭제
     * @method DELETE
     * @see /admin/sms/result/destroy
     */
    public function destroy(Request $request)
    {
        $chk = $request->post('chk');

        if ($chk) {
            foreach ($chk as $resultID) {
                $this->smsResultModel->find($resultID)->delete();
            }
        }

        $message = '전송 결과가 삭제되었습니다.';
        return redirect()->route('admin.sms.result.index')->with('message', $message);
    }
}
