<?php

namespace App\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class ReportSale extends Model {
	//
	use SoftDeletes;

	const NOT_PROCESS = 0; // chưa xử lý
	const PROCESSING = 1; // đang xử lý
	const RIGHT = 2; // TT đúng
	const WRONG = 3; // TT sai


	const TYPE_WRONG_DATA = 0; //sai thông tin
	const TYPE_CANT_CONTACT = 1; //không liên lạc được
	const TYPE_NOT_REAL = 2; //nhà không có thật
	const TYPE_HIRED = 3; // nhà đã cho thuê
	const TYPE_SALE = 4; // môi giới

	const TYPES = [
		self::TYPE_WRONG_DATA   => 'Sai thông tin',
		self::TYPE_CANT_CONTACT => 'Không liên lạc được',
		self::TYPE_NOT_REAL     => 'Nhà không có thật',
		self::TYPE_HIRED        => 'Nhà đã cho thuê',
		self::TYPE_SALE         => 'Môi giới',
	];

	protected $fillable = [
		'user_id',
		'conversation_id',
		'owner_id',
		'note',
		'content',
		'hostel_id',
		'status',
		'hostel_post_crawl_id'
	];

	protected $dates = [
		'created_at',
		'updated_at'
	];

	public function user() {
		return $this->belongsTo( User::class, 'user_id', 'id' );
	}

	public function owner() {
		return $this->belongsTo( User::class, 'owner_id', 'id' );
	}

	public function types() {
		return $this->hasMany( ReportSaleType::class, 'report_sale_id', 'id' );
	}

	public function hostel() {
		return $this->belongsTo( Hostel::class );
	}

	public function getStatusTextAttribute()
	{
		if ( $this->attributes['status'] == ReportSale::NOT_PROCESS ) {
			return 'Chờ xử lý';
		}

		if ( $this->attributes['status'] == ReportSale::PROCESSING ) {
			return 'Đang xử lý';
		}

		if ( $this->attributes['status'] == ReportSale::RIGHT ) {
			return 'Thông tin đúng';
		}

		if ( $this->attributes['status'] == ReportSale::WRONG ) {
			return 'Thông tin sai';
		}

		return 'Chưa xác thực';
	}
}
