<?php

namespace App\Http\Controllers\Api\v1;

use App\Models\Lead;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class LeadController extends BaseController
{
    //
	/**
	 * @api {get} /list Lấy danh sách list
	 * @apiName list
	 * @apiGroup Lead
	 * @apiDescription Api Lấy danh sách list
	 * @apiParam {String} limit
	 * @apiParam {String} offset
	 *
	 * @apiSuccess {Number} status 1 hoặc 0. 1 là thành công, 0 là không thành công.
	 * @apiSuccess {String} message  Tin nhắn hệ thống.
	 * @apiSuccess {String} data
	 */
	public function getList(Request $request)
	{
		$limit = $request->input('limit', 10);
		$offset = $request->input('offset', 0);
		$items = Lead::query()
			->limit($limit)
			->offset($offset)
			->get()->map(function($item) {
				return [
					'name' => $item->name,
					'email' => $item->email,
					'phone' => $item->phone,
					'address' => $item->address,
					'source' => $item->source,
					'note' => $item->note,
					'status' => $item->status,

					'province' => [
						'id' => $item->province_id,
						'name' => optional($item->province)->name
					],
					'district' => [
						'id' => $item->district_id,
						'name' => optional($item->district)->name
					],
					'ward' => [
						'id' => $item->ward_id,
						'name' => optional($item->ward)->name
					],
					'source_link' => $item->source_link,
					'price_min' => $item->price_min,
					'price_max' => $item->price_max
				];
			});
		return response([
			'status' => 1,
			'data' => $items
		]);
	}
}
