<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class SendZaloLog extends Model
{
    //
    const TYPE_SPEND = 1;
    const TYPE_ADD = 2;

    protected $fillable = [
        'user_id',
        'properties',
        'response',
        'hostel_id',
        'room_id',
        'customer_name',
        'customer_phone',
        'number_change',
        'type'
    ];

    protected $dates = [
        'created_at'
    ];

    protected $casts = [
        'properties' => 'array',
        'response' => 'array'
    ];

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

    public function room()
    {
        return $this->belongsTo(Room::class);
    }

    public function getContentAttribute()
    {
        if ($this->type == self::TYPE_SPEND) {
            $tem = 'Khách hàng ' . $this->customer_name . ' tại phòng ' . $this->room_name . ' nhà ' . $this->hostel_name . ' 
        có hóa đơn chưa thanh toán với tổng số tiền là ' . $this->properties['template_data']['amount'] . ' VNĐ. 
        Quý khách vui lòng bỏ qua tin nhắn này nếu đã thanh toán hết';
            return $tem;
        }
        return 'Mua thêm ' . $this->attributes['number_change'] . ' tin nhắn';
    }

    public function getStatusAttribute()
    {
        if($this->type == self::TYPE_ADD)
        {
            return 'Thành công';
        }
        $response = $this->response;
        if (is_array($response)) {
            if (!empty($response['error'])) {
                return 'Thất bại (' . $response['error'] . ')';
            }
            return 'Thành công';
        }
        return null;
    }
}
