<?php
/**
 * Its used for api base
 *
 * @author: Sarwar Hasan
 * @version 1.0.0
 * @package minicart_lite\Libs
 */

namespace minicart_lite\libs;

use Appsbd_Lite\V5\libs\AppInput;

/**
 * Class Apbd_Nmca_Discount_Item
 *
 * @package minicart_lite\libs
 */
class Apbd_Nmca_Discount_Item {
	/**
	 * Its property id
	 *
	 * @var int
	 */
	public $id;

	/**
	 * Its property base_amount
	 *
	 * @var int
	 */
	public $base_amount = 0;
	/**
	 * Its property view_amount
	 *
	 * @var int
	 */
	public $view_amount = 0;
	/**
	 * Its property offer_type
	 *
	 * @var string
	 */
	public $offer_type = 'A';
	/**
	 * Its property discount_type
	 *
	 * @var string
	 */
	public $discount_type = 'F';
	/**
	 * Its property discount_amount
	 *
	 * @var int
	 */
	public $discount_amount = 0;
	/**
	 * Its property status
	 *
	 * @var string
	 */
	public $status;
	/**
	 * Its property eu_date
	 *
	 * @var int
	 */
	public $eu_date;

	/**
	 * Apbd_Nmca_Discount_Item constructor.
	 */
	public function __construct() {
		$this->status = 'A';
		$this->eu_date = time();
	}

	/**
	 * The set from post is generated by appsbd
	 *
	 * @param false $is_with_id Its is_with_id param.
	 */
	public function set_from_post( $is_with_id = false ) {
		if ( $is_with_id ) {
			$this->set_id();
		}
		$this->base_amount = AppInput::post_value( 'base_amount', 0 );
		$this->view_amount = AppInput::post_value( 'view_amount', 0 );
		$this->offer_type = AppInput::post_value( 'offer_type', 'A' );
		$this->discount_type = AppInput::post_value( 'discount_type', 'F' );
		$this->discount_amount = AppInput::post_value( 'discount_amount', 0 );
		$this->status = AppInput::post_value( 'status', 'A' );
		$this->eu_date = time();
	}

	/**
	 * The set id is generated by appsbd
	 */
	public function set_id() {
		if ( empty( $this->id ) ) {
			$this->id = hash( 'crc32b', time() . wp_rand( 10, 99 ) );
		}
	}

	/**
	 * The is valid is generated by appsbd
	 *
	 * @param string $msg Its msg param.
	 *
	 * @return bool
	 */
	public function is_valid( &$msg = '' ) {
		if ( empty( $this->base_amount ) ) {
			$msg = 'Minimum cart amount is empty';
			return false;
		}
		if ( empty( $this->view_amount ) ) {
			$msg = 'View cart amount is empty';
			return false;
		}
		if ( $this->base_amount <= $this->view_amount ) {
			$msg = 'View  amount is greater then or equals to minimum cart amount';
			return false;
		}
		return true;
	}
}
