<?php
$editFlag = false;
if (isset($_GET['table']) && trim($_GET['table']) != '') {
    if (!in_array($_GET['table'], $this->tables)) {
        redirect('admin/table/index.php');
    }
    $editFlag = true;
}
?>
<div class="container" >
		<ol class="breadcrumb">
        	<li><a href="<?php echo strUrl('admin/dashboard'); ?>"><?php echo __LBL_DASHBOARD__;?></a></li>
        	<li class="active">User Manager: Tables</li>
      	</ol>
        <ul class="nav nav-tabs" id="auth_tab" style="margin-bottom: 10px;">
            <?php if ((int) $_SESSION['CRUD_AUTH']['group']['group_manage_flag'] == 1 || 
            		(int) $_SESSION['CRUD_AUTH']['group']['group_manage_flag'] == 3 ||
            		(int) $_SESSION['CRUD_AUTH']['user_manage_flag'] == 1 || 
            		(int) $_SESSION['CRUD_AUTH']['user_manage_flag'] == 3 ) { ?>
            <li><a href="<?php echo strUrl('admin/user/user.php'); ?>"> &nbsp; Users &nbsp; </a></li>
            <li><a href="<?php echo strUrl('admin/user/group.php'); ?>">Groups</a></li>
            <li ><a href="<?php echo strUrl('admin/user/permission.php'); ?>">Permissions</a></li>
          <?php } ?>
          <?php if ((int) $_SESSION['CRUD_AUTH']['group']['group_manage_flag'] == 2 || 
          			(int) $_SESSION['CRUD_AUTH']['group']['group_manage_flag'] == 3 ||
          			(int) $_SESSION['CRUD_AUTH']['user_manage_flag'] == 2 || 
          			(int) $_SESSION['CRUD_AUTH']['user_manage_flag'] == 3 ) { ?>
            <li  class="active"><a href="<?php echo strUrl('admin/table/index.php'); ?>">Tables</a></li>
          <?php } ?>
        </ul>
        <h3 style="margin-top: 7px;"><?php if ($editFlag == true) { ?>Edit<?php } else { ?>Create<?php } ?> table</h3>
        <div id="errors" style="display:none;"></div>
        <form class="form-horizontal" id="frm_new_table">
            <?php if ($editFlag == true) { ?>
                <input type="hidden" id="table_name_id" value="<?php echo $_GET['table']; ?>" />
            <?php } ?>
            <div class="form-group" style="margin-bottom: 8px !important;">
                <label class="col-lg-2 control-label" for="table_name">Table Name:</label>
                <div class="col-lg-4">
                    <input type="text" id="table_name" name="table_name" placeholder="Table Name" class="form-control" />
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 8px !important;">
                <label class="col-lg-2 control-label" for="storage_engine">Storage Engine:</label>
                <div class="col-lg-4">
                    <select id="storage_engine" name="storage_engine" class="form-control" >
                        <?php foreach ($this->engines as $k => $v) { ?>
                            <option value="<?php echo $k; ?>"><?php echo $v; ?></option>
                        <?php } ?>
                    </select>
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 8px !important;">
                <label class="col-lg-2 control-label" for="collation">Collation:</label>
                <div class="col-lg-4">
                    <select name="collation" id="collation" class="form-control" >
                        <option value=""></option>
                        <?php foreach ($this->collations as $k => $v) { ?>
                            <optgroup label="<?php echo $k; ?>">
                                <?php foreach ($v as $k1 => $v1) { ?>
                                    <option value="<?php echo $k1; ?>"><?php echo $v1; ?></option>
                                <?php } ?>
                            </optgroup>
                        <?php } ?>
                    </select>
                </div>
            </div>
            <div class="form-group" style="margin-bottom: 8px !important;">
                <label class="col-lg-2 control-label" for="table_comment">Table Comments:</label>
                <div class="col-lg-4">
                    <input type="text" id="table_comment" name="table_comment" placeholder="Comments" class="form-control" >
                </div>
            </div>
            <table class="table table-bordered list table-condensed" id="crud_table">
                <thead>
                    <tr>
                        <th style=" cursor:default; color:#333333;text-shadow: 0 1px 0 #FFFFFF;background-color: #e6e6e6;">Name</th>
                        <th style=" cursor:default; color:#333333;text-shadow: 0 1px 0 #FFFFFF;background-color: #e6e6e6;">Type</th>
                        <th style=" cursor:default; color:#333333;text-shadow: 0 1px 0 #FFFFFF;background-color: #e6e6e6;">Length/Values</th>
                        <th style=" cursor:default; color:#333333;text-shadow: 0 1px 0 #FFFFFF;background-color: #e6e6e6;">Default</th>
                        <th style=" cursor:default; color:#333333;text-shadow: 0 1px 0 #FFFFFF;background-color: #e6e6e6;">Collation</th>
                        <th style=" cursor:default; color:#333333;text-shadow: 0 1px 0 #FFFFFF;background-color: #e6e6e6;">Null</th>
                        <th style=" cursor:default; color:#333333;text-shadow: 0 1px 0 #FFFFFF;background-color: #e6e6e6;">Key</th>
                        <th style=" cursor:default; color:#333333;text-shadow: 0 1px 0 #FFFFFF;background-color: #e6e6e6;">A_I</th>
                        <th style=" cursor:default; color:#333333;text-shadow: 0 1px 0 #FFFFFF;background-color: #e6e6e6;">Action</th>
                    </tr>
                </thead>
                <tr style="display:none;" id="template">
                    <td style="width: 240px;">
                        <input type="text" name="field_name[]" placeholder="Field Name" class="form-control" />
                    </td>
                    <td style="width: 130px;">
                        <select name="field_type[]" style="width: auto;" onchange="resetCollation(this);" class="form-control" >
                            <option selected="selected" value="INT">INT</option>
                            <option value="VARCHAR">VARCHAR</option>
                            <option value="TEXT">TEXT</option>
                            <option value="DATE">DATE</option>
                            <optgroup label="NUMERIC">
                                <option value="TINYINT">TINYINT</option>
                                <option value="SMALLINT">SMALLINT</option>
                                <option value="MEDIUMINT">MEDIUMINT</option>
                                <option value="INT">INT</option>
                                <option value="BIGINT">BIGINT</option>
                                <option value="-">-</option>
                                <option value="DECIMAL">DECIMAL</option>
                                <option value="FLOAT">FLOAT</option>
                                <option value="DOUBLE">DOUBLE</option>
                                <option value="REAL">REAL</option>
                                <option value="-">-</option>
                                <option value="BIT">BIT</option>
                                <option value="BOOL">BOOL</option>
                                <option value="SERIAL">SERIAL</option>
                            </optgroup>
                            <optgroup label="DATE and TIME">
                                <option value="DATE">DATE</option>
                                <option value="DATETIME">DATETIME</option>
                                <option value="TIMESTAMP">TIMESTAMP</option>
                                <option value="TIME">TIME</option>
                                <option value="YEAR">YEAR</option>
                            </optgroup>
                            <optgroup label="STRING">
                                <option value="CHAR">CHAR</option>
                                <option value="VARCHAR">VARCHAR</option>
                                <option value="-">-</option>
                                <option value="TINYTEXT">TINYTEXT</option>
                                <option value="TEXT">TEXT</option>
                                <option value="MEDIUMTEXT">MEDIUMTEXT</option>
                                <option value="LONGTEXT">LONGTEXT</option>
                                <option value="-">-</option>
                                <option value="BINARY">BINARY</option>
                                <option value="VARBINARY">VARBINARY</option>
                                <option value="-">-</option>
                                <option value="TINYBLOB">TINYBLOB</option>
                                <option value="MEDIUMBLOB">MEDIUMBLOB</option>
                                <option value="BLOB">BLOB</option>
                                <option value="LONGBLOB">LONGBLOB</option>
                                <option value="-">-</option>
                                <option value="ENUM">ENUM</option>
                                <option value="SET">SET</option>
                            </optgroup>
                        </select>
                    </td>
                    <td style="width: 90px;">
                        <input type="text" name="field_length_value[]" style="width: 89%;" placeholder="Length" class="form-control" />
                    </td>
                    <td  style="width: 170px;">
                        <select name="field_default[]" style="width: auto;" onchange="default_field(this);" class="form-control" >
                            <option value=""></option>
                            <option value="NULL">NULL</option>
                            <option value="USER_DEFINED">As defined:</option>
                            <option value="CURRENT_TIMESTAMP">CURRENT_TIMESTAMP</option>
                        </select>
                        <input type="text" name="field_user_defined[]" style=" margin-top: 3px; width: 93%; display: none;" class="form-control" />

                    </td>
                    <td style="width: 185px;">
                        <select  name="field_collation[]" class="form-control" >
                            <option value=""></option>
                            <?php foreach ($this->collations as $k => $v) { ?>
                                <optgroup label="<?php echo $k; ?>">
                                    <?php foreach ($v as $k1 => $v1) { ?>
                                        <option value="<?php echo $k1; ?>"><?php echo $v1; ?></option>
                                    <?php } ?>
                                </optgroup>
                            <?php } ?>
                        </select>
                    </td>
                    <td style="width: 30px; text-align: center;">
                        <input type="checkbox" value="1" checked="checked" name="field_null[]">
                    </td>
                    <td style="width: 30px; text-align: center;">
                        <input type="checkbox" value="1" name="field_key[]"/>
                    </td>
                    <td style="width: 30px; text-align: center;">
                        <input type="checkbox" value="1" name="field_ai[]"/>
                    </td>
                    <td style="text-align: center;">
                        <a class="btn btn-info btn-xs" onclick="add_field(this);">Add</a>
                        <a class="btn btn-danger btn-xs" onclick="delete_field(this);">Delete</a>
                    </td>
                </tr>

            </table>
            <?php if ($editFlag == true){ ?>
                <p>A_I: AUTO_INCREMENT<br />
                Config of this table will be removed when save button is clicked.
                </p>
            <?php } ?>
            <div style="text-align: center;">
                <input type="button" class="btn btn-default" value="Cancel" onclick="cancel();"/>
                <?php if ($editFlag == true) { ?>
                    <input type="button" class="btn btn-info" value=" Save " onclick="updateTable();" />
                <?php } else { ?>
                    <input type="button" class="btn btn-info" value=" Save " onclick="createNewTable();" />
                <?php } ?>
            </div>
        </form>
        <footer>
	        <hr />
            <p><?php echo __LBL_COPYRIGHT__; ?></p>
        </footer>
</div>

<script>
    function add_field(obj){
        var _field = $('#template').clone();
        _field.attr('id', '');
        _field.addClass('crud_field');
        _field.show();
        $(obj).parent().parent().after(_field);
    }
    function delete_field(obj){
        if ($('.crud_field').length > 1){
            $(obj).parent().parent().remove();
        }
    }
    function default_field(obj){
        var target = $(obj).parent().children('input[name="field_user_defined\[\]"]');
        if ($(obj).val() == 'USER_DEFINED'){
            target.show();
        }else{
            target.hide();
        }
    }
    function cancel(){
        window.location = '<?php echo strUrl('admin/table/index.php'); ?>';
    }
    function resetCollation(obj){
        var _field = $(obj).parent().parent();
        _field.find('select[name="field_collation\[\]"]').val('');
    }
    function createNewTable(){
        var obj = {};
        obj.table_name = $('#table_name').val();
        obj.storage_engine = $('#storage_engine').val();
        obj.collation = $('#collation').val();
        obj.table_comment = $('#table_comment').val();
        obj.fields = [];
        $('.crud_field').each(function(){
            var o = {};
            o.name = $(this).find('input[name="field_name\[\]"]').val();
            o.type = $(this).find('select[name="field_type\[\]"]').val();
            o.length_value = $(this).find('input[name="field_length_value\[\]"]').val();
            o.def = $(this).find('select[name="field_default\[\]"]').val();
            o.user_def = $(this).find('input[name="field_user_defined\[\]"]').val();
            o.collation = $(this).find('select[name="field_collation\[\]"]').val();
            o.is_null = $(this).find('input[name="field_null\[\]"]:checked').val();
            o.key = $(this).find('input[name="field_key\[\]"]:checked').val();
            o.ai = $(this).find('input[name="field_ai\[\]"]:checked').val();
            
            obj.fields[obj.fields.length] = o;
        });
        
        $.post('<?php echo strUrl('admin/table/insert.php'); ?>', obj, function(o){
            if (o.error == 1){
                var objError = $('<div class="alert alert-danger"></div>');
                objError.append('<button data-dismiss="alert" class="close" type="button">×</button>');
                if (o.messages.length >0){
                    for(var i in o.messages){
                        objError.append('<strong>Error!</strong> '+o.messages[i]+'. <br/>');
                    }
                }
                $('#errors').html(objError);
                $('#errors').show();
            }else{
                window.location = '<?php echo strUrl('admin/table/index.php'); ?>';
            }
        }, 'json');
    }
    
    function updateTable(){
        var obj = {};
        obj.table_name = $('#table_name').val();
        obj.table_name_id = $('#table_name_id').val();
        obj.storage_engine = $('#storage_engine').val();
        obj.collation = $('#collation').val();
        obj.table_comment = $('#table_comment').val();
        obj.fields = [];
        $('.crud_field').each(function(){
            var o = {};
            o.id = $(this).attr('id');
            o.name = $(this).find('input[name="field_name\[\]"]').val();
            o.type = $(this).find('select[name="field_type\[\]"]').val();
            o.length_value = $(this).find('input[name="field_length_value\[\]"]').val();
            o.def = $(this).find('select[name="field_default\[\]"]').val();
            o.user_def = $(this).find('input[name="field_user_defined\[\]"]').val();
            o.collation = $(this).find('select[name="field_collation\[\]"]').val();
            o.is_null = $(this).find('input[name="field_null\[\]"]:checked').val();
            o.key = $(this).find('input[name="field_key\[\]"]:checked').val();
            o.ai = $(this).find('input[name="field_ai\[\]"]:checked').val();
            
            obj.fields[obj.fields.length] = o;
        });
        
        $.post('<?php echo strUrl('admin/table/update.php'); ?>', obj, function(o){
            if (o.error == 1){
                var objError = $('<div class="alert alert-danger"></div>');
                objError.append('<button data-dismiss="alert" class="close" type="button">×</button>');
                if (o.messages.length >0){
                    for(var i in o.messages){
                        objError.append('<strong>Error!</strong> '+o.messages[i]+'. <br/>');
                    }
                }
                $('#errors').html(objError);
                $('#errors').show();
            }else{
                window.location = '<?php echo strUrl('admin/table/index.php'); ?>';
            }
        }, 'json');
    }
    
    $(document).ready(function(){
        $('title').html($('h3').text());
        
<?php if ($editFlag == true) { ?>
            $('#table_name').val('<?php echo $this->table_info['Name']; ?>');
            $('#storage_engine').val('<?php echo $this->table_info['Engine']; ?>');
            $('#collation').val('<?php echo $this->table_info['Collation']; ?>');
            $('#table_comment').val('<?php echo $this->table_info['Comment']; ?>');
    <?php if (is_array($this->columns_info) && count($this->columns_info) > 0) { ?>
        <?php foreach ($this->columns_info as $k => $v) { ?>
            <?php
            $aryType = explode('(', $v['Type']);
			preg_match("/\((.*)\)/i", $v['Type'],$aryTmp);
			$valType = (isset($aryTmp[1]))?$aryTmp[1]:'';
            ?>
                                var _field = $('#template').clone();
                                _field.attr('id', '<?php echo $v['Field'] ?>');
                                _field.addClass('crud_field');
                                _field.find('input[name="field_name\[\]"]').val('<?php echo $v['Field'] ?>');
                                _field.find('select[name="field_type\[\]"]').val('<?php echo strtoupper($aryType[0]); ?>');
            <?php if (isset($aryType[1])) { ?>
                                    _field.find('input[name="field_length_value\[\]"]').val('<?php echo $valType; ?>');
            <?php } ?>
            <?php if ($v['Default'] === NULL) { ?>
                <?php if ($v['Null'] == 'YES') { ?>
                                            _field.find('select[name="field_default\[\]"]').val('NULL');
                <?php } ?>
            <?php } else if ($v['Default'] == 'CURRENT_TIMESTAMP') { ?>
                                    _field.find('select[name="field_default\[\]"]').val('<?php echo $v['Default'] ?>');
            <?php } else { ?>
                                    _field.find('select[name="field_default\[\]"]').val('USER_DEFINED');
                                    _field.find('input[name="field_user_defined\[\]"]').val('<?php echo $v['Default'] ?>');
                                    _field.find('input[name="field_user_defined\[\]"]').show();
            <?php } ?>
                                _field.find('select[name="field_collation\[\]"]').val('<?php echo $v['Collation'] ?>');
            <?php if ($v['Null'] == 'NO') { ?>
                                    _field.find('input[name="field_null\[\]"]').attr({checked:false});
            <?php } ?>
            <?php if ($v['Key'] == 'PRI') { ?>
                                    _field.find('input[name="field_key\[\]"]').attr({checked:true});
            <?php } ?>
            <?php if ($v['Extra'] == 'auto_increment') { ?>
                                    _field.find('input[name="field_ai\[\]"]').attr({checked:true});
            <?php } ?>
                                            
                                _field.show();
                                $('#crud_table').append(_field);
        <?php } ?>
    <?php } ?>
                                                    
<?php } ?>
    
        if ($('.crud_field').length <= 0){
            var _field = $('#template').clone();
            _field.attr('id', '');
            _field.addClass('crud_field');
            _field.find('input[name="field_name\[\]"]').val('id');
            _field.find('input[name="field_null\[\]"]').attr({checked:false});
            _field.find('input[name="field_key\[\]"]').attr({checked:true});
            _field.find('input[name="field_ai\[\]"]').attr({checked:true});
            _field.show();
            $('#crud_table').append(_field);
        }
    
    });
</script>