package main

import (
	"database/sql"
	"fmt"
	"log"
	"net/http"
	"os"
	"time"

	"ocbo-esign-backend/connections"
	"ocbo-esign-backend/middleware"

	"github.com/gin-contrib/cors"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"github.com/joho/godotenv"
)

func main() {
	err := godotenv.Load()
	if err != nil {
		log.Fatal("Error loading .env file")
	}

	connect()
}

func getCORSConfig(env string) cors.Config {
	switch env {
	case "dev":
		return cors.Config{
			AllowOrigins:     []string{"http://localhost:5173"},
			AllowMethods:     []string{"GET", "POST", "OPTIONS", "DELETE"},
			AllowHeaders:     []string{"Origin", "OCBO-Token", "Content-Length", "Content-Type", "X-Server"},
			ExposeHeaders:    []string{"Content-Length"},
			AllowCredentials: true,
		}
	case "prod":
		return cors.Config{
			AllowOrigins: []string{"https://ocboapps.davaocity.gov.ph", "https://esign.patalcala.com"},
			AllowMethods: []string{"GET", "POST", "OPTIONS", "DELETE"},
			// AllowHeaders:     []string{"Origin", "OCBO-Token", "Content-Length", "Content-Type", "X-Server", "Authorization"},
			AllowHeaders:     []string{"*"},
			ExposeHeaders:    []string{"Content-Length"},
			AllowCredentials: true,
		}
	default:
		return cors.DefaultConfig()
	}
}

func getConnectionStrings(env string) (string, string, error) {
	switch env {
	case "dev":
		return connections.GetConnectionString(), connections.GetConnectionStringPops(), nil
	case "prod":
		return connections.GetConnectionStringServer(), connections.GetConnectionStringPopsServer(), nil
	default:
		return "", "", fmt.Errorf("unknown environment: %s", env)
	}
}

func connect() {
	env := os.Getenv("ENVIRONMENT")

	conn, connPops, err := getConnectionStrings(env)
	if err != nil {
		panic(err)
	}

	db, err := sql.Open("mysql", conn)
	if err != nil {
		panic(err)
	}
	dbpop, err := sql.Open("mysql", connPops)
	if err != nil {
		panic(err)
	}

	defer db.Close()
	defer dbpop.Close()

	fmt.Println(env)

	router := gin.Default()
	router.Use(cors.New(getCORSConfig(env)))
	// router.Use(cors.Default())

	router.StaticFile("/", "static/index.html")
	router.StaticFile("/esign.webp", "static/esign.webp")
	router.StaticFile("/favicon.ico", "static/favicon.ico")

	router.GET("/api/:method", func(c *gin.Context) {
		var result string
		method := c.Param("method")

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		switch method {
		case "test":
			err = db.QueryRow("SELECT IFNULL(employeename, '') FROM employee WHERE uname = ?", "TEST").Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.String(http.StatusOK, "Connection is OK")

		case "check-connection":
			err = db.QueryRow("SELECT 1 AS result").Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadGateway, err)
				c.JSON(http.StatusBadGateway, gin.H{"result": false})
				return
			}
			c.JSON(http.StatusOK, gin.H{"result": true})

		case "get-listopapproval-building":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT IFNULL(o.controlNo, '') AS result, IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName) ) AS result2
                                FROM occupancy o JOIN customer c ON o.customerid = c.customerid JOIN ref_occupancy_type ot ON o.ref_occupancy_typeid = ot.ref_occupancy_typeid JOIN ref_occupancy ro ON ot.ref_occupancyid = ro.ref_occupancyid JOIN occupancydocflowtxn od ON o.occupancyid = od.occupancyreceivingid JOIN (SELECT occupancyreceivingid, MAX(occupancydocflowtxnid) AS latest_occupancydocflowtxnid FROM occupancydocflowtxn GROUP BY occupancyreceivingid) latest_doc ON od.occupancyreceivingid = latest_doc.occupancyreceivingid  AND od.occupancydocflowtxnid = latest_doc.latest_occupancydocflowtxnid
                                WHERE (remarks = "FOR OCCUPANCY RECOMMENDING APPROVAL" OR remarks = "FOR ADDITIONAL ORDER OF PAYMENT RECOMMENDING APPROVAL") AND od.is_approve = 0 ORDER BY od.txndate DESC`)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-listopapproval-occupancy":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT IFNULL(o.controlNo, '') AS result, IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName) ) AS result2
                                FROM occupancy o JOIN customer c ON o.customerid = c.customerid JOIN occupancydocflowtxn od ON o.occupancyid = od.occupancyreceivingid JOIN (SELECT occupancyreceivingid, MAX(occupancydocflowtxnid) AS latest_occupancydocflowtxnid FROM occupancydocflowtxn GROUP BY occupancyreceivingid) latest_doc ON od.occupancyreceivingid = latest_doc.occupancyreceivingid AND od.occupancydocflowtxnid = latest_doc.latest_occupancydocflowtxnid
                                WHERE (remarks = "FOR OCCUPANCY RECOMMENDING APPROVAL" OR remarks = "FOR ADDITIONAL ORDER OF PAYMENT RECOMMENDING APPROVAL") AND od.is_approve = 0 ORDER BY od.txndate DESC`)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-listopapproval-electrical":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT IFNULL(e.electricalno, '') AS result, IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName) ) AS result2 
                                FROM iips.electrical e JOIN iips.customer c ON e.customerid = c.customerid JOIN iips.ref_elec_occupancy ec ON e.ref_elec_occupancyid = ec.ref_elec_occupancyid JOIN iips.electricaldocflowtxn ed ON e.electricalid = ed.electricalid JOIN (SELECT electricalid, MAX(electricaldocflowtxnid) AS latest_electricaldocflowtxnid FROM electricaldocflowtxn GROUP BY electricalid) latest_doc ON ed.electricalid = latest_doc.electricalid AND ed.electricaldocflowtxnid = latest_doc.latest_electricaldocflowtxnid 
                                WHERE remarks = ? AND is_approve = 0 ORDER BY ed.txndate DESC`, "FOR ELECTRICAL ORDER OF PAYMENT APPROVAL")
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-listopprinting-building":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT IFNULL(r.applicationNo, '') AS result, IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName) ) AS result2
                                FROM receiving r JOIN customer c ON r.customerid = c.customerid JOIN ref_occupancy_type rot ON r.ref_occupancy_typeid = rot.ref_occupancy_typeid JOIN ref_occupancy ro ON rot.ref_occupancyid = ro.ref_occupancyid JOIN docflowtxn d ON r.receivingid = d.receivingid
                                WHERE d.remarks = "FOR ORDER OF PAYMENT PRINT" AND d.is_approve = 0 ORDER BY r.applicationNo ASC LIMIT 10`)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-listopprinting-occupancy":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT IFNULL(o.controlNo, '') AS result, IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName) ) AS result2
                                FROM occupancy o JOIN customer c ON o.customerid = c.customerid JOIN ref_occupancy_type rot ON o.ref_occupancy_typeid = rot.ref_occupancy_typeid JOIN ref_occupancy ro ON rot.ref_occupancyid = ro.ref_occupancyid JOIN occupancydocflowtxn od ON o.occupancyid = od.occupancyreceivingid JOIN (SELECT occupancyreceivingid, MAX(occupancydocflowtxnid) AS latest_occupancydocflowtxnid FROM occupancydocflowtxn GROUP BY occupancyreceivingid) latest_doc ON od.occupancyreceivingid = latest_doc.occupancyreceivingid  AND od.occupancydocflowtxnid = latest_doc.latest_occupancydocflowtxnid
                                WHERE  remarks = ? AND is_approve = 0 ORDER BY o.controlNo ASC`, "APPROVED FOR PRINTING OF BUREAU OF FIRE AND ORDER OF PAYMENT")
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-listopreprinting-occupancy":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT DISTINCT IFNULL(e.electricalno, '') AS result, IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName) ) AS result2
                                FROM electrical e JOIN customer c ON e.customerid = c.customerid JOIN ref_elec_occupancy ec ON e.ref_elec_occupancyid = ec.ref_elec_occupancyid JOIN electricaldocflowtxn ed ON e.electricalid = ed.electricalid LEFT JOIN esign_transactions et ON o.controlNo = et.referenceNo JOIN (SELECT electricalid, MAX(electricaldocflowtxnid) AS latest_electricaldocflowtxnid FROM electricaldocflowtxn GROUP BY electricalid) latest_doc ON ed.electricalid = latest_doc.electricalid  AND ed.electricaldocflowtxnid = latest_doc.latest_electricaldocflowtxnid
                                WHERE remarks = ? AND is_approve = 0 ORDER BY e.electricalno ASC`, "FOR ELECTRICAL OFFICIAL RECEIPT VALIDATION")
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-listopprinting-electrical":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT IFNULL(e.electricalno, '') AS result, IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName) ) AS result2 
                                FROM iips.electrical e JOIN iips.customer c ON e.customerid = c.customerid JOIN iips.ref_elec_occupancy ec ON e.ref_elec_occupancyid = ec.ref_elec_occupancyid JOIN iips.electricaldocflowtxn ed ON e.electricalid = ed.electricalid LEFT JOIN esign_transactions et ON e.electricalNo = et.referenceNo JOIN (SELECT electricalid, MAX(electricaldocflowtxnid) AS latest_electricaldocflowtxnid FROM electricaldocflowtxn GROUP BY electricalid) latest_doc ON ed.electricalid = latest_doc.electricalid AND ed.electricaldocflowtxnid = latest_doc.latest_electricaldocflowtxnid 
                                WHERE remarks = ? AND is_approve = 0 ORDER BY e.electricalno ASC`, "FOR ELECTRICAL ORDER OF PAYMENT PRINTING")
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-listopreprinting-electrical":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT DISTINCT IFNULL(e.electricalno, '') AS result, IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName) ) AS result2
                                FROM electrical e JOIN customer c ON e.customerid = c.customerid JOIN ref_elec_occupancy ec ON e.ref_elec_occupancyid = ec.ref_elec_occupancyid JOIN electricaldocflowtxn ed ON e.electricalid = ed.electricalid LEFT JOIN esign_transactions et ON e.electricalNo = et.referenceNo JOIN (SELECT electricalid, MAX(electricaldocflowtxnid) AS latest_electricaldocflowtxnid FROM electricaldocflowtxn GROUP BY electricalid) latest_doc ON ed.electricalid = latest_doc.electricalid AND ed.electricaldocflowtxnid = latest_doc.latest_electricaldocflowtxnid
                                WHERE remarks = ? AND is_approve = 0 ORDER BY e.electricalno ASC`, "FOR ELECTRICAL OFFICIAL RECEIPT VALIDATION")
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-list-assessors":
			array := []string{}

			results, err := db.Query("SELECT IFNULL(employeename, '') AS result FROM employee WHERE is_assessment = ? AND is_delete = ? AND NOT (employeename LIKE ? OR employeename LIKE ? OR employeename LIKE ?) AND employeeid NOT IN (?, ?, ?, ?, ?, ?, ?, ?, ?)", 1, 0, "%OFFICE%", "%TEST%", "%SAMPLE%", 55, 68, 120, 136, 103, 233, 235, 243, 310)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
			}
			c.JSON(http.StatusOK, gin.H{
				"result": array,
			})

		case "get-list-approvers":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query("SELECT IFNULL(employeeid, '') AS result, IFNULL(employeename, '') AS result2 FROM employee WHERE is_finalapprover = ? AND is_delete = ? AND NOT (employeename LIKE ? OR employeename LIKE ? OR employeename LIKE ? OR employeename LIKE ? OR employeename LIKE ?) AND employeeid NOT IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", 1, 0, "%OFFICE%", "%TEST%", "%SAMPLE%", "%BUILDING%", "%OCCUPANCY%", 124, 141, 178, 14, 77, 82, 83, 129, 137, 144, 169, 233, 247, 267, 282)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-list-registered":
			headId := os.Getenv("HEADID")
			array := []string{}

			results, err := db.Query("SELECT IFNULL(emp.employeename, '') AS result FROM esign e LEFT JOIN employee emp ON e.employeeid = emp.employeeid WHERE e.employeeid <> ?", headId)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
			}
			c.JSON(http.StatusOK, gin.H{
				"result": array,
			})

		case "get-headname":
			err = db.QueryRow("SELECT IFNULL(employeename, '') AS result FROM employee WHERE is_head = 1").Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-headid":
			err = db.QueryRow("SELECT IFNULL(employeeid, '') AS result FROM employee WHERE is_head = 1").Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-datetime":
			now := time.Now()
			formattedDateTime := now.Format(time.RFC3339)

			c.JSON(http.StatusOK, gin.H{
				"result": formattedDateTime,
			})

		}
	})

	router.GET("/api/:method/:data/fetch-data", func(c *gin.Context) {
		var result string
		method := c.Param("method")
		data := c.Param("data")

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		switch method {
		case "check-building":
			err = db.QueryRow("SELECT COUNT(receivingid) AS result FROM receiving WHERE applicationNo = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-occupancy":
			err = db.QueryRow("SELECT COUNT(occupancyid) AS result FROM occupancy WHERE controlNo = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-signage":
			err = db.QueryRow("SELECT COUNT(signageid) AS result FROM signage WHERE signApplicationNo = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-electrical":
			err = db.QueryRow("SELECT COUNT(electricalid) AS result FROM electrical WHERE electricalNo = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-mechanical":
			err = db.QueryRow("SELECT COUNT(mechanicalid) AS result FROM mechanical WHERE mechApplicationNo = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-owner-building":
			var result2, result3, result4, result5, result6 string

			err = db.QueryRow("SELECT IFNULL(c.firstName, '') AS result, IFNULL(c.middleInitial, '') AS result2, IFNULL(c.lastName, '') AS result3, IFNULL(rp.block, '') AS result4, IFNULL(rp.lot, '') AS result5, IFNULL(rp.address, '') AS result6 FROM customer c, receiving r, receiving_permitnoaddress rp WHERE r.customerid = c.customerid AND r.receivingid = rp.receivingid AND r.applicationNo = ?", data).Scan(&result, &result2, &result3, &result4, &result5, &result6)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  result,
				"result2": result2,
				"result3": result3,
				"result4": result4,
				"result5": result5,
				"result6": result6,
			})

		case "get-owner-occupancy":
			var result2, result3, result4 string

			err = db.QueryRow("SELECT IFNULL(c.firstName, '') AS result, IFNULL(c.middleInitial, '') AS result2, IFNULL(c.lastName, '') AS result3, IFNULL(c.address, '') AS result4 FROM customer c, receiving r, occupancy o WHERE r.customerid = c.customerid AND r.applicationNo = o.bldgApplicationNo AND o.controlNo = ?", data).Scan(&result, &result2, &result3, &result4)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  result,
				"result2": result2,
				"result3": result3,
				"result4": result4,
			})

		case "get-owner-electrical":
			var result2, result3, result4 string

			err = db.QueryRow("SELECT IFNULL(c.firstName, '') AS result, IFNULL(c.middleInitial, '') AS result2, IFNULL(c.lastName, '') AS result3, IFNULL(c.address, '') AS result4 FROM customer c, electrical e WHERE e.customerid = c.customerid AND e.electricalNo = ?", data).Scan(&result, &result2, &result3, &result4)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  result,
				"result2": result2,
				"result3": result3,
				"result4": result4,
			})

		case "get-status-building":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT IFNULL(DATE_FORMAT(b.datetransac, '%M %d, %Y'), '') AS result, IFNULL(REPLACE(REPLACE(b.remarks, 'RECEIVING', 'RECEIVED'), 'PERMIT ALREADY RELEASE', 'PERMIT RELEASED'), '')  AS result2 
									FROM docflowtxn b, receiving r WHERE r.receivingid = b.receivingid AND r.applicationNo = ? ORDER BY b.docflowtxnid DESC`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-status-occupancy":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT IFNULL(DATE_FORMAT(b.txndate, '%M %d, %Y'), '') AS result, IFNULL(REPLACE(b.remarks, 'RECEIVE', 'RECEIVED'), '') AS result2
									              FROM occupancydocflowtxn b, occupancy o WHERE o.occupancyid = b.occupancyreceivingid AND o.controlNo = ? ORDER BY b.occupancydocflowtxnid DESC`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-status-electrical":
			var result2 string

			array := []string{}
			array2 := []string{}

			results, err := db.Query(`SELECT IFNULL(DATE_FORMAT(b.txndate, '%M %d, %Y'), '') AS result, IFNULL(b.remarks, '') AS result2
			FROM electricaldocflowtxn b, electrical e WHERE e.electricalid = b.electricalid AND e.electricalNo = ? ORDER BY b.electricaldocflowtxnid DESC`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
			})

		case "get-list-clients":
			var result2, result3, result4 string

			array := []string{}
			array2 := []string{}
			array3 := []string{}
			array4 := []string{}

			results, err := db.Query(`SELECT IFNULL(IF(firstName = '', lastName, CONCAT(firstName, ' ', IF(middleInitial = '', lastName, CONCAT(middleInitial, '. ', lastName)))), "") AS result, IFNULL(address, '') AS result2, IFNULL(lastName, "") AS result3, IFNULL(firstName, "") AS result4 FROM customer WHERE (lastName LIKE ? OR firstName LIKE ?)`, "%"+data+"%", "%"+data+"%")
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3, &result4)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
				array4 = append(array4, result4)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
				"result3": array3,
				"result4": array4,
			})

		case "get-laststatus-building":
			err := db.QueryRow(`SELECT IFNULL(remarks, '') AS result FROM docflowtxn WHERE docflowtxnid = (SELECT MAX(docflowtxnid) FROM docflowtxn WHERE receivingid = ?)`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-laststatus-occupancy":
			err := db.QueryRow(`SELECT IFNULL(remarks, '') AS result FROM occupancydocflowtxn WHERE occupancydocflowtxnid = (SELECT MAX(occupancydocflowtxnid) FROM occupancydocflowtxn WHERE occupancyreceivingid = ?)`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-laststatus-electrical":
			err := db.QueryRow(`SELECT IFNULL(remarks, '') AS result FROM electricaldocflowtxn  WHERE electricaldocflowtxnid = (SELECT MAX(electricaldocflowtxnid) FROM electricaldocflowtxn WHERE electricalid = ?)`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-applicationbyid-building":
			err := db.QueryRow(`SELECT IFNULL(applicationNo, '') AS result FROM receiving WHERE receivingid = ?`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-applicationbyid-occupancy":
			err := db.QueryRow(`SELECT IFNULL(controlNo, '') AS result FROM occupancy WHERE occupancyid = ?`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-applicationbyid-electrical":
			err := db.QueryRow(`SELECT IFNULL(electricalNo, '') AS result FROM electrical WHERE electricalid = ?`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-idbyapplication-building":
			err := db.QueryRow(`SELECT IFNULL(receivingid, '') AS result FROM receiving WHERE applicationNo = ?`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-idbyapplication-occupancy":
			err := db.QueryRow(`SELECT IFNULL(occupancyid, 0) AS result FROM occupancy WHERE controlNo = ?`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-idbyapplication-electrical":
			err := db.QueryRow(`SELECT IFNULL(electricalid, 0) AS result FROM electrical WHERE electricalNo = ?`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-employeeid":
			err := db.QueryRow("SELECT IFNULL(employeeid, 0) AS result FROM employee WHERE employeename = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-registered":
			err := db.QueryRow("SELECT IFNULL(e.esignid, 0) AS result FROM esign e LEFT JOIN employee emp ON e.employeeid = emp.employeeid WHERE emp.employeename = ?", data).Scan(&result)
			if err != nil {
				if err == sql.ErrNoRows {
					result = "0"
				} else {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}

			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-password":
			err := db.QueryRow("SELECT IFNULL(password, '') AS result FROM esign WHERE employeeid = ?", data).Scan(&result)
			if err != nil {
				if err == sql.ErrNoRows {
					result = "0"
				} else {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}

			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-employeename":
			err := db.QueryRow("SELECT IFNULL(employeename, '') AS result FROM employee WHERE employeeid = ?", data).Scan(&result)
			if err != nil {
				if err == sql.ErrNoRows {
					result = "0"
				} else {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-opdetails-occupancy":
			var result2, result3, result4, result5, result6 string
			array := []string{}
			array2 := []string{}
			array3 := []string{}
			array4 := []string{}
			array5 := []string{}
			array6 := []string{}

			results, err := db.Query(`SELECT IFNULL(r.locationofconstruction, '') AS result, IFNULL(CONCAT(ro.occupancy, ' - ', rot.occupancyType), '') AS result2, IFNULL(em.employeename, '') AS result3, IFNULL(o.amount, '') AS result4, IFNULL(o.assessedDate, '') AS result5, IFNULL(oc.occupancyid, 0) AS result6
                                FROM occupancy oc LEFT JOIN receiving r ON oc.bldgApplicationNo = r.applicationNo JOIN ref_occupancy_type rot ON oc.ref_occupancy_typeid = rot.ref_occupancy_typeid JOIN ref_occupancy ro ON rot.ref_occupancyid  = ro.ref_occupancyid JOIN occupancy_orderofpayment o ON oc.occupancyid = o.occupancyid JOIN employee em ON o.assessedbyid = em.employeeid
                                WHERE oc.controlNo = ?`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3, &result4, &result5, &result6)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
				array4 = append(array4, result4)
				array5 = append(array5, result5)
				array6 = append(array6, result6)

			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,  //applicationNo
				"result2": array2, //noOfPermits
				"result3": array3, //firstName
				"result4": array4, //middleName
				"result5": array5, //lastName
				"result6": array6, //occFirstName
			})

		case "get-opdetails-electrical":
			var result2, result3, result4, result5, result6 string
			array := []string{}
			array2 := []string{}
			array3 := []string{}
			array4 := []string{}
			array5 := []string{}
			array6 := []string{}

			// results, err := db.Query(`SELECT IFNULL(e.electricalNo, '') AS result, IFNULL(c.firstName, '') AS result2, IFNULL(c.middleInitial, '') AS result3, IFNULL(c.lastName, '') AS result4, IFNULL(e.locationofinstallation, '') AS result5, IFNULL(re.occupancyoruse, '') AS result6, IFNULL(em.employeename, '') AS result7, IFNULL(rb.accountdescription, '') AS result8, IFNULL(o.amount, '') AS result9, IFNULL(o.opDate, '') AS result10, IFNULL(e.electricalid, 0) AS result11, IFNULL(o.assessedbyid, 0) AS result12
			//                           FROM electrical e join customer c on e.customerid = c.customerid join electrical_orderofpayment_new o on e.electricalid = o.electricalid join ref_elec_occupancy re on e.ref_elec_occupancyid = re.ref_elec_occupancyid join ref_bldgcomputationsheet rb on o.ref_bldgcomputationsheetid = rb.ref_bldgcomputationsheetid join employee em on o.assessedbyid = em.employeeid
			//                           WHERE e.electricalNo = ?`, data)
			results, err := db.Query(`SELECT IFNULL(e.locationofinstallation, '') AS result, IFNULL(re.occupancyoruse, '') AS result2, IFNULL(em.employeename, '') AS result3, IFNULL(o.amount, '') AS result4, IFNULL(o.opDate, '') AS result5, IFNULL(e.electricalid, 0) AS result6
                                FROM electrical e JOIN electrical_orderofpayment_new o ON e.electricalid = o.electricalid JOIN ref_elec_occupancy re ON e.ref_elec_occupancyid = re.ref_elec_occupancyid JOIN employee em ON o.assessedbyid = em.employeeid
                                WHERE e.electricalNo = ?`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3, &result4, &result5, &result6)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
				array4 = append(array4, result4)
				array5 = append(array5, result5)
				array6 = append(array6, result6)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
				"result3": array3,
				"result4": array4,
				"result5": array5,
				"result6": array6,
			})

		case "get-paymentname":
			err := db.QueryRow("SELECT IFNULL(accountdescription, '') AS result FROM ref_bldgcomputationsheet WHERE ref_bldgcomputationsheetid = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-printdetails-occupancy":
			var result2, result3, result4, result5, result6, result7, result8, result9, result10 string

			err := db.QueryRow(`SELECT DISTINCT IFNULL(o.controlNo, '') AS result, IFNULL(op.opDate, '') AS result2, IFNULL(r.locationofconstruction, '') AS result3, IFNULL(ro.occupancy, '') AS result4, IFNULL(rot.occupancyType, '') AS result5, IFNULL(o.noOfPermitsApplied, '') AS result6, IFNULL(e.employeename, '') AS result7, IF(cu.firstName IS NULL OR cu.firstName = '', cu.lastName, CONCAT(cu.firstName, ' ', IF(cu.middleInitial IS NULL OR cu.middleInitial = '', '', CONCAT(cu.middleInitial, '. ')), cu.lastName) ) AS result8, IFNULL(op.numUnits, 0) AS result9, IFNULL(o.totalFloorArea, 0) AS result10
                          FROM occupancy o LEFT JOIN receiving r on o.bldgApplicationNo = r.applicationNo JOIN occupancy_orderofpayment op ON o.occupancyid = op.occupancyid JOIN ref_occupancy_type rot ON o.ref_occupancy_typeid = rot.ref_occupancy_typeid JOIN ref_occupancy ro ON rot.ref_occupancyid = ro.ref_occupancyid JOIN customer cu ON o.customerid = cu.customerid JOIN employee e ON op.assessedbyid = e.employeeid
                          WHERE op.is_approve = 1 AND op.for_approval = 1 AND op.is_release = 0 AND is_paid = 0 AND popstransmitted = 0 AND o.occupancyid = ?`, data).Scan(&result, &result2, &result3, &result4, &result5, &result6, &result7, &result8, &result9, &result10)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result":   result,
				"result2":  result2,
				"result3":  result3,
				"result4":  result4,
				"result5":  result5,
				"result6":  result6,
				"result7":  result7,
				"result8":  result8,
				"result9":  result9,
				"result10": result10,
			})

		case "get-printdetails-electrical":
			var result2, result3, result4, result5, result6, result7, result8 string

			err := db.QueryRow(`SELECT DISTINCT IFNULL(el.electricalNo, '') AS result, IFNULL(op.opDate, '') AS result2, IFNULL(el.locationofinstallation, '') AS result3, IFNULL(occ.occupancy, '') AS result4, IFNULL(u.occupancyoruse, '') AS result5, IFNULL(el.noofUnits, '') AS result6, IFNULL(e.employeename, '') AS result7, IF(cu.firstName IS NULL OR cu.firstName = '', cu.lastName, CONCAT(cu.firstName, ' ', IF(cu.middleInitial IS NULL OR cu.middleInitial = '', '', CONCAT(cu.middleInitial, '. ')), cu.lastName) ) AS result8
                          FROM electrical el JOIN customer cu ON el.customerid = cu.customerid JOIN electrical_orderofpayment_new op ON el.electricalid = op.electricalid JOIN ref_elec_occupancy u ON el.ref_elec_occupancyid = u.ref_elec_occupancyid LEFT JOIN ref_occupancy occ ON u.ref_occupancyid = occ.ref_occupancyid JOIN employee e ON op.assessedbyid = e.employeeid
                          WHERE op.is_approve = 1 AND op.for_approval = 1 AND op.is_release = 0 AND is_paid = 0 AND popstransmitted = 0 AND el.electricalid = ?`, data).Scan(&result, &result2, &result3, &result4, &result5, &result6, &result7, &result8)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  result,
				"result2": result2,
				"result3": result3,
				"result4": result4,
				"result5": result5,
				"result6": result6,
				"result7": result7,
				"result8": result8,
			})

		case "get-reprintdetails-electrical":
			var result2, result3, result4, result5, result6, result7, result8 string

			err := db.QueryRow(`SELECT DISTINCT IFNULL(el.electricalNo, '') AS result, IFNULL(op.opDate, '') AS result2, IFNULL(el.locationofinstallation, '') AS result3, IFNULL(occ.occupancy, '') AS result4, IFNULL(u.occupancyoruse, '') AS result5, IFNULL(el.noofUnits, '') AS result6, IFNULL(e.employeename, '') AS result7, IF(cu.firstName IS NULL OR cu.firstName = '', cu.lastName, CONCAT(cu.firstName, ' ', IF(cu.middleInitial IS NULL OR cu.middleInitial = '', '', CONCAT(cu.middleInitial, '. ')), cu.lastName) ) AS result8
                          FROM electrical el JOIN customer cu ON el.customerid = cu.customerid JOIN electrical_orderofpayment_new op ON el.electricalid = op.electricalid JOIN ref_elec_occupancy u ON el.ref_elec_occupancyid = u.ref_elec_occupancyid LEFT JOIN ref_occupancy occ ON u.ref_occupancyid = occ.ref_occupancyid JOIN employee e ON op.assessedbyid = e.employeeid
                          WHERE op.is_approve = 1 AND op.for_approval = 1 AND is_paid = 0 AND el.electricalid = ?`, data).Scan(&result, &result2, &result3, &result4, &result5, &result6, &result7, &result8)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  result,
				"result2": result2,
				"result3": result3,
				"result4": result4,
				"result5": result5,
				"result6": result6,
				"result7": result7,
				"result8": result8,
			})

		case "get-printdetailsfees-occupancy":
			var result2, result3 string
			array := []string{}
			array2 := []string{}
			array3 := []string{}

			results, err := db.Query(`SELECT IFNULL(ref.accountdescription, '') AS result, IFNULL(ref.accountcode, '') AS result2, IFNULL(op.amount, '') AS result3
                                FROM occupancy o JOIN occupancy_orderofpayment op ON  o.occupancyid = op.occupancyid JOIN ref_bldgcomputationsheet ref ON op.ref_bldgcomputationsheetid = ref.ref_bldgcomputationsheetid
                                WHERE op.is_approve = 1 AND op.for_approval = 1 AND op.is_release = 0 AND op.is_paid = 0 AND op.popstransmitted = 0 AND op.is_delete <> 1 AND op.occupancyid = ?`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
				"result3": array3,
			})

		case "get-printdetailsfees-bldgadditional":
			var result2, result3, result4 string
			array := []string{}
			array2 := []string{}
			array3 := []string{}
			array4 := []string{}

			results, err := db.Query(`SELECT IFNULL(ref.accountdescription, '') AS result, IFNULL(ref.accountcode, '') AS result2, IFNULL(op.amount, '') AS result3, IFNULL(op.numUnits, 0) AS result4
                                FROM occupancy o JOIN building_orderofpayment op ON  o.occupancyid = op.occupancyid JOIN ref_bldgcomputationsheet ref ON op.ref_bldgcomputationsheetid = ref.ref_bldgcomputationsheetid
                                WHERE op.is_approve = 1 AND op.for_approval = 1 AND op.is_paid = 0 AND op.popstransmitted = 0 AND op.is_delete <> 1 AND op.occupancyid = ?`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3, &result4)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
				array4 = append(array4, result4)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
				"result3": array3,
				"result4": array4,
			})

		case "get-printdetailsfees-electrical":
			var result2, result3 string
			array := []string{}
			array2 := []string{}
			array3 := []string{}

			results, err := db.Query(`SELECT IFNULL(ref.accountdescription, '') AS result, IFNULL(ref.accountcode, '') AS result2, IFNULL(op.amount, '') AS result3
                                FROM electrical el JOIN electrical_orderofpayment_new op ON  el.electricalid = op.electricalid JOIN ref_bldgcomputationsheet ref ON op.ref_bldgcomputationsheetid = ref.ref_bldgcomputationsheetid
                                WHERE op.is_approve = 1 AND op.for_approval = 1 AND op.is_release = 0 AND op.is_paid = 0 AND op.popstransmitted = 0 AND op.is_delete <> 1 AND op.electricalid = ?`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
				"result3": array3,
			})

		case "get-reprintdetailsfees-electrical":
			var result2, result3 string
			array := []string{}
			array2 := []string{}
			array3 := []string{}

			results, err := db.Query(`SELECT IFNULL(ref.accountdescription, '') AS result, IFNULL(ref.accountcode, '') AS result2, IFNULL(op.amount, '') AS result3
                                FROM electrical el JOIN electrical_orderofpayment_new op ON  el.electricalid = op.electricalid JOIN ref_bldgcomputationsheet ref ON op.ref_bldgcomputationsheetid = ref.ref_bldgcomputationsheetid
                                WHERE op.is_approve = 1 AND op.for_approval = 1 AND op.is_paid = 0 AND op.is_delete <> 1 AND op.electricalid = ?`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
				"result3": array3,
			})

		case "get-signatureimage":
			err := db.QueryRow("SELECT IFNULL(image, '') AS result FROM esign WHERE employeeid = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-esignid":
			err := db.QueryRow("SELECT IFNULL(esignid, 0) AS result FROM esign WHERE employeeid = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-signeddateassessed-occupancy":
			err := db.QueryRow(`SELECT IFNULL(txndate, '') AS result FROM occupancydocflowtxn WHERE occupancyreceivingid = ? AND remarks = "FOR OCCUPANCY RECOMMENDING APPROVAL"`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-signeddateassessed-electrical":
			err := db.QueryRow(`SELECT IFNULL(txndate, '') AS result FROM electricaldocflowtxn WHERE electricalid = ? AND remarks = "FOR ELECTRICAL ORDER OF PAYMENT APPROVAL"`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-popsdetails-occupancy":
			var result2, result3, result4, result5, result6, result7, result8, result9 string
			array := []string{}
			array2 := []string{}
			array3 := []string{}
			array4 := []string{}
			array5 := []string{}
			array6 := []string{}
			array7 := []string{}
			array8 := []string{}
			array9 := []string{}

			results, err := db.Query(`SELECT IFNULL(o.controlNo, '') AS result, IFNULL(c.customerid, 0) AS result2, IFNULL(IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName)), '') AS result3,
                                IFNULL(r.locationofconstruction, '') AS result4, IFNULL(op.amount, '') AS result5, IFNULL(op.amt_Gflgu, '') AS result6, IFNULL(op.amt_Gfdpwh, '') AS result7, IFNULL(op.amt_Tfbo, '') AS result8, IFNULL(ref.accountcode, '') AS result9
                                FROM occupancy o JOIN receiving r ON o.bldgApplicationNo = r.applicationNo JOIN customer c ON r.customerid = c.customerid JOIN occupancy_orderofpayment op ON o.occupancyid = op.occupancyid JOIN ref_bldgcomputationsheet ref ON op.ref_bldgcomputationsheetid = ref.ref_bldgcomputationsheetid
                                WHERE o.occupancyid = (SELECT occupancyid FROM occupancy WHERE controlNo = ?)`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3, &result4, &result5, &result6, &result7, &result8, &result9)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
				array4 = append(array4, result4)
				array5 = append(array5, result5)
				array6 = append(array6, result6)
				array7 = append(array7, result7)
				array8 = append(array8, result8)
				array9 = append(array9, result9)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
				"result3": array3,
				"result4": array4,
				"result5": array5,
				"result6": array6,
				"result7": array7,
				"result8": array8,
				"result9": array9,
			})

		case "get-popsdetails-electrical":
			var result2, result3, result4, result5, result6, result7, result8, result9 string
			array := []string{}
			array2 := []string{}
			array3 := []string{}
			array4 := []string{}
			array5 := []string{}
			array6 := []string{}
			array7 := []string{}
			array8 := []string{}
			array9 := []string{}

			results, err := db.Query(`SELECT IFNULL(e.electricalNo, '') AS result, IFNULL(c.customerid, 0) AS result2, IFNULL(IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName)), '') AS result3,
                                IFNULL(e.locationofinstallation, '') AS result4, IFNULL(op.amount, '') AS result5, IFNULL(op.amt_Gflgu, '') AS result6, IFNULL(op.amt_Gfdpwh, '') AS result7, IFNULL(op.amt_Tfbo, '') AS result8, IFNULL(ref.accountcode, '') AS result9
                                FROM electrical e JOIN customer c ON e.customerid = c.customerid JOIN electrical_orderofpayment_new op ON e.electricalid = op.electricalid JOIN ref_bldgcomputationsheet ref ON op.ref_bldgcomputationsheetid = ref.ref_bldgcomputationsheetid
                                WHERE e.electricalid = (SELECT electricalid FROM electrical WHERE electricalNo = ?)`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3, &result4, &result5, &result6, &result7, &result8, &result9)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
				array4 = append(array4, result4)
				array5 = append(array5, result5)
				array6 = append(array6, result6)
				array7 = append(array7, result7)
				array8 = append(array8, result8)
				array9 = append(array9, result9)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
				"result3": array3,
				"result4": array4,
				"result5": array5,
				"result6": array6,
				"result7": array7,
				"result8": array8,
				"result9": array9,
			})

		case "check-bldgadditional-approval":
			err := db.QueryRow("SELECT COUNT(building_orderofpaymentid) AS result FROM building_orderofpayment WHERE occupancyid = (SELECT occupancyid FROM occupancy WHERE controlNo = ?) AND for_approval = 1 AND is_approve = 0 AND popstransmitted = 0 AND is_paid = 0 AND is_delete = 0", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-bldgadditional-printing":
			err := db.QueryRow("SELECT COUNT(building_orderofpaymentid) AS result FROM building_orderofpayment WHERE occupancyid = (SELECT occupancyid FROM occupancy WHERE controlNo = ?) AND for_approval = 1 AND is_approve = 1 AND popstransmitted = 0 AND is_paid = 0 AND is_delete = 0", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-bldgadditional-reprinting":
			err := db.QueryRow("SELECT COUNT(building_orderofpaymentid) AS result FROM building_orderofpayment WHERE occupancyid = (SELECT occupancyid FROM occupancy WHERE controlNo = ?) AND for_approval = 1 AND is_approve = 1 AND is_paid = 0 AND is_delete = 0", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-paid-pops":
			err := dbpop.QueryRow("SELECT IF(COUNT(AFNum) > 0, 1, 0) AS result FROM orderpaydetail WHERE OPRefId = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-qr":
			err := db.QueryRow("SELECT SUBSTR(signature, 36) AS result FROM esign WHERE employeeid = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-qr":
			var count int
			err := db.QueryRow("SELECT COUNT(esignid) AS result FROM esign WHERE SUBSTR(signature, 36) = ?", data).Scan(&count)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			if count > 0 {
				err := db.QueryRow("SELECT IFNULL(e.employeename, '') AS result FROM esign es JOIN employee e ON es.employeeid = e.employeeid WHERE SUBSTR(signature, 36) = ?", data).Scan(&result)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}

				c.JSON(http.StatusOK, gin.H{
					"result": result,
				})
			} else {
				c.JSON(http.StatusBadRequest, gin.H{
					"result": count,
				})
			}

		case "get-transactions":
			var result2, result3 string
			array := []string{}
			array2 := []string{}
			array3 := []string{}

			results, err := db.Query(`SELECT IFNULL(referenceNo, '') AS result, IFNULL(DATE_FORMAT(date_signed, '%M %e, %Y'), '') AS result2, IFNULL(DATE_FORMAT(date_signed, '%h:%i %p'), '') AS result3 FROM esign_transactions WHERE esignid = (SELECT esignid FROM esign WHERE employeeid = (SELECT employeeid FROM employee WHERE employeename = ?)) ORDER BY date_signed DESC LIMIT 300`, data)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
				"result3": array3,
			})

		case "get-transactions-count":
			err := db.QueryRow(`SELECT COUNT(esign_transactionsid) AS result FROM esign_transactions WHERE esignid = (SELECT esignid FROM esign WHERE employeeid = (SELECT employeeid FROM employee WHERE employeename = ?))`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-esignid-byname":
			err := db.QueryRow(`SELECT IFNULL(esignid, 0) AS result FROM esign WHERE employeeid = (SELECT employeeid FROM employee WHERE employeename = ?)`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-totalamount-electrical":
			err := db.QueryRow(`SELECT IFNULL(SUM(amount), 0) AS result FROM electrical_orderofpayment_new WHERE electricalid = (SELECT electricalid FROM electrical WHERE electricalNo = ?)`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-popsrecord":
			err := dbpop.QueryRow("SELECT COUNT(OrderPayId) AS result FROM orderpaydetail WHERE oprefid = ?", data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-opdata-electrical":
			err := db.QueryRow(`SELECT IFNULL(CONCAT(electricalid, '-', assessedbyid, '-', reviewedbyid, '-', SUM(ref_bldgcomputationsheetid), '-', SUM(amount)),'') AS result FROM electrical_orderofpayment_new WHERE electricalid = (SELECT electricalid FROM electrical WHERE electricalNo = ?)`, data).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		}
	})

	router.GET("/api/:method/:data/:data2/fetch-data", func(c *gin.Context) {
		var result string
		method := c.Param("method")
		data := c.Param("data")
		data2 := c.Param("data2")

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		switch method {
		case "check-access":
			err = db.QueryRow("SELECT COUNT(accessid) AS result FROM access a JOIN ref_access ra ON a.ref_accessid = ra.ref_accessid AND ra.access = ? and employeeid = ?", data, data2).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-signeddate":
			err := db.QueryRow("SELECT IFNULL(date_signed, '') AS result FROM esign_transactions WHERE esignid = ? AND referenceNo = ?", data, data2).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "check-approval":
			err := db.QueryRow("SELECT IFNULL(is_approve, 0) AS result FROM occupancydocflowtxn WHERE remarks = ? AND occupancyreceivingid = (SELECT occupancyid FROM occupancy WHERE controlNo = ?)", data, data2).Scan(&result)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result": result,
			})

		case "get-transactions-filter":
			var result2, result3 string
			array := []string{}
			array2 := []string{}
			array3 := []string{}

			results, err := db.Query("SELECT IFNULL(referenceNo, '') AS result, IFNULL(DATE_FORMAT(date_signed, '%M %e, %Y'), '') AS result2, IFNULL(DATE_FORMAT(date_signed, '%h:%i %p'), '') AS result3 FROM esign_transactions WHERE esignid = (SELECT esignid FROM esign WHERE employeeid = (SELECT employeeid FROM employee WHERE employeename = ?)) AND referenceNo LIKE ? ORDER BY date_signed DESC", data, "%"+data2+"%")
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			for results.Next() {
				err = results.Scan(&result, &result2, &result3)
				if err != nil {
					c.AbortWithError(http.StatusBadRequest, err)
					c.String(http.StatusBadRequest, err.Error())
					return
				}
				array = append(array, result)
				array2 = append(array2, result2)
				array3 = append(array3, result3)
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  array,
				"result2": array2,
				"result3": array3,
			})

		case "get-infoapproval-electrical":
			var result2, result3, result4 string

			err := db.QueryRow(`SELECT IF(c.firstName IS NULL OR c.firstName = '', c.lastName, CONCAT(c.firstName, ' ', IF(c.middleInitial IS NULL OR c.middleInitial = '', '', CONCAT(c.middleInitial, '. ')), c.lastName)) AS result, DATE_FORMAT(et.date_signed, '%M') AS result2, DATE_FORMAT(et.date_signed, '%e') AS result3, DATE_FORMAT(et.date_signed, '%Y') AS result4
                          FROM electrical e JOIN customer c ON e.customerid = c.customerid JOIN esign_transactions et ON e.electricalNo = et.referenceNo WHERE e.electricalNo = ? AND et.esignid = ?`, data, data2).Scan(&result, &result2, &result3, &result4)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  result,
				"result2": result2,
				"result3": result3,
				"result4": result4,
			})

		case "get-infoprinted-electrical":
			var result2, result3 string

			err := db.QueryRow(`SELECT DATE_FORMAT(et.date_signed, '%M') AS result, DATE_FORMAT(et.date_signed, '%e') AS result2, DATE_FORMAT(et.date_signed, '%Y') AS result3
                          FROM electrical e JOIN customer c ON e.customerid = c.customerid JOIN esign_transactions et ON e.electricalNo = et.referenceNo WHERE e.electricalNo = ? AND et.esignid <> ?`, data, data2).Scan(&result, &result2, &result3)
			if err != nil {
				c.AbortWithError(http.StatusBadRequest, err)
				c.String(http.StatusBadRequest, err.Error())
				return
			}
			c.JSON(http.StatusOK, gin.H{
				"result":  result,
				"result2": result2,
				"result3": result3,
			})
		}
	})

	router.POST("/api/post-registration", middleware.TokenChecker(), func(c *gin.Context) {
		type RegistrationData struct {
			Data  int    `json:"data"`
			Data2 string `json:"data2"`
			Data3 string `json:"data3"`
			Data4 string `json:"data4"`
		}
		var registrationData RegistrationData
		if err := c.ShouldBindJSON(&registrationData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("INSERT INTO esign (esignid, employeeid, password, signature, image) VALUES (NULL, ?, ?, ?, ?)")
		if err != nil {
			panic(err.Error())
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(registrationData.Data, registrationData.Data2, registrationData.Data3, registrationData.Data4)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Registrating e-Sign")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Registrating e-Sign")
		}

	})

	router.POST("/api/post-newstatus-occupancy", middleware.TokenChecker(), func(c *gin.Context) {
		type NewstatusData struct {
			Data  int    `json:"data"`  //occuapancyid
			Data2 string `json:"data2"` //date
			Data3 string `json:"data3"` //remarks
			Data4 string `json:"data4"` //is_tag
			Data5 string `json:"data5"` //tagword
			Data6 int    `json:"data6"` //is_aprrove
			Data7 int    `json:"data7"` //employeeid
		}
		var newstatusData NewstatusData
		if err := c.ShouldBindJSON(&newstatusData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare(`INSERT INTO occupancydocflowtxn (occupancydocflowtxnid, occupancyreceivingid, txndate, remarks, is_tag, tagword, is_approve, employeeid, is_compliance, comments)
                              VALUES (NULL, ?, ?, ?, ?, ?, ?, ?, 0, NULL)`)
		if err != nil {
			panic(err.Error())
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(newstatusData.Data, newstatusData.Data2, newstatusData.Data3, newstatusData.Data4, newstatusData.Data5, newstatusData.Data6, newstatusData.Data7)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Setting New Status")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Setting New Status")
		}

	})

	router.POST("/api/post-newstatus-electrical", middleware.TokenChecker(), func(c *gin.Context) {
		type NewstatusData struct {
			Data  int    `json:"data"`
			Data2 string `json:"data2"`
			Data3 string `json:"data3"`
			Data4 string `json:"data4"`
			Data5 string `json:"data5"`
			Data6 int    `json:"data6"`
			Data7 int    `json:"data7"`
		}
		var newstatusData NewstatusData
		if err := c.ShouldBindJSON(&newstatusData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare(`INSERT INTO electricaldocflowtxn (electricaldocflowtxnid, electricalid, txndate, remarks, comments, is_tag, tagword, is_approve, employeeid, is_delete) 
                              VALUES (NULL, ?, ?, ?, NULL, ?, ?, ?, ?, 0)`)
		if err != nil {
			panic(err.Error())
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(newstatusData.Data, newstatusData.Data2, newstatusData.Data3, newstatusData.Data4, newstatusData.Data5, newstatusData.Data6, newstatusData.Data7)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Setting New Status")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Setting New Status")
		}

	})

	router.POST("/api/update-docflow-occupancy", middleware.TokenChecker(), func(c *gin.Context) {
		type UpdateDocflowData struct {
			Data  string `json:"data"`
			Data2 string `json:"data2"`
		}
		var updateDocflowData UpdateDocflowData
		if err := c.ShouldBindJSON(&updateDocflowData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("UPDATE occupancydocflowtxn SET is_approve = 1 WHERE remarks = ? AND occupancyreceivingid = (SELECT occupancyid FROM occupancy WHERE controlNo = ?)")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(updateDocflowData.Data, updateDocflowData.Data2)
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Updating Docflow on Electrical")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Updating Docflow on Electrical")
		}
	})

	router.POST("/api/update-docflow-electrical", middleware.TokenChecker(), func(c *gin.Context) {
		type UpdateDocflowData struct {
			Data  string `json:"data"`
			Data2 string `json:"data2"`
		}
		var updateDocflowData UpdateDocflowData
		if err := c.ShouldBindJSON(&updateDocflowData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("UPDATE electricaldocflowtxn SET is_approve = 1 WHERE remarks = ? AND electricalid = (SELECT electricalid FROM electrical WHERE electricalNo = ?)")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(updateDocflowData.Data, updateDocflowData.Data2)
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Updating Docflow on Electrical")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Updating Docflow on Electrical")
		}
	})

	router.POST("/api/update-opprinted-occupancy", middleware.TokenChecker(), func(c *gin.Context) {
		type UpdateOpData struct {
			Data int `json:"data"`
		}
		var updateOpData UpdateOpData
		if err := c.ShouldBindJSON(&updateOpData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("UPDATE occupancy_orderofpayment SET popstransmitted = 1, is_release = 1 WHERE occupancyid = ? AND for_approval = 1 AND is_paid = 0 AND is_approve = 1")
		if err != nil {
			panic(err.Error())
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(updateOpData.Data)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Updating Order of Payment for Printing")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Updating Order of Payment for Printing")
		}
	})

	router.POST("/api/update-opprinted-electrical", middleware.TokenChecker(), func(c *gin.Context) {
		type UpdateOpData struct {
			Data int `json:"data"`
		}
		var updateOpData UpdateOpData
		if err := c.ShouldBindJSON(&updateOpData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("UPDATE electrical_orderofpayment_new SET popstransmitted = 1, is_release = 1 WHERE electricalid = ? AND for_approval = 1 AND is_paid = 0 AND is_approve = 1")
		if err != nil {
			panic(err.Error())
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(updateOpData.Data)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Updating Order of Payment for Printing")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Updating Order of Payment for Printing")
		}
	})

	router.POST("/api/update-foropreprint-electrical", middleware.TokenChecker(), func(c *gin.Context) {
		type UpdateOpData struct {
			Data int `json:"data"`
		}
		var updateOpData UpdateOpData
		if err := c.ShouldBindJSON(&updateOpData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("UPDATE electrical_orderofpayment_new SET popstransmitted = 1, is_release = 1 WHERE electricalid = ? AND for_approval = 1 AND is_paid = 0 AND is_approve = 1")
		if err != nil {
			panic(err.Error())
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(updateOpData.Data)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Updating Order of Payment for Printing")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Updating Order of Payment for Printing")
		}
	})

	router.POST("/api/post-esigntransaction", middleware.TokenChecker(), func(c *gin.Context) {
		type UpdateOpData struct {
			Data  int    `json:"data"`
			Data2 string `json:"data2"`
			Data3 string `json:"data3"`
		}
		var updateOpData UpdateOpData
		if err := c.ShouldBindJSON(&updateOpData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("INSERT INTO esign_transactions (esign_transactionsid, esignid, referenceNo, date_signed) VALUES (NULL, ?, ?, ?)")
		if err != nil {
			panic(err.Error())
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(updateOpData.Data, updateOpData.Data2, updateOpData.Data3)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Saving eSign transaction")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Saving eSign transaction")
		}
	})

	router.POST("/api/post-pops", middleware.TokenChecker(), func(c *gin.Context) {
		type PostPopData struct {
			Data   string `json:"data"`   //applicationNo
			Data2  string `json:"data2"`  //date
			Data3  string `json:"data3"`  //customerId
			Data4  string `json:"data4"`  //customerName
			Data5  string `json:"data5"`  //customerAddress
			Data6  string `json:"data6"`  //accountCode
			Data7  string `json:"data7"`  //amount
			Data8  string `json:"data8"`  //approverName
			Data9  string `json:"data9"`  //dateAndTime
			Data10 string `json:"data10"` //gflgu
			Data11 string `json:"data11"` //gfdpwh
			Data12 string `json:"data12"` //tfobo
			Data13 string `json:"data13"` //publicIp
		}
		var postPopDate PostPopData
		if err := c.ShouldBindJSON(&postPopDate); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := dbpop.Prepare(`INSERT INTO orderpaydetail (OrderPayId, OPRefId, OPSysId, OPDate, AcctRefId, AcctFullName, AcctAddress, AccountCode, AmountBasic, OPPostedBy, OPPostDate, OfficeCode, Amt_GFLGU, Amt_GFDPWH, Amt_TFBO, TranRefId)
                                  VALUES (NULL, ?, 'IIPS', ?, ?, ?, ?, ?, ?, ?, ?, 8751, ?, ?, ?, ?)`)
		if err != nil {
			panic(err.Error())
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(postPopDate.Data, postPopDate.Data2, postPopDate.Data3, postPopDate.Data4, postPopDate.Data5, postPopDate.Data6, postPopDate.Data7, postPopDate.Data8, postPopDate.Data9, postPopDate.Data10, postPopDate.Data11, postPopDate.Data12, postPopDate.Data13)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Posting on POPS eSign transaction")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Posting on POPS")
		}
	})

	router.POST("/api/update-opapproved-occupancy", middleware.TokenChecker(), func(c *gin.Context) {
		type UpdateOpData struct {
			Data int `json:"data"`
		}
		var updateOpData UpdateOpData
		if err := c.ShouldBindJSON(&updateOpData); err != nil {
			c.AbortWithError(http.StatusBadRequest, err)
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("UPDATE occupancy_orderofpayment SET is_approve = 1 WHERE occupancyid = ? AND for_approval = 1 AND is_paid = 0")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(updateOpData.Data)
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Updating Order of Payment on Approval")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Updating Order of Payment on Approval")
		}
	})

	router.POST("/api/update-opapproved-bldgadditional", middleware.TokenChecker(), func(c *gin.Context) {
		type UpdateOpData struct {
			Data int `json:"data"`
		}
		var updateOpData UpdateOpData
		if err := c.ShouldBindJSON(&updateOpData); err != nil {
			c.AbortWithError(http.StatusBadRequest, err)
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("UPDATE building_orderofpayment SET is_approve = 1 WHERE occupancyid = ? AND for_approval = 1 AND is_paid = 0")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(updateOpData.Data)
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Updating Order of Payment on Approval")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Updating Order of Payment on Approval")
		}
	})

	router.POST("/api/update-opapproved-electrical", middleware.TokenChecker(), func(c *gin.Context) {
		type UpdateOpData struct {
			Data int `json:"data"`
		}
		var updateOpData UpdateOpData
		if err := c.ShouldBindJSON(&updateOpData); err != nil {
			c.AbortWithError(http.StatusBadRequest, err)
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("UPDATE electrical_orderofpayment_new SET is_approve = 1 WHERE electricalid = ? AND for_approval = 1 AND is_paid = 0")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(updateOpData.Data)
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Updating Order of Payment on Approval")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Updating Order of Payment on Approval")
		}
	})

	router.POST("/api/update-name", middleware.TokenChecker(), func(c *gin.Context) {
		type NewnameData struct {
			Data  int    `json:"data"`  //employeeid
			Data2 string `json:"data2"` //new name
		}
		var newnameData NewnameData
		if err := c.ShouldBindJSON(&newnameData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("UPDATE employee SET employeename = ? WHERE employeeid = ?")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(newnameData.Data2, newnameData.Data)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Changing Name")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Changing Name")
		}

	})

	router.POST("/api/update-password", middleware.TokenChecker(), func(c *gin.Context) {
		type NewnameData struct {
			Data  int    `json:"data"`  //employeeid
			Data2 string `json:"data2"` //new password
		}
		var newnameData NewnameData
		if err := c.ShouldBindJSON(&newnameData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("UPDATE esign SET password = ? WHERE employeeid = ?")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(newnameData.Data2, newnameData.Data)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Changing Password")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Changing Password")
		}

	})

	router.POST("/api/save-lockdata", middleware.TokenChecker(), func(c *gin.Context) {
		type saveLockData struct {
			Data  string `json:"data"`  //referenceNo
			Data2 string `json:"data2"` //lock_data
		}
		var savelockData saveLockData
		if err := c.ShouldBindJSON(&savelockData); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		dbpost, err := db.Prepare("INSERT INTO esign_lock (esign_lockid, referenceNo, lock_code) VALUES (NULL, ?, ?)")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(savelockData.Data, savelockData.Data2)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Saving Lock Data")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Saving Lock Data")
		}
	})

	router.DELETE("/api/delete-orderofpayment-electrical", middleware.TokenChecker(), func(c *gin.Context) {
		type DeleteOP struct {
			Data int `json:"data"`
		}
		var deleteOp DeleteOP
		if err := c.ShouldBindJSON(&deleteOp); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		var isPaid int
		checkErr := db.QueryRow("SELECT is_paid FROM electrical_orderofpayment_new WHERE electricalid = ?", deleteOp.Data).Scan(&isPaid)
		if checkErr != nil {
			c.AbortWithError(http.StatusInternalServerError, checkErr)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		if isPaid == 1 {
			c.String(http.StatusBadRequest, "Order of Payment has Already been Paid")
			return
		}

		dbpost, err := db.Prepare("DELETE FROM electrical_orderofpayment_new WHERE electricalid = ? AND is_paid = 0")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(deleteOp.Data)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Deleting Order of Payment")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Deleting Order of Payment")
		}
	})

	router.DELETE("/api/delete-orderofpayment-occupancy", middleware.TokenChecker(), func(c *gin.Context) {
		type DeleteOP struct {
			Data int `json:"data"`
		}
		var deleteOp DeleteOP
		if err := c.ShouldBindJSON(&deleteOp); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		var isPaid int
		checkErr := db.QueryRow("SELECT is_paid FROM occupancy_orderofpayment WHERE occupancyid = ?", deleteOp.Data).Scan(&isPaid)
		if checkErr != nil {
			c.AbortWithError(http.StatusInternalServerError, checkErr)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		if isPaid == 1 {
			c.String(http.StatusBadRequest, "Order of Payment has Already been Paid")
			return
		}

		dbpost, err := db.Prepare("DELETE FROM occupancy_orderofpayment WHERE occupancyid = ? AND is_paid = 0")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(deleteOp.Data)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Deleting Order of Payment")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Deleting Order of Payment")
		}
	})

	router.DELETE("/api/void-op-local", func(c *gin.Context) {
		type DeleteOPLocal struct {
			Data string `json:"data"`
		}
		var deleteOpLocal DeleteOPLocal
		if err := c.ShouldBindJSON(&deleteOpLocal); err != nil {
			c.String(http.StatusBadRequest, "Invalid request body")
			return
		}

		c.Writer.Header().Set("X-XSS-Protection", "1; mode=block")
		c.Writer.Header().Set("X-Content-Type-Options", "nosniff")
		c.Writer.Header().Set("X-DNS-Prefetch-Control", "off")
		c.Writer.Header().Set("X-Frame-Options", "DENY")
		c.Writer.Header().Set("X-Download-Options", "noopen")
		c.Writer.Header().Set("Referrer-Policy", "no-referrer")
		c.Writer.Header().Set("Content-Security-Policy", "default-src 'self'; img-src 'self';")
		c.Writer.Header().Set("X-Server", "OCBO Server")

		var resultCount int
		checkErr := dbpop.QueryRow("SELECT COUNT(AFNum) AS resultCount FROM orderpaydetail WHERE OPRefId = ?", deleteOpLocal.Data).Scan(&resultCount)
		if checkErr != nil {
			c.AbortWithError(http.StatusInternalServerError, checkErr)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}

		if resultCount > 0 {
			c.String(http.StatusBadRequest, "Error! Payment already exist.")
			return
		}

		dbpost, err := dbpop.Prepare("DELETE FROM orderpaydetail WHERE OPRefId = ?")
		if err != nil {
			c.AbortWithError(http.StatusInternalServerError, err)
			c.String(http.StatusInternalServerError, "Internal Server Error")
			return
		}
		defer dbpost.Close()

		exec, err := dbpost.Exec(deleteOpLocal.Data)
		if err != nil {
			panic(err.Error())
		}

		affect, err := exec.RowsAffected()
		if err != nil {
			panic(err.Error())
		}

		if affect > 0 {
			c.String(http.StatusOK, "Success on Deleting POPS Local")
		} else {
			c.String(http.StatusInternalServerError, "Failed on Deleting POPS Local")
		}
	})

	router.Run(":4320")
}
