class Pages{
    headTable = "";
    nameTable = "";
    urlTable = "";
    addData = "";
    ///////////////////////////////////////////////////////////
    /////////////////////// Years   ///////////////////////////
    ///////////////////////////////////////////////////////////
    async catService(){
        const data = await powers.checkOpenPage("service");
        if (!data) return;
        pages.addData = "pages.addCatService()"
        pages.urlTable = "model/catService.php"
        pages.nameTable = "اقسام الخدمات"
        pages.headTable = 
        `
        <th>أسم القسم</th>
        <th>#</th>
        `
        main.drawTable(pages.headTable,pages.urlTable,pages.addData,pages.nameTable)
    }
    async addCatService(){

        main.idTypePage = 0;
        $('.bodyModalPage').html('')
        inputData.drawInput("nameCatService","أسم  القسم")
        inputData.buttonModal("اضافة","pages.saveCatService()")

        $('#modalPage').modal('show')
    }
    async saveCatService(){
        if(main.idTypePage != 0){
            const data = await powers.checkOpenMethod("service","update");
            if (!data) return;
        }else{
            const data = await powers.checkOpenMethod("service","add");
            if (!data) return;
        }
        var nameCatService = $('#nameCatService').val()

        main.sendData("addCatService",main.idTypePage,[nameCatService])
    } 
    async getCatService(id){
        const data = await powers.checkOpenMethod("service","show");
        if (!data) return;
        await pages.addCatService()
        await main.getData("getData","geCatService",id)
        await $('#nameCatService').val(main.dataResponse[0].name_cat_service)
        main.idTypePage = id
    }
    ///////////////////////////////////////////////////////////


    ///////////////////////////////////////////////////////////
    ///////////////////////  exps   ///////////////////////////
    ///////////////////////////////////////////////////////////
    async exp(){
        const data = await powers.checkOpenPage("exp");
        if (!data) return;
        pages.addData = "pages.addExp()"
        pages.urlTable = "model/exp.php"
        pages.nameTable = "المصاريف"
        pages.headTable = 
        `
        <th>أسم المصروف</th>
        <th>#</th>
        `
        main.drawTable(pages.headTable,pages.urlTable,pages.addData,pages.nameTable)
    }
    async addExp(){
        main.idTypePage = 0;
        $('.bodyModalPage').html('')
        inputData.drawInput("nameExp","أسم المصروف")
        inputData.buttonModal("اضافة","pages.saveExp()")

        $('#modalPage').modal('show')
    }
    async saveExp(){
        if(main.idTypePage ==0){
            const data = await powers.checkOpenMethod("exp","add");
            if (!data) return;
        }else{
            const data = await powers.checkOpenMethod("exp","update");
            if (!data) return;
        }
        var nameExp = $('#nameExp').val()
        main.sendData("addExp",main.idTypePage,[nameExp])
    } 
    async getExp(id){
        await pages.addExp()
        await main.getData("getData","getExp",id)
        await $('#nameExp').val(main.dataResponse[0].name_exp)
        main.idTypePage = id
    }

    async outExp(id){
        const data = await powers.checkOpenMethod("exp","outExp");
        if (!data) return;
        main.idTypePage = id
        $('.bodyModalPage').html('')
        inputData.drawInput("exp","القيمة","number")
        inputData.drawTextAreaInput("notes","ملاحظات")
        inputData.buttonModal("اضافة","pages.saveOutExp()")

         $('#modalPage').modal('show')
    }

    async saveOutExp(){
        var exp = $('#exp').val()
        var notes = $('#notes').val()
        main.sendData("addOutExp",main.idTypePage,[main.idEmp,exp,notes])
    }
 

    ///////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////
    ///////////////////////  cus   ///////////////////////////
    ///////////////////////////////////////////////////////////

        async cus(){
        const data = await powers.checkOpenPage("cus");
        if (!data) return;
        pages.addData = "pages.addCus()"
        pages.urlTable = "model/cus.php"
        pages.nameTable = "العملاء"
        pages.headTable = 
        `
        <th>أسم العميل</th>
        <th> رقم الهاتف</th>
        <th>#</th>
        `
        main.drawTable(pages.headTable,pages.urlTable,pages.addData,pages.nameTable)
    }
    async addCus(){

        main.idTypePage = 0;
        $('.bodyModalPage').html('')
        await inputData.drawInput("nameCus","أسم العميل")
        await inputData.drawInput("phoneCus"," رقم الهاتف")
        await inputData.buttonModal("اضافة","pages.saveCus()")

        $('#modalPage').modal('show')
    }
    async saveCus(){
        if(main.idTypePage == 0){
            const data = await powers.checkOpenMethod("cus","add");
            if (!data) return;
        }else{
            const data = await powers.checkOpenMethod("cus","update");
            if (!data) return;
        }
        var nameCus = $('#nameCus').val()
        var phoneCus = $('#phoneCus').val()
        main.sendData("addCus",main.idTypePage,[nameCus,phoneCus])
    } 
    async getCus(id){
        
        await pages.addCus()
        await main.getData("getData","getCus",id)
        await $('#nameCus').val(main.dataResponse[0].name_cus)
        await $('#phoneCus').val(main.dataResponse[0].phone_cus)

        main.idTypePage = id
    }

    ///////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////
    ///////////////////////  pro   ///////////////////////////
    ///////////////////////////////////////////////////////////
    async service(){
        const data = await powers.checkOpenPage("service");
        if (!data) return;
        pages.addData = "pages.addService()"
        pages.urlTable = "model/service.php"
        pages.nameTable = "الخدمات"
        pages.headTable = 
        `
        <th>أسم الخدمة</th>
        <th>أسم القسم</th>
        <th>السعر</th>
        <th>#</th>
        `
        main.drawTable(pages.headTable,pages.urlTable,pages.addData,pages.nameTable)
    }
    async addService(){

        main.idTypePage = 0;
        $('.bodyModalPage').html('')
        await inputData.drawInput("nameService","أسم الخدمة")
        await inputData.drawSelectInput("selectCat","geCatService","أقسام الخدمة",0,1)
        await inputData.drawInput("priceService"," السعر")
        await inputData.buttonModal("اضافة","pages.saveService()")

        $('#modalPage').modal('show')
    }
    async saveService(){
        if(main.idTypePage == 0){
            const data = await powers.checkOpenMethod("service","add");
            if (!data) return;
        }else{
            const data = await powers.checkOpenMethod("service","update");
            if (!data) return;
        }
        var nameService = $('#nameService').val()
        var selectCat = $('#selectCat').val()
        var priceService = $('#priceService').val()
        main.sendData("addService",main.idTypePage,[nameService,selectCat,priceService])
        sales.getDataService()
    } 
    async getService(id){
        
        await pages.addService()
        await main.getData("getData","getService",id)
        await $('#nameService').val(main.dataResponse[0].name_service)
        await $('#selectCat').val(main.dataResponse[0].id_cat_service)
        await $('#priceService').val(main.dataResponse[0].price_service)
        main.idTypePage = id
    }


    ///////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////
    /////////////////////// jobs    ///////////////////////////
    ///////////////////////////////////////////////////////////

    async job(){
        const data = await powers.checkOpenPage("emp");
        if (!data) return;
        pages.addData = "pages.addJob()"
        pages.urlTable = "model/job.php"
        pages.nameTable = " الوظائف "
        pages.headTable = 
        `
        <th>أسم الوظيفة</th>
        <th>#</th>
        `
        main.drawTable(pages.headTable,pages.urlTable,pages.addData,pages.nameTable)
    }
    async addJob(){
        main.idTypePage = 0;
        $('.bodyModalPage').html('')
        inputData.drawInput("nameJob","أسم الوظيفة ")
        inputData.buttonModal("اضافة","pages.saveJob()")

        $('#modalPage').modal('show')
    }
    async saveJob(){
        var nameJob = $('#nameJob').val()

        main.sendData("addJob",main.idTypePage,[nameJob])
    } 
    async getJob(id){
        await pages.addJob()
        await main.getData("getData","getJob",id)
        await $('#nameJob').val(main.dataResponse[0].name_job)
        main.idTypePage = id
    }

    

    ///////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////
    /////////////////////// type_paids    ///////////////////////////
    ///////////////////////////////////////////////////////////

    async typePaid(){
        const data = await powers.checkOpenPage("service");
        if (!data) return;
        pages.addData = "pages.addTypePaid()"
        pages.urlTable = "model/typePaid.php"
        pages.nameTable = " طرق الدقع "
        pages.headTable = 
        `
        <th> أسم طريقة الدفع</th>
        <th>#</th>
        `
        main.drawTable(pages.headTable,pages.urlTable,pages.addData,pages.nameTable)
    }
    async addTypePaid(){
        main.idTypePage = 0;
        $('.bodyModalPage').html('')
        inputData.drawInput("nameTypePaid","أسم طريقة الدفع ")
        inputData.buttonModal("اضافة","pages.saveTypePaid()")

        $('#modalPage').modal('show')
    }
    async saveTypePaid(){
        var nameTypePaid = $('#nameTypePaid').val()

        main.sendData("addTypePaid",main.idTypePage,[nameTypePaid])
    } 
    async getTypePaid(id){
        await pages.addTypePaid()
        await main.getData("getData","getTypePaid",id)
        await $('#nameTypePaid').val(main.dataResponse[0].name_type_paids)
        main.idTypePage = id
    }



    ///////////////////////////////

    async home(){
       await  $('.mainBody').html(`
          <!-- PAGE BODY -->
    <div class="content">
        <h2 class="mb-4">الصفحة الرئيسية</h2>

        <div class="row">
            <!-- Box -->
            <div class="col-md-3">
                <div class="card shadow-sm mb-4">
                    <div class="card-body">
                        <h5 class="card-title">عدد الزيارات الجديدة</h5>
                        <p class="card-text fs-3 fw-bold text-primary countVistToday" id="allUser">***</p>
                    </div>
                </div>
            </div>

            <div class="col-md-3">
                <div class="card shadow-sm mb-4">
                    <div class="card-body">
                        <h5 class="card-title">المبيعات</h5>
                        <p class="card-text fs-3 fw-bold text-success totalSales" id="allSales">***</p>
                    </div>
                </div>
            </div>

            <div class="col-md-3">
                <div class="card shadow-sm mb-4">
                    <div class="card-body">
                        <h5 class="card-title">المصاريف</h5>
                        <p class="card-text fs-3 fw-bold text-danger totalExp" id="allExp">***</p>
                    </div>
                </div>
            </div>

            <div class="col-md-3">
                <div class="card shadow-sm mb-4">
                    <div class="card-body">
                        <h5 class="card-title">الخزنة</h5>
                        <p class="card-text fs-3 fw-bold text-success totalSafe" id="netTotal">***</p>
                    </div>
                </div>
            </div>
        </div>


      </div>

        <br/>
        <div class="row">
        <div class="col-4">
        <div class="card shadow-sm">
          <div class="card-header bg-white fw-bold">طرق الدفع</div>
          <div class="card-body">
            <canvas id="paymentChart" height="180"></canvas>
          </div>
        </div>
      </div>


              <div class="col-8">
        <div class="card shadow-sm">
          <div class="card-header bg-white fw-bold">مبيعات الموظفين</div>
          <div class="card-body">
            <canvas id="paymentCatChart" height="180"></canvas>
          </div>
        </div>
      </div>
        </div>





        </div>

        <br/>
    </div>    
        `)
       await  pages.getDataDashboard()

    }

    //////////////////

    async getDataDashboard(){
        async function getDataDashboard(){
    await $.ajax({
        url:"model/dashboard.php",
        type:"POST",
        dataType:"JSON",
        data:{
            empPower:[powersEmp[11],powersEmp[12],powersEmp[17]],
        },
        success:async function(data){

            console.log(data);

            if (data.success != 1) {
                console.log("No Permission / success = 0");
                return;
            }

            // KPIs
            $("#allUser").text(data.allUser ?? 0);
            $("#allExp").text((data.allExp ?? 0).toLocaleString("ar-EG"));
            $("#allSales").text((data.allSales ?? 0).toLocaleString("ar-EG"));
            $("#netTotal").text((data.total ?? 0).toLocaleString("ar-EG"));

            // --- Doughnut Chart (طرق الدفع) ---
            const payCtx = document.getElementById("paymentChart");
            if (payCtx) {
                new Chart(payCtx, {
                    type: "doughnut",
                    data: {
                        labels: data.paymentChart.labels,
                        datasets: [{
                            label: "إجمالي المبيعات",
                            data: data.paymentChart.data,
                            backgroundColor: data.paymentChart.colors,
                            borderWidth: 1
                        }]
                    },
                    options: {
                        plugins: {
                            legend: {
                                position: "bottom",
                                labels: {
                                    color: "#1e293b",
                                    font: { size: 14 }
                                }
                            },
                            tooltip: {
                                callbacks: {
                                    label: (ctx) => {
                                        const v = ctx.raw.toLocaleString("ar-EG");
                                        return `${ctx.label} : ${v} جـ`;
                                    }
                                }
                            }
                        }
                    }
                });
            }

            // --- Bar Chart (مبيعات الموظفين) ---
            const empCtx = document.getElementById("paymentCatChart");
            if (empCtx) {
                new Chart(empCtx, {
                    type: "bar",
                    data: {
                        labels: data.empChart.labels,
                        datasets: [{
                            label: "مبيعات الموظف",
                            data: data.empChart.data,
                            backgroundColor: data.empChart.colors,
                            borderWidth: 1
                        }]
                    },
                    options: {
                        plugins: { legend: { display: false } },
                        scales: { y: { beginAtZero: true } }
                    }
                });
            }
        }
    });
}

// <-- تشغيل الداشبورد عند تحميل الصفحة -->
$(document).ready(()=>{
    getDataDashboard();
});

    }
    ///////////////

    async deleteExpRebort(id){
        var x = confirm("هل تريد الحذف")
        if(x){
            await main.deleteData("out_exp",id)
            await main.updatePage()
        }
    }
  



}
var pages = new Pages()