<?php
require_once __DIR__ . '/../config.php'; require_admin();


$db = contracts_db();
mysqli_set_charset($db, 'utf8mb4');             // <-- juda muhim
$db->query("SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci");
$db->query("SET time_zone = '+05:00'");



$in=json_decode(file_get_contents('php://input'), true);
$ym=$in['ym']??null; $operator_id=(int)($in['operator_id']??0); $confirm=(int)($in['confirm']??0); $amount=isset($in['amount'])?(int)$in['amount']:null;
if(!$ym || !$operator_id) json_out(['error'=>'ym and operator_id required'],400);
$db=db(); $st=$db->prepare("SELECT id FROM operator_monthly_metrics WHERE operator_id=? AND ym=?"); $st->bind_param("is",$operator_id,$ym); $st->execute(); $ex=$st->get_result()->fetch_assoc(); $st->close();
if($ex){ $q=$db->prepare("UPDATE operator_monthly_metrics SET base_confirmed=?, base_amount=? WHERE operator_id=? AND ym=?"); $q->bind_param("iiis",$confirm,$amount,$operator_id,$ym); $q->execute(); }
else { $q=$db->prepare("INSERT INTO operator_monthly_metrics (operator_id,ym,base_confirmed,base_amount) VALUES (?,?,?,?)"); $q->bind_param("isii",$operator_id,$ym,$confirm,$amount); $q->execute(); }
json_out(['ok'=>true]);
