<!-- Template for iV setup -->

<div style="margin-top:30px;">

<h1>i-V curve setup for Rig [% rig_info.name %]</h1>
<div class="content-container fit-content" style="margin-top:10px;">
<table class=ivsetup><tbody>

<tr><td  rowspan="14" style="padding:20px;"><img src="/celltest/logfilenew.png"></td><th>Current measuring simple channel:</th><td colspan=2>
<select name="current_label" onchange="change_val(this)" id="current_label" class="textfield">
<option value=""></option>
[% FOR i IN data.clabels -%]
<option value="[% i %]" [% IF data.current_label == i %]selected=selected[% END %]>[% i%]</option>
[% END -%]
</select>
</td></tr>
<tr><th>Voltage measuring simple channel:</th><td colspan=2>
<select name="voltage_label" onchange="change_val(this)" id="voltage_label" class="textfield">
<option value=""></option>
[% FOR i IN data.vlabels -%]
<option value="[% i %]" [% IF data.voltage_label == i %]selected=selected[% END %]>[% i%]</option>
[% END -%]
</select>
</td></tr>
<tr><th>Temperature log device:</th><td colspan=2>
<select name="temp_label" onchange="change_val(this)" id="temp_label" class="textfield">
<option value=""></option>
[% FOR i IN data.tlabels -%]
<option value="[% i %]" [% IF data.temp_label == i %]selected=selected[% END %]>[% i%]</option>
[% END -%]
</select></td></tr>
<tr><th>PSUs used for iV-curves:</th><td colspan=2>
<select name="iv_control_names" onchange="change_list(this)" id="iv_control_names" class="textfield" multiple=1>
[% FOR i IN data.psulabels -%]
<option value="[% i %]" [% IF data.iv_control_names.$i %]selected=selected[% END %]>[% i%]</option>
[% END -%]
</select>
</td></tr>
<tr><th>Current step 1:</th><td><input type="text" name="currentstep1" value="[% data.currentstep1 %]" onchange="change_val(this)" class="textfield2" id="currentstep1"></td><th> A </th><td>(min [% data.minimum_current_step %], max 1)</td></tr>
<tr><th>Current step 2:</th><td><input type="text" name="currentstep2" value="[% data.currentstep2 %]" onchange="change_val(this)" class="textfield2" id="currentstep2"></td><th> A </th><td>(min [% data.minimum_current_step %], max 3)</td></tr>
<tr><th>Current step 3:</th><td><input type="text" name="currentstep3" value="[% data.currentstep3 %]" onchange="change_val(this)" class="textfield2" id="currentstep3"></td><th> A </th><td>(min [% data.minimum_current_step %], max 0.5)</td></tr>
<tr><th>Current limit:</th><td><input type="text" name="currentlimit" value="[% data.currentlmit %]" onchange="change_val(this)" class="textfield2" id="currentlimit"></td><th>A</th><td> </td></tr>
<tr><th>Voltage limit (FC mode):</th><td><input type="text" name="voltagelimit_iv" value="[% data.voltage_limit_iv %]" onchange="change_val(this)" class="textfield2" id="voltagelimit_iv"></td><th> mV </th><td> (min [% data.miniv %], max [% data.maxiv %]) </td></tr>
<tr><th>Voltage limit (EC mode):</th><td><input type="text" name="electrolysis_limit_voltage" value="[% data.electrolysis_lmit_voltage %]" onchange="change_val(this)" class="textfield2" id="electrolysis_limit_voltage"></td><th> mV </th><td> (min [% data.miniv %], max [% data.maxiv %]) </td></tr>
<tr><th>Time between steps:</th><td><input type="text" name="ivpause" value="[% data.ivpause %]" onchange="change_val(this)" class="textfield2" id="ivpause"></td><th> s </th><td>(min 1, max 1000) </td></tr>
<tr><th>OCV measurements (start and end):</th><td><input type="text" name="OCV_measure_number" value="[% data.OCV_measure_number %]" onchange="change_val(this)" class="textfield2" id="OCV_measure_number"></td><th> </th><td> (min 2, max 10)</td></tr>
<tr><th>Current error limit:</th><td><input type="text" name="diff_current_limit" value="[% data.diff_current_limit %]" onchange="change_val(this)" class="textfield2" id="diff_current_limit"></td><th> A </th><td> </td></tr>
<tr><th>Allow caching durring i-V curves:</th><td>
<select onchange="change_val(this)" class="textfield2" id="allow_caching" name=allow_caching>
[% IF data.allow_caching.lower == 'yes' %]
<option value="No">No</option>
<option value="Yes" selected=selected>Yes</option>
[% ELSE%]
<option value="No" selected=selected>No</option>
<option value="Yes">Yes</option>
[% END %]
</select>
</td><td colspan=2>
<input type="button" name="show_caching" value="Show list of caching devices" onclick="run_ajax('rig_ajax_run.cgi','&action=list_caching',alert)" class="rfcbutton"></td></tr>
<tr><td colspan=2></td><td><button type=button class="rfcbutton textfield2"><i class="fa fa-check"></i> Save</button>
</table>

</div>
</div>


<style>

.ivsetup {
   margin:auto;
   text-align:left;
}

.ivsetup td, .ivsetup th {
   padding:5px;
}

.textfield {
   width:100%;
}

.textfield2 {
   width:70px;
}

</style>


<script>


function change_list(list)
{
    var n = list.name;
    var vals = new Array;;
    for (var i = 0; i < list.length; i++)
    {
        if (list.options[i].selected == true)
        {
	    vals.push(list.options[i].value);
        }
    }
    var val = vals.join(",");
    list.disabled = true;
    run_ajax('change_config.cgi','&action=change_config_value&section=IV_control&key='+n+'&value='+val,set_list);
}


function set_val(res)
{
    var arr = res.split("\n");
    if (arr[0] == "OK" || arr[0] == "WARN")
    {
        if (arr[0] == "WARN")
        {
            alert(arr[3]);
        }
        var obj = document.getElementById(arr[1]);
        obj.disabled = false;
        obj.value = arr[2];
    }
    else
    {
        alert(arr[0]);
    }
}

function change_val(element)
{
    var n = element.name;
    var val = element.value;
    element.disabled = true;
    run_ajax('change_config.cgi','&action=change_config_value&section=IV_control&key='+n+'&value='+val,set_val);
}



function change_list(list)
{
    var n = list.name;
    var vals = new Array;;
    for (var i = 0; i < list.length; i++)
    {
        if (list.options[i].selected == true)
        {
	    vals.push(list.options[i].value);
        }
    }
    var val = vals.join(",");
    list.disabled = true;
    run_ajax('change_config.cgi','&action=change_config_value&section=IV_control&key='+n+'&value='+val,set_list);
}


function set_list(res)
{
    var arr = res.split("\n");
    if (arr[0] == "OK" || arr[0] == "WARN")
    {
        if (arr[0] == "WARN")
        {
            alert(arr[3]);
        }
        var list = document.getElementById(arr[1]);
        list.disabled = false;
	var vals = new Array;
	var reg = "\s*\,\s*";
	vals = arr[2].split(reg);
	for (var i = 0; i < vals.length; i++)
	{
	    for (var j = 0; j < list.length; j++)
	    {
		if (list.options[j].value == vals[i])
		{
		    list.options[j].selected = true;
		}
	    }
	} 
    }
    else
    {
        alert(arr[0]);
    }
}


</script>