<?php
  include '../autoload.php';
 ?>
<!DOCTYPE html>

<html>

<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  
  <script src="/eoDACSBootstrap/eoDACS/SubFW/js/C_Date.js"></script>
  <!--
  <link rel="stylesheet" type="text/css" href="<?php echo $DHTMLX_URL;?>/codebase/fonts/font_roboto/roboto.css" />
  <link rel="stylesheet" type="text/css" href="<?php echo $DHTMLX_URL;?>/codebase/dhtmlx.css" />
  <link rel="stylesheet" type="text/css" href="<?php echo $DHTMLX_URL;?>/skins/web/dhtmlx.css" />
  <link rel="stylesheet" type="text/css" href="<?php echo $EXTERN_CSS;?>/bootstrap-4.3.1/css/bootstrap.min.css" />
  <link rel="shortcut icon" type="image/x-icon" href="<?php echo $EODACS_ROOT;?>/../eoDACSCommon/pics/eodacs.png" />
  <link rel="stylesheet" type="text/css" href="<?php echo $EODACS_ROOT;?>/css/eoslider.css" />
  <script type='text/javascript' src="<?php echo $DHTMLX_URL;?>/codebase/dhtmlx.js"></script>
  <link href="<?php echo $EXTERN_CSS;?>/font-awesome-current/css/all.min.css" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="<?php echo $EXTERN_CSS;?>/checkbox/searchableOptionList.css">
  <script type="text/javascript" src="js/jquery/jquery.js"></script>
  <script type="text/javascript" src="<?php echo $EXTERN_CSS;?>/checkbox/sol.js"></script>
  <script type="text/javascript" src="js/eodacs_utils.js?v=<?php echo $js_version;?>"></script>
  

  <?php
            $libmanager = new eoLibrariesManagerDACS ();
            $libmanager->init_page();
            $libmanager->render_library ();
        ?>
    -->

  <title>Study export</title>
  <style>

    .ui-slider .ui-btn-inner {
    padding: 4px 0 0 0 !important;
    }

    .ui-slider-popup {
        position: absolute !important;
        width: 64px;
        height: 64px;
        text-align: center;
        font-size: 36px;
        padding-top: 14px;
        z-index: 100;
        opacity: 0.8;
    }

    .progressinfinitebar{
      position:absolute;
      width:90%;
      height:50px;
      overflow-x: hidden;
    }

    .line{
      position:absolute;
      opacity: 0.4;
      background:#4a8df8;
      width:150%;
      height:5px;
    }

    .subline{
      position:absolute;
      background:#4a8df8;
      width:100%;
      height:5px;
    }
    .inc{
      animation: increase 1s infinite;
    }
    .dec{
      animation: decrease 1s 0.5s infinite;
    }

    @keyframes increase {
       from {transform:translateX(-5%) scale(.05);}
       to {transform:translateX(130%) scale(1);}
    }
    @keyframes decrease {
       from {transform:translateX(-80%) scale(.8);}
       to {transform:translateX(110%) scale(.1);}
    }
    </style>
</head>

<script>

    function init_layout ()
    {
        var myCalendar = new C_Date('inputFrom');
        myCalendar.setDateFormat("%d/%m/%Y");
        myCalendar.hideTime();
        myCalendar.showToday();

        myCalendar.attachEvent("onButtonClick", function(date){
            if (date == null) 
            {
                // clear button
            } 
            else 
            {
                // today button
                myCalendar.hide();
                myCalendar2.show();
            }
        });

        var myCalendar2 = new C_Date('inputTo')
        myCalendar2.setDateFormat("%d/%m/%Y");
        myCalendar2.hideTime();
        myCalendar2.showToday();

        myCalendar.attachEvent("onShow", function(){
            myCalendar2.hide();
        });
        myCalendar2.attachEvent("onButtonClick", function(date){
            if (date == null) 
            {
                // clear button
            } 
            else 
            {
                // today button
                // hide calendar if needed
                myCalendar2.hide();
            }
        });

        myCalendar.attachEvent("onClick",function(date){
            if(document.getElementById('inputTo').value == '')
                myCalendar2.setDate(myCalendar.getDate());
            myCalendar2.show();
    });
    myCalendar.attachEvent("onButtonClick", function(date){
        if (date == null) {
            // clear button
        } else {
            // today button
            this.hide();
            myCalendar2.show();
        }
    });
    myCalendar2.attachEvent("onButtonClick", function(date){
        if (date == null) {
            // clear button
        } else {
            // today button
            this.hide();
        }
      });

      <?php
        $v_masterdescstudy = true;
        $v_masterdescserie = true;
        $v_descstudy = true;
        $v_descserie = true;
        $v_modalitytype = $_GET['modalitytitle'];
        $v_weight = true;
        $v_weightmin = 0;
        $v_weightmax = 200;
        $v_age = true;
        $v_agemin = 0;
        $v_agemax = 120;
        $v_exporttype = true;
        $v_activity = false;

        if ($_GET['type'] == AFCN_ANNEXE3) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = AFCN_ANNEXE3_AGEMIN;
            $v_agemax = AFCN_ANNEXE3_AGEMAX;
            $v_weightmin = AFCN_ANNEXE3_WEIGHTMIN;
            $v_weightmax = AFCN_ANNEXE3_WEIGHTMAX;
        }


        if ($_GET['type'] == AFCN_ANNEXE4) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = AFCN_ANNEXE4_AGEMIN;
            $v_agemax = AFCN_ANNEXE4_AGEMAX;
            $v_weightmin = AFCN_ANNEXE4_WEIGHTMIN;
            $v_weightmax = AFCN_ANNEXE4_WEIGHTMAX;
        }

        if ($_GET['type'] == AFCN_ANNEXE5) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = AFCN_ANNEXE5_AGEMIN;
            $v_agemax = AFCN_ANNEXE5_AGEMAX;
            $v_weightmin = AFCN_ANNEXE5_WEIGHTMIN;
            $v_weightmax = AFCN_ANNEXE5_WEIGHTMAX;
        }

        if ($_GET['type'] == AFCN_ANNEXE6) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = AFCN_ANNEXE6_AGEMIN;
            $v_agemax = AFCN_ANNEXE6_AGEMAX;
            $v_weightmin = AFCN_ANNEXE6_WEIGHTMIN;
            $v_weightmax = AFCN_ANNEXE6_WEIGHTMAX;
        }
        if ($_GET['type'] == AFCN_ANNEXE7) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = AFCN_ANNEXE7_AGEMIN;
            $v_agemax = AFCN_ANNEXE7_AGEMAX;
            $v_weightmin = AFCN_ANNEXE7_WEIGHTMIN;
            $v_weightmax = AFCN_ANNEXE7_WEIGHTMAX;
        }
        if ($_GET['type'] == AFCN_ANNEXE8) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = AFCN_ANNEXE8_AGEMIN;
            $v_agemax = AFCN_ANNEXE8_AGEMAX;
            $v_weightmin = AFCN_ANNEXE8_WEIGHTMIN;
            $v_weightmax = AFCN_ANNEXE8_WEIGHTMAX;
        }

        if ($_GET['type'] == IRSN_NMADULTE) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = IRSN_NMADULTE_AGEMIN;
            $v_agemax = IRSN_NMADULTE_AGEMAX;
            $v_weightmin = IRSN_NMADULTE_WEIGHTMIN;
            $v_weightmax = IRSN_NMADULTE_WEIGHTMAX;
        }

        if ($_GET['type'] == IRSN_NMPEDIATRIQUE) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = IRSN_NMPEDIATRIQUE_AGEMIN;
            $v_agemax = IRSN_NMPEDIATRIQUE_AGEMAX;
            $v_weightmin = IRSN_NMPEDIATRIQUE_WEIGHTMIN;
            $v_weightmax = IRSN_NMPEDIATRIQUE_WEIGHTMAX;
            $v_activity = true;
        }

        if ($_GET['type'] == IRSN_NMCTADULTE) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = IRSN_NMCTADULTE_AGEMIN;
            $v_agemax = IRSN_NMCTADULTE_AGEMAX;
            $v_weightmin = IRSN_NMCTADULTE_WEIGHTMIN;
            $v_weightmax = IRSN_NMCTADULTE_WEIGHTMAX;
        }
        if ($_GET['type'] == IRSN_CTADULTE) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = IRSN_CTADULTE_AGEMIN;
            $v_agemax = IRSN_CTADULTE_AGEMAX;
            $v_weightmin = IRSN_CTADULTE_WEIGHTMIN;
            $v_weightmax = IRSN_CTADULTE_WEIGHTMAX;
        }
        if ($_GET['type'] == IRSN_CTPEDIATRIQUE) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = IRSN_CTPEDIATRIQUE_AGEMIN;
            $v_agemax = IRSN_CTPEDIATRIQUE_AGEMAX;
            $v_weightmin = IRSN_CTPEDIATRIQUE_WEIGHTMIN;
            $v_weightmax = IRSN_CTPEDIATRIQUE_WEIGHTMAX;
        }
        if ($_GET['type'] == IRSN_RXADULTE) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = IRSN_RXADULTE_AGEMIN;
            $v_agemax = IRSN_RXADULTE_AGEMAX;
            $v_weightmin = IRSN_RXADULTE_WEIGHTMIN;
            $v_weightmax = IRSN_RXADULTE_WEIGHTMAX;
        }
        if ($_GET['type'] == IRSN_RXPEDIATRIQUE) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = IRSN_RXPEDIATRIQUE_AGEMIN;
            $v_agemax = IRSN_RXPEDIATRIQUE_AGEMAX;
            $v_weightmin = IRSN_RXPEDIATRIQUE_WEIGHTMIN;
            $v_weightmax = IRSN_RXPEDIATRIQUE_WEIGHTMAX;
        }

        if ($_GET['type'] == IRSN_RIADULTE) {
            $v_masterdescstudy = false;
            $v_masterdescserie = false;
            $v_descstudy = false;
            $v_descserie = false;
            $v_agemin = IRSN_RIADULTE_AGEMIN;
            $v_agemax = IRSN_RIADULTE_AGEMAX;
            $v_weightmin = IRSN_RIADULTE_WEIGHTMIN;
            $v_weightmax = IRSN_RIADULTE_WEIGHTMAX;
        }
       ?>

    <?php if ($v_masterdescstudy) { ?>
    $(function() {
        __solstudymaster = $('#studylist').searchableOptionList({
            texts: {
                selectNone: 'Select all'
            },
            events: {

            },
            maxHeight: '250px',
            showSelectNoneOnly: true,
            data: '<?php echo $EODACS_ROOT;?>/SubFW/database/stats/get_studymaster.php'
        });
    });
    <?php } ?>

    <?php if ($v_masterdescserie) { ?>
    $(function() {
        __solseriemaster = $('#serielist').searchableOptionList({
            texts: {
                selectNone: 'Select all'
            },
            events: {

            },
            maxHeight: '250px',
            showSelectNoneOnly: true,
            data: '<?php echo $EODACS_ROOT;?>/SubFW/database/stats/get_seriemaster.php'
        });
    });
    <?php } ?>
    <?php if ($v_descstudy) { ?>

    <?php } ?>

    <?php if ($v_descserie) { ?>

    <?php } ?>

    $(function() {
        __solmodality = $('#exp_modality').searchableOptionList({
            texts: {
                selectNone: 'Select all'
            },
            events: {

            },
            maxHeight: '250px',
            showSelectNoneOnly: true,
            data: '<?php echo $EODACS_ROOT;?>/SubFW/database/common/get_modality.php?modality=<?php echo $v_modalitytype;?>&filterri=true'

        });
    });

            // data: '<?php echo $EODACS_ROOT;?>/dbconnector/common/get_modality.php?modality=<?php echo $v_modalitytype;?>&filterri=true'
    <?php if ($v_exporttype) { ?>
    $(function() {
        __solexporttype = $('#export').searchableOptionList({
            texts: {
                selectNone: 'Select all'
            },
            events: {

            },
            maxHeight: '200px',
            showSelectNoneOnly: true,
            // data: '<?php echo $EODACS_ROOT;?>/dbconnector/afcn/get_exporttype.php?afcnid=<?php echo $_GET['type'];?>'
            data: '<?php echo $EODACS_ROOT;?>/SubFW/database/afcn/get_exporttype.php?afcnid=<?php echo $_GET['type'];?>'
        });
    });
    <?php } ?>


    //prevent submit with enter
    document.getElementById("formstudy").onkeypress = function(e) {
        var key = e.charCode || e.keyCode || 0;
        if (key == 13) {
            return false;
        }
    }
  }
</script>

<body>
  <form id="formstudy" class="needs-validation w-100" novalidate="">
    <?php if ($v_masterdescstudy || $v_masterdescserie) { ?>
    <div class="row m-0 p-0">
      <?php if ($v_masterdescstudy) { ?>
      <div class="col-md-6 m-0 p-0 pe-3">
        <label for="inputStudy" data-langkey="Studies :">
          <?php echo $_Study;?>
        </label>
        <div id='studylist' data-sol-name='character' multiple='multiple'></div>
      </div>
      <?php } ?>
      <?php if ($v_masterdescserie) { ?>
      <div class="col-md-6  m-0 p-0">
        <label for="inputSerie" data-langkey="Series :">
          <?php echo $_Serie;?>
        </label>
        <div id='serielist' data-sol-name='character' multiple='multiple'></div>
      </div>
      <?php } ?>
    </div>
    <?php } ?>
    <?php if (isset($_GET['lol'])) { ?>
    <div class="form-row">
      <div class="form-group col-md-6">
        <label for="inputbodypart">
          <?php echo $_BodyPart;?>
        </label>
        <div id='bodypart' data-sol-name='character' multiple='multiple'></div>
      </div>
    </div>
    <?php }?>
    <div class="row m-0 p-0">
        <div class="col-md-auto m-0 p-0">
            <label for="inputFrom" data-langkey="From :"></label>
            <input type="text" class="form-control form-control-sm mt-2" id="inputFrom" placeholder="<?php echo $_From;?>" autocomplete="off">
            <div class="invalid-feedback">
                Start date can not be empty
            </div>
        </div>
        
        <div class="col-md-auto m-0 p-0 ps-3">
            <label for="inputTo" data-langkey="To :"></label>
            <input type="text" class="form-control form-control-sm mt-2" id="inputTo" placeholder="<?php echo $_To;?>" autocomplete="off">
            <div class="invalid-feedback">
                End date can not be empty
            </div>
        </div>
    </div>

    <div class="row m-0 p-0 pt-3">
        <div class="col-md-6 m-0 p-0">
            <label for="inputModality" data-langkey="Modality :"></label>
            <div id='exp_modality' class="m-0 p-0" data-sol-name='character' multiple='multiple'></div>
        </div>
        <div class="col-md-6 m-0 p-0 ps-3">
            <label for="inputExport" data-langkey="Exams :"></label>
            <div id='export' data-sol-name='character' multiple='multiple'></div>
        </div>
    </div>

    <?php if ($v_weight || $v_age) { ?>
    <div class="row m-0 p-0">
      <?php if ($v_weight) { ?>
      <div class="col-md-6 p-0 pt-3">
        <label id="range-labelweight" for="inputWeight" class="m-0 p-0 pb-2">
          Poids
        </label>
        <div id="containersliderWeight" class="m-0 p-0 ps-2 pe-2 border" style="padding-top: 2px !important; padding-bottom: 2px !important;"></div>
        <script type="text/javascript">
          __sliderweight = createD3RangeSlider(<?php echo $v_weightmin;?>, <?php echo $v_weightmax;?>, "#containersliderWeight", false);

          __sliderweight.onChange(function(newRange) {
            d3.select("#range-labelweight").html("Poids : " + newRange.begin + " &mdash; " + newRange.end + " Kg");
            __sliderweight.rangeValue = newRange;
          });

          __sliderweight.range(<?php echo $v_weightmin;?>, <?php echo $v_weightmax;?>);
        </script>
      </div>
      <?php } ?>
      <?php if ($v_age) { ?>
      <div class="col-md-6 p-0 pt-3 ps-3">
            <label id="range-labelage" for="inputAge" class="m-0 p-0 pb-2">
                Age
            </label>

            <div id="containersliderAge" class="m-0 p-0 ps-2 pe-2 border" style="padding-top: 2px !important; padding-bottom: 2px !important;"></div>

            <script type="text/javascript">
                __sliderage = createD3RangeSlider(<?php echo $v_agemin;?>, <?php echo $v_agemax;?>, "#containersliderAge", false);

                __sliderage.onChange(function(newRange) {
                d3.select("#range-labelage").html("Age : " + newRange.begin + " &mdash; " + newRange.end + " ans");
                __sliderage.rangeValue = newRange;
                });

                __sliderage.range(<?php echo $v_agemin;?>, <?php echo $v_agemax;?>);
            </script>
      </div>
      <?php } ?>
    </div>
    <?php } ?>
    <?php if ($v_activity == true) { ?>
    <div class="row m-0 p-0 pt-3">
      <label id="range-labelactivity" for="inputActivity" class="pe-3">
        Poids :
      </label>
      <div id="containersliderActivity" class="m-0 p-0 ps-2 pe-2 border" style="padding-top: 2px !important; padding-bottom: 2px !important;"></div>
      <script type="text/javascript">
        __slideractivity = createD3RangeSlider(0, 3000, "#containersliderActivity", false);

        __slideractivity.onChange(function(newRange) {
          d3.select("#range-labelactivity").html("Activité (MBq) :" + newRange.begin + " &mdash; " + newRange.end );
          __slideractivity.rangeValue = newRange;
        });

        __slideractivity.range(0, 3000);
      </script>
    </div>
    <?php } ?>
    <div class="row m-0 p-0 pt-3 pb-3">
        <div class="col-md-3 m-0 p-0">
            <div class="form-group">
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" id="generateasync">
                    <label class="form-check-label" for="exampleCheck1" data-langkey="Asynchronous generation">Asynchronous generation</label>
                </div>
            </div>
        </div>
        <div class="col-md-3" style="display:none">
            <div class="form-group">
                <div class="form-check">
                    <input type="checkbox" class="form-check-input" id="sendbymail">
                    <label class="form-check-label" for="exampleCheck1">Send by mail</label>
                </div>
            </div>
        </div>
        
    </div>

    <div class="row w-100 m-0 p-0 pt-3 border-top">
        <div class="col-md-6">
            <button id="submitbtn" class="ximeo_button ximeo_app-color"><i class="fa fa-download"></i>
                Exporter...</button>
        </div>
    </div>

  </form>
  <div id="progressbar" class="progressinfinitebar" style="display :none">

    <div id="progressbarmsg">
      <?php echo $_WaitGenerateDownload;?>
    </div>
    <div id="progressbarbar">
      <div class="line"></div>
      <div class="subline inc"></div>
      <div class="subline dec"></div>
    </div>
  </div>
</body>
<script>
  init_layout();


  document.getElementById("submitbtn").addEventListener("click", 
    function(event) 
    {
        console.log ( "Export button clicked..." );

        event.preventDefault();

        var form = document.getElementById('formstudy');

        var fromdate = document.getElementById('inputFrom');
        fromdate.classList.remove('is-invalid');
        fromdate.classList.remove('is-valid');

        console.log ( "fromdate.value : " + fromdate.value );
        var formValidate = true;
        if (!isValidDate(fromdate.value)) 
        {
            fromdate.classList.add('is-invalid');
            fromdate.classList.add('was-validated');
            formValidate = false;
        }
        console.log ( "formValidate : " + formValidate );
        var todate = document.getElementById('inputTo');
        todate.classList.remove('is-invalid');
        todate.classList.remove('is-valid');
        if (!isValidDate(todate.value)) 
        {
            todate.classList.add('is-invalid');
            todate.classList.add('was-validated');
            formValidate = false;
        }

        if (formValidate == false) 
        {
            return false;
        }

        var generaratasync = document.getElementById('generateasync');

        var sendbymail = document.getElementById('sendbymail');

        var endpoint = '<?php echo $EODACS_ROOT;?>/SubFW/database/afcn//prepare_exportafcn.php';

        var params = {
      type: "<?php echo $_GET['type'];?>",
      <?php if ($v_masterdescstudy == true) { ?>
      mstuid: __solstudymaster.getSelectionID(),
      <?php } ?>
      <?php if ($v_masterdescserie == true) { ?>
      mserid: __solseriemaster.getSelectionID(),
      <?php } ?>
      modid: __solmodality.getSelectionID(),
      <?php if ($v_weight == true) { ?>
      weightmin: __sliderweight.rangeValue.begin,
      weightmax: __sliderweight.rangeValue.end,
      <?php } ?>
      <?php if ($v_age == true) { ?>
      agemin: __sliderage.rangeValue.begin,
      agemax: __sliderage.rangeValue.end,
      <?php } ?>
      <?php if ($v_activity == true) { ?>
      activitystart: __slideractivity.rangeValue.begin,
      activityend: __slideractivity.rangeValue.end,
      <?php } ?>
      <?php if ($v_exporttype == true) { ?>
      exportlist: __solexporttype.getSelectionID(),
      <?php } ?>
      from: fromdate.value,
      to: todate.value,
      generateasync: generaratasync.checked,
      sendbymail: sendbymail.checked
    }

    var url = endpoint + formatParams(params);
    var downloadurl = '<?php echo $EODACS_FOLDER;?>/eoDACS/Common/download_file.php';

    var progressmsg = document.getElementById('progressbarmsg');
    progressmsg.innerHTML = "<?php echo $_WaitGenerateDownload;?>";
    var progressbar = document.getElementById('progressbar');
    progressbar.style.display = 'block';
    var progressbarbar = document.getElementById('progressbarbar');
    progressbarbar.style.display = 'block';
    prepare_and_download(url, downloadurl);

    return true;
  });

  switchLanguage ( eoDACS.CurrentLanguage );
</script>

</html>
