#!/usr/bin/perl

#use utf8;
use strict;
use CGI;
use JSON;
use Opals::Context; 
my $dbh = Opals::Context->dbh();
END { $dbh->disconnect(); }
my $status=0;
if ($ENV{'REQUEST_METHOD'} eq "POST") {
    my $json ="";
    while (<STDIN>) {
        $json .= $_;
    }
    my $request = decode_json($json);
    $status =delUserType($dbh,$request->{'catid'});
    
}

print "Content-type: text/plain\n\n";
print  "{\"status\":\"$status\"}";
#===================================================================================================
sub delUserType{
    my ($dbh,$catid)=@_;
    my ($count) =$dbh->selectrow_array("select count(*) from opl_user where categorycode=$catid && uid<>1");
    if($count==0){
        $dbh->do("delete from opl_category where catid=$catid ");
    }
   return ($count==0)?1:0;
}

