class EMP{
///////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////
    /////////////////////// emp     ///////////////////////////
    ///////////////////////////////////////////////////////////

    async emp(){
        const result = await powers.checkOpenPage("emp");
          if (!result) return;
        pages.addData = "emp.addEmp()"
        pages.urlTable = "model/emp.php"
        pages.nameTable = " الوظائف "
        pages.headTable = 
        `
        <th>أسم الموظف</th>
        <th>رقم الهاتف</th>
        <th>الوظيفة</th>
        <th>#</th>
        `
        main.drawTable(pages.headTable,pages.urlTable,pages.addData,pages.nameTable)
    }
    async addEmp(){
        main.idTypePage = 0;
        $('.bodyModalPage').html('')
        await inputData.drawInput("nameEmp","أسم الموظف ")
        await inputData.drawSelectInput("selectJob","getJob","الوظيقة",0,1,".bodyModalPage","col-12")
        await inputData.drawInput("phoneEmp"," رقم الهاتف ")
        await inputData.drawInput("identyEmp"," رقم البطاقة ")
        await $('.bodyModalPage').append(`<hr/><hr/>`)
        await $('.bodyModalPage').append(`
        <div class=" col-12 row">
        <label for="basic-url">أسم المستخدم </label>
        <div class="input-group mb-3">

        <input type="text" id="usernameEmp" class="form-control " placeholder="Username" aria-label="Username" aria-describedby="basic-addon1">
        <div class="input-group-prepend addationCodeUser">
            <span class="input-group-text" id="basic-addon1">${main.codeUser}</span>
        </div>
        </div>    
        </div>    
        `)
        await inputData.drawInput("passEmp"," الرقم السري ")
        inputData.buttonModal("اضافة","emp.saveEmp()")

        $('#modalPage').modal('show')
    }
    async saveEmp(){
        var nameEmp = $('#nameEmp').val()
        var selectJob = $('#selectJob').val()
        var phoneEmp = $('#phoneEmp').val()
        var identyEmp = $('#identyEmp').val()
        var usernameEmp = $('#usernameEmp').val()
        var passEmp = $('#passEmp').val()
        if(usernameEmp == "" || passEmp == "" || nameEmp == ""){
            new sweetAlert("يجب اكمال ادخال البيانات","","error")
            return false
        }
        await main.getCount("getCountEmp",usernameEmp,[main.codeUser]);
        if(main.dataResponse.num == 0){
            main.sendData("addEmp",main.idTypePage,[nameEmp,phoneEmp,identyEmp,usernameEmp,passEmp,selectJob]);
        }else{
            new sweetAlert("تم استخدام اسم المستخدم من قبل","","error")
        }
        // main.sendData("addEmp",main.idTypePage,[nameEmp,selectJob,phoneEmp,identyEmp])
    }

    async getEmp(id){
        await emp.addEmp()
        await main.getData("getData","getEmp",id)
        await $('#nameEmp').val(main.dataResponse[0].name_emp)
        await $('#selectJob').val(main.dataResponse[0].id_job)
        await $('#phoneEmp').val(main.dataResponse[0].phone_emp)
        await $('#identyEmp').val(main.dataResponse[0].identy_emp)
        await $('#usernameEmp').val(main.codeUser+main.dataResponse[0].username)
        await $('#passEmp').val(main.dataResponse[0].password)
        await $('#usernameEmp').attr("readonly",true)
        await $('.addationCodeUser').remove()
        main.idTypePage = id;
    }

    async getPowers(id){
        main.idTypePage = id;
        await $('.bodyModalPage').html('');
        await $('.bodyModalPage').append(
            `
       <div class="container-fluid permission-modal">

  <!-- الموظفين -->
  <div class="card permission-card mb-3">
    <div class="card-header bg-primary text-white fw-bold text-center">
      الموظفين
    </div>
    <div class="card-body text-center">
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power1" value="1">
        <label class="form-check-label" for="power1">الموظفين</label>
      </div>
    </div>
  </div>

  <!-- الخدمات -->
  <div class="card permission-card mb-3">
    <div class="card-header bg-primary text-white fw-bold text-center">
      الخدمات
    </div>
    <div class="card-body text-center">
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power2" value="2">
        <label class="form-check-label" for="power2">عرض</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power3" value="3">
        <label class="form-check-label" for="power3">إضافة</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power4" value="4">
        <label class="form-check-label" for="power4">تعديل</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power5" value="5">
        <label class="form-check-label" for="power5">حذف</label>
      </div>
    </div>
  </div>

  <!-- العملاء -->
  <div class="card permission-card mb-3">
    <div class="card-header bg-primary text-white fw-bold text-center">
      العملاء
    </div>
    <div class="card-body text-center">
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power6" value="6">
        <label class="form-check-label" for="power6">عرض</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power7" value="7">
        <label class="form-check-label" for="power7">إضافة</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power8" value="8">
        <label class="form-check-label" for="power8">تعديل</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power9" value="9">
        <label class="form-check-label" for="power9">حذف</label>
      </div>
    </div>
  </div>

  <!-- المبيعات -->
  <div class="card permission-card mb-3">
    <div class="card-header bg-primary text-white fw-bold text-center">
      المبيعات
    </div>
    <div class="card-body text-center">
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power10" value="10">
        <label class="form-check-label" for="power10">البيع</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power11" value="11">
        <label class="form-check-label" for="power11">الخصم</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power12" value="12">
        <label class="form-check-label" for="power12">حذف المبيعات</label>
      </div>
    </div>
  </div>

  <!-- المصاريف -->
  <div class="card permission-card mb-3">
    <div class="card-header bg-primary text-white fw-bold text-center">
      المصاريف
    </div>
    <div class="card-body text-center">
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power14" value="14">
        <label class="form-check-label" for="power14">إضافة</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power15" value="15">
        <label class="form-check-label" for="power15">تعديل</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power16" value="16">
        <label class="form-check-label" for="power16">حذف</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power17" value="17">
        <label class="form-check-label" for="power17">صرف</label>
      </div>
    </div>
  </div>

  <!-- التقارير -->
  <div class="card permission-card mb-3">
    <div class="card-header bg-primary text-white fw-bold text-center">
      التقارير
    </div>
    <div class="card-body text-center">
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power18" value="18">
        <label class="form-check-label" for="power18">تقرير المصاريف</label>
      </div>
      <div class="form-check d-inline-block mx-3" dir="rtl">
        <input class="form-check-input" type="checkbox" id="power13" value="13">
        <label class="form-check-label" for="power13">تقرير المبيعات</label>
      </div>
    </div>
  </div>

</div>

          
            `

        )
        await inputData.buttonModal("حفظ","emp.savePowers()")
        await $('#modalPage').modal('show')
        await main.getData("getData","getPowerEmp",id)
        for(var i = 0;i <= main.dataResponse.length;i++){
            let powerId = main.dataResponse[i].power;
            let isPower = main.dataResponse[i].type_power;
            if(isPower == 1){
                $('#power'+powerId).prop('checked', true);
            }
        }
    }
    async savePowers(){
        var countPowers = 18;
        var empPowers = [];
        for(var i = 1;i <= countPowers;i++){
            if($('#power'+i).prop("checked")){
                var typePower = 1;
            }else{
                var typePower = 0;
            }
            empPowers[i - 1] = typePower
        }
        main.sendData("savePowers",main.idTypePage,empPowers);
    }


    async login(){
        const result = await powers.checkOpenPage("emp");
        if (!result) return;
        pages.urlTable = "model/login_emp.php"
        pages.addData = ""
        pages.nameTable = " سجل دخول الموظفين "
        pages.headTable = 
        `
        <th>أسم الموظف</th>
        <th>حالة الموظف</th>
        <th>IP ADDRESS</th>
        <th>الوقت</th>
        `
        main.drawTable(pages.headTable,pages.urlTable,pages.addData,pages.nameTable)
    }
}

var emp = new EMP()