//===== Custom Script ===========================================================
//= Cash Codes
-	script	credits	-1,{
end;
OnInit:
	bindatcmd "givecredits",strnpcinfo(3)+"::OnGive",99,99;
	bindatcmd "removecredits",strnpcinfo(3)+"::OnRemove",99,99;
	end;

OnRemove:
	mes "^FF0000[ GM Credits ]^000000";
	mes "Remove Credits";
	.@player_name$ = callfunc("F_WhichChar");
	if ( .@player_name$ != "") {
		query_sql( "SELECT `account_id`,`char_id`,`online` FROM `char` WHERE `name` = '"+escape_sql( .@player_name$ )+"' LIMIT 1",.@player_aid,.@player_cid,.@online );
		if (!.@online) { mes "Sorry that character is not online"; close; }
		mes "Player: ^FF0000"+.@player_name$+"^000000";
		input .@credits;
		mes "Credits: ^FF0000"+.@credits+"^000000";
		if (select("Cancel:Proceed") == 2) {
			query_sql "SELECT `account_id`,`balance` FROM cp_credits WHERE `account_id` = "+.@player_aid, .@bool,.@balance;
			if( .@bool ) 
				if (.@balance < .@credits) {
					mes "Credit Balance is not enought";
					close();
				} else {
					query_sql "UPDATE cp_credits SET `balance` = `balance` -"+.@credits+" WHERE `account_id` = "+.@player_aid;
					close();
				}

			else
				mes "Account has no credits";
				close();
		}
		close;
	}	

OnGive:
	mes "^FF0000[ GM Credits ]^000000";
	mes "Add Credits";
	.@player_name$ = callfunc("F_WhichChar");
	if ( .@player_name$ != "") {
		query_sql( "SELECT `account_id`,`char_id`,`online` FROM `char` WHERE `name` = '"+escape_sql( .@player_name$ )+"' LIMIT 1",.@player_aid,.@player_cid,.@online );
		if (!.@online) { mes "Sorry that character is not online"; close; }
		mes "Player: ^FF0000"+.@player_name$+"^000000";
		input .@credits;
		mes "Credits: ^FF0000"+.@credits+"^000000";
		if (select("Cancel:Proceed") == 2) {
			query_sql "SELECT `account_id` FROM cp_credits WHERE `account_id` = "+.@player_aid, .@bool;
			if( .@bool ) 
				query_sql "UPDATE cp_credits SET `balance` = `balance` +"+.@credits+" WHERE `account_id` = "+.@player_aid;
			else
				query_sql "INSERT INTO cp_credits (`account_id`,`balance`,`last_donation_date`,`last_donation_amount`) VALUES ("+.@player_aid+","+.@credits+",NOW(), 0)";
		}
		close;
	}	
}