import { ApiCall } from "tsrpc";
import { Reqdisable, Resdisable } from "../../monopoly/protocols/user/Ptldisable";
import { setGud } from "../../public/gud";

export default async function (call: ApiCall<Reqdisable, Resdisable>) {
    //1. 接受参数
    let value = call.req.value;  //0. 正常， 1. 禁用
    let uid = call.req.uid
    let type = call.req.type; // 1. 封号，2. 禁言
    let data:any = {}
    let typeStr = ''
    //2. 写入参数
    switch(type) {
        case 1: 
            typeStr = 'status'
            data[typeStr] = value
            break;
        case 2: 
            typeStr = 'status_chat'
            data[typeStr] = value
            break;
        default:
            call.succ({result: 1})
            break;
    }
    let res = await G.mongodb.collection('user').updateOne({uid}, {$set: data});
    setGud(uid, data);
    let result = res.modifiedCount > 0 ? 0 : 1;
    //3. 返回结果
    if(result == 0) {
        if(type == 1 && value == 1) await G.server.sendMsgByUid(uid, 'msg_s2c/OtherLogin', '1');
    }
    //3. 广播踢线
    return call.succ({result})
}