// Script edit
function loadRecordData(node){
    length = node.getAttribute('data-length');
    for(i = 0; i < length; i++){
        attrData = node.getAttribute("data".concat("-", i.toString()));
        document.getElementById("inp-"+i.toString()).value = attrData;
    }

}
function showPass(t){
    if (t.checked){
        document.getElementById("inp-3").setAttribute('type','text');
        document.getElementById("hienmatkhau-btn").innerHTML = '<i class="fa-solid fa-eye"></i>';
    }
    else{
        document.getElementById("inp-3").setAttribute('type','password');
        document.getElementById("hienmatkhau-btn").innerHTML = '<i class="fa-solid fa-eye-slash"></i>';
    }
}

function showNhommon(val,makhoi) {
    function ten(mn){
        return mn.MaKhoi == val;
    }
    console.log(makhoi);
    console.log(val);
    tenNM = makhoi.find(ten).TenNhomMon;
    document.getElementById("tenNhom").value =tenNM ;
}

// Script chinh sua cac thanh phan chung
function showUploadedImage(t, target, mode){
    if(mode == "replace"){
        const [file] = t.files
        if (file) {
          let imgTag = document.getElementById(target)
          imgTag.src = URL.createObjectURL(file);
          imgTag.style.outline = "2px solid black";
          imgTag.classList = "uploaded-img";
        }
    }
    else{
        const [file] = t.files;
        if (file) {
          let imgContainer = document.createElement('div');
          let imgTag = document.createElement('img');
          let imgRemoveBtn = document.createElement('button');
          numUploadedImg = imgContainer.childElementCount;

          imgContainerId = 'carousel-'+numUploadedImg;
          imgContainer.setAttribute('id', imgContainerId);
          imgContainer.classList = "uploaded-img-container";
          imgContainer.appendChild(imgTag);
          imgContainer.appendChild(imgRemoveBtn);

          imgTag.src = URL.createObjectURL(file);
          imgTag.setAttribute('name', 'carousel[]');
          imgTag.style.border = "2px solid black";
          imgTag.classList = "uploaded-img";

          imgRemoveBtn.innerHTML = '<i class="fa-solid fa-trash"></i>';
          imgRemoveBtn.classList = "remove-uploaded-img-btn";
          imgRemoveBtn.type = "button";
          imgRemoveBtn.addEventListener("click", function(){
            document.getElementById(imgContainerId).remove();
          });

          document.getElementById(target).appendChild(imgContainer);
        }
    }
}

// Script phan quyen nguoi dung
function toggleAllQuyens(nhomquyen){
    let maNhomQuyen = nhomquyen.getAttribute('data-nhomquyen');
    let inputQuyens = document.querySelectorAll('[data-nhomquyen="'+ maNhomQuyen +'"]');
    inputQuyens.forEach(element => {
        element.checked = nhomquyen.checked;
    });
}

function toggleQuyen(quyen){
    let maNhomQuyen = quyen.getAttribute('data-nhomquyen');
    let inputQuyens = document.querySelectorAll('[data-nhomquyen="'+ maNhomQuyen +'"]');

    let firstStatus = inputQuyens[1].checked;
    let isAllStatusTheSame = true;

    for(let i = 2; i< inputQuyens.length; i++){
        if (firstStatus != inputQuyens[i].checked){
            isAllStatusTheSame = false;
        }
    }

    let nhom = document.getElementById('nhom-' + maNhomQuyen);
    if (isAllStatusTheSame == true){
        nhom.checked = quyen.checked;
    }
    else{
        nhom.checked = false;
    }
}

