<?php
ob_start();
include_once "../include/config.inc.php";	//Include config file;
$db = new ora();
$title="计划设置";
include_once "head.php";				//include head file
echo $hr;

$t = 1; //initial type to 1;
if($_REQUEST['doSubmit']) bom_op($_REQUEST['t']);  //operate when submit
if($_REQUEST['bomid'] and !$_REQUEST['doSubmit'])  {
    $bom = bom_data_by_id();
    $_REQUEST["bom"] = $bom['BOMNAME'];
    $_REQUEST["bomid"] = $bom['BOMID'];
    $_REQUEST["bomnote"] = $bom['FNOTE'];
    $_REQUEST["flowid"] = $bom['FLOWID'];
    $_REQUEST["ver"] = $bom['VER'];
    $t = 0;
}
?>
    <link rel="stylesheet" href="../lib/layui/css/layui.min.css">

<div id="mainContent">
    <h2><?php echo $title; ?></h2>
    <form name="formhead" id="formhead" method="post" enctype="multipart/form-data" >
        <div class="layui-inline">
        <input type="file" id="file" name="file" class="layui-btn">
        </div>
        <div class="layui-inline">
            <button type="button" class="layui-btn" id="btnUpload">
                <i class="layui-icon">&#xe67c;</i>EXCEL上传验证
            </button>
        </div>
    </form>
    <form name="form2" id="form2" method="post" >
        <div class="stay_center" id="fblock" style="display: none">
            <div style="margin-top:10px;" id="ftext" class="layui-col-md12"></div>
            <input type="hidden" name="fpath" id="fpath" value="">
            <div class="layui-inline">
                <input type="button" id="btnExcel" value="处理上传文件" class="layui-btn">
            </div>
        </div>
    </form>

<script type="text/javascript">
    $(function () {
        $("#btnUpload").click(function () {
            console.log("Upload button clicked!");
            var formData = new FormData($('#formhead')[0]);
            $.ajax({
                type: 'post',
                url: "http://192.168.10.21/cimv2/cim/bom/upload.php", //上传文件的请求路径必须是绝对路劲
                data: formData,
                cache: false,
                processData: false,
                contentType: false,
            }).success(function (data) {
                afterUpload(data);
            }).error(function () {
                alert("上传失败");
            });
        });
    });
    function afterUpload(data){
        var arr = $.parseJSON(data);
        if(arr['status']!=1) {
            alert(arr.msg);
            return false;
        }
        $("#file").val('');  //set input(file) to null
        $("#fblock").css("display","block");
        $("#ftext").html("文件上传成功！点击下面按钮开始执行<br />"+arr['fpath']+"<br />");
        $("#fpath").val(arr['fpath']);
    }
    $(function () {
        $("#btnExcel").click(function () {
            console.log("Process excel button clicked!");
            $("#btnExcel").attr("disabled",true);
            console.log("Button disabled!");
            var formData = new FormData($('#form2')[0]);
            $.ajax({
                type: 'post',
                url: "http://192.168.10.21/cimv2/cim/bom/excel.php", //上传文件的请求路径必须是绝对路劲
                data: formData,
                cache: false,
                processData: false,
                contentType: false,
            }).success(function (data) {
                excelProcess(data);
                $("#btnExcel").attr("disabled",false);
                console.log("Button enabled!");
                console.log("Done everything!");
            }).error(function () {
                alert("处理失败");
            });
        });
    });
    function excelProcess(data){
        console.log("Excel data upload successful!");
        console.log(data);
        var arr = $.parseJSON(data);
        var htmlmsg;
        for(i = 0;i < arr.msg.length;i++) htmlmsg += arr['msg'][i];
        console.log("htmlmsg proceed!");
        $("#ftext").html(htmlmsg);
    }
</script>
</div>
<?php
include_once "foot.php";
?>