createDevice.php 16.2 KB
<?php

use yii\helpers\Url;
use app\ht\helpers\CssFiles;

$this->title = '新建序列号';
$this->params['breadcrumbs'][] = '序列号管理';
$this->params['breadcrumbs'][] = ['label' => '序列号列表', 'url' => ['/device/device/index']];
$this->params['breadcrumbs'][] =  $this->title;

CssFiles::register($this, 'exts/showimg/css/showimg.css');

?>
<style>
 .create-div .form-group{padding:1rem 0}
 .create-div .full-width{width:100%;}
 .combo-select{max-width:100% !important;}
    .source-cls{padding:1rem 0.5rem; box-sizing: border-box;}
 .source-cls .add_way,
 .source-cls .append_way{margin-left:1rem;}
 .source-cls .upload-cls{position: relative;width:100px;display: inline-block;cursor: pointer}
 .source-cls .upload-cls-txt{position: absolute;top:0;left:0;width: 100%;display: inline; padding: 0.5rem 0;text-align: center;}
 .source-cls .file-upload-cls{background-color:rgba(0,0,0,0);opacity: 0;max-width: 100px;position: absolute;top: 0;left: 0;z-index: 222;padding: 0.5rem;}
</style>
<div class="panel panel-default">
    <div class="panel-body form-inline create-div">

            <ul class="nav nav-tabs" role="tablist">
                <li role="presentation" class="active"><a href="#new" aria-controls="new" role="tab" data-toggle="tab">新创建</a></li>
                <li role="presentation"><a href="#append" aria-controls="append" role="tab" data-toggle="tab">追加创建</a></li>
            </ul>

            <!-- Tab panes -->
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="new">
                    <form action="" method="get" id="create-form" class="filter-form">
                        <div class="form-group col-sm-12">
                            <label for="manufacture" class="col-sm-4 control-label text-right">厂商:</label>
                            <div class="col-sm-4 form-inline">
                                <select class="form-control full-width select-cls" id="manufacture" name="manufacture">

                                </select>

                            </div>
                            <div class="col-sm-4"><a href="<?=Url::toRoute('/manufacturer/manufacturer/create')?>">+创建厂商</a></div>
                        </div>
                        <div class="form-group col-sm-12">
                            <label for="project" class="col-sm-4 control-label text-right">项目名:</label>
                            <div class="col-sm-4 form-inline">
                                <select class="form-control full-width select-cls" id="project" name="project">

                                </select>
                            </div>
                            <div class="col-sm-4"><a href="<?=Url::toRoute('/project/project/create')?>">+创建项目</a></div>

                        </div>
                        <div class="form-group col-sm-12">
                            <label for="model" class="col-sm-4 control-label text-right">设备型号:</label>
                            <div class="col-sm-4 form-inline">
                                <select class="form-control full-width select-cls" id="model" name="model">

                                </select>
                            </div>
                            <div class="col-sm-4"><a href="<?=Url::toRoute('/model/model/create')?>">+创建型号</a></div>
                        </div>
                        <div class="form-group col-sm-12">
                            <label for="production" class="col-sm-4 control-label text-right">生产日期:</label>
                            <div class="col-sm-4 form-inline">
                                <select class="form-control full-width select-cls" id="production" name="production">

                                </select>
                            </div>
                            <div class="col-sm-4"><a href="<?=Url::toRoute('/production/production/create')?>">+生产日期</a></div>
                        </div>
                        <div class="form-group col-sm-12">
                            <label for="batch_no" class="col-sm-4 control-label text-right">编码预览:</label>
                            <div class="col-sm-4 form-inline">
                                <span id="preview_batch_no">----</span>
                            </div>
                        </div>
                        <div class="form-group col-sm-12">
                            <label for="production" class="col-sm-4 control-label text-right">来源:</label>
                            <div class="col-sm-4 form-inline">
                                <div class="source-cls" style="padding-top: 0;">
                                    自动生成
                                    <input type="number" class="form-control" style="max-width: 100px;" id="num" name="num" value="" placeholder="例如:20" autocomplete="off">
                                    <input type="radio" class="form-control add_way" name="add_way" value="1"  checked/>
                                </div>

                                <div class="source-cls">
                                    导入文件
                                    <span class="upload-cls form-control" ><input type="file" style="max-width: 100px;" class="file-upload-cls" id="file_num" name="file" disabled><span class="upload-cls-txt">点击上传</span></span><input type="radio" class="form-control add_way" name="add_way" value="2" />
                                    <input type="hidden" value="" id="upload_path"> <span style="color:#cc0000">(格式是txt,每行一个mac地址,5M以内)</span>
                                </div>
                
                            </div>
                        </div>

                        <div class="form-group col-sm-12" style="text-align: center;">
                            <div><button type="submit" class="btn btn-primary font-1" id="createDeviceBtn">创建</button></div>
                        </div>
                    </form>

                </div>

                <div role="tabpanel" class="tab-pane" id="append">
                    <form action=""  id="append-form" class="filter-form">
                        <div class="form-group col-sm-12">
                            <label for="batch_id" class="col-sm-4 control-label text-right">批次编码:</label>
                            <div class="col-sm-4 form-inline">
                                <select class="form-control full-width batch-select-cls" id="batch_id" name="batch_id"></select>
                            </div>
                        </div>
                        <div class="form-group col-sm-12">
                            <label for="append_num" class="col-sm-4 control-label text-right">来源:</label>
                            <div class="col-sm-4 form-inline">

                                <div class="source-cls" style="padding-top: 0;">
                                    追加生成
                                    <input type="number" class="form-control" style="max-width: 100px;" id="append_num" name="append_num" value="" placeholder="例如:20" autocomplete="off">
                                    <input type="radio" class="form-control append_way" name="append_way" value="1"  checked/>
                                </div>

                                <div class="source-cls">
                                    导入文件
                                    <span class="upload-cls form-control" ><input type="file" style="max-width: 100px;" class="file-upload-cls" id="append_file_num" name="file" disabled><span class="upload-cls-txt">点击上传</span></span><input type="radio" class="form-control append_way" name="append_way" value="2" /> <span style="color:#cc0000">(格式是txt,每行一个mac地址,5M以内)</span>
                                    <input type="hidden" value="" id="append_upload_path">
                                </div>

                            </div>
                        </div>

                        <div class="form-group col-sm-12" style="text-align: center;">
                            <div><button type="submit" class="btn btn-primary font-1" id="appendDeviceBtn">追加创建</button></div>
                        </div>
                    </form>
                </div>

            </div>



    </div>

</div>
<script src="<?= Yii::$app->request->baseUrl . "/exts/base/1.0.0/ui/ajaxfileupload/ajaxfileupload-1.0.0.js"?>"></script>
<script src="<?= Yii::$app->request->baseUrl . "/exts/combo-select/js/jquery.combo.select.js"?>"></script>
<link rel="stylesheet" type="text/css" href="<?= Yii::$app->request->baseUrl . "/exts/combo-select/css/combo.select.css"?>" />
<script type="text/javascript">
    var searchItemUrl = "<?=Url::toRoute('/device/device/batch-item')?>";
    var saveUrl = "<?=Url::toRoute('/device/device/do-create-device')?>";
    var getBatchSelectUrl = "<?=Url::toRoute('/device/device/get-batch-select')?>";
    var appendSerialUrl = "<?=Url::toRoute('/device/device/append-serial-no')?>";
    var upLoadURL = "<?=Url::toRoute('/device/device/upload-mac')?>";
$(function() {

    function selectAll(id) {
        $.post(searchItemUrl, {type:id}, function(res) {
            if(res.list.length > 0) {
                var wrapper = '<option value="">请选择</option>';
                $.each(res.list, function(i,n){
                    wrapper = wrapper + '<option value="'+ n.uid+'">'+ n.name+'</option>';
                })
                $('#'+id).html(wrapper);
                $('#'+id).comboSelect();
            }

        }, 'json')

    }
    selectAll('manufacture');
    selectAll('project');
    selectAll('model');
    selectAll('production');

    // 追加选批次
    function getBatchSelectList() {
        $.post(getBatchSelectUrl, {type:0}, function(res) {
            if(res.list.length > 0) {
                var wrapper = '<option value="">请选择</option>';
                $.each(res.list, function(i,n){
                    wrapper = wrapper + '<option value="'+ n.id+'">'+ n.batch_no+' | '+ n.manufacture + '-' + n.project +'-'+ n.model +'</option>';
                })
                $('#batch_id').html(wrapper);
                $('#batch_id').comboSelect();
            }

        }, 'json')
    }
    getBatchSelectList();

    $('.select-cls').change(function(e){
        var manufacture = $('#manufacture').val();
        var project = $('#project').val();
        var model = $('#model').val();
        var production = $('#production').val();
        var manufactureArr = manufacture.split('_');
        var projectArr = project.split('_');
        var modelArr = model.split('_');
        var productionArr = production.split('_');
        if (undefined !== manufactureArr[1]) {
            manufacture = manufactureArr[1];
        } else {
            manufacture = '-';
        }

        if (undefined !== projectArr[1]) {
            project = projectArr[1];
        } else {
            project = '-';
        }

        if (undefined !== modelArr[1]) {
            model = modelArr[1];
        } else {
            model = '-';
        }
        if (undefined !== productionArr[1]) {
            production = productionArr[1];
        } else {
            production = '-';
        }
        $('#preview_batch_no').html(manufacture+ project+ model + production +'');
    })

    $('#createDeviceBtn').click(function(e) {
        e.preventDefault();
        var manufacture = $('#manufacture').val();
        if ('' == manufacture) {
            alert('请选择厂商');
            return false;
        }
        //manufacture = manufacture.split('_');

        var project = $('#project').val();
        if ('' == project) {
            alert('请选择项目');
            return false;
        }
        //project = project.split('_');

        var model = $('#model').val();
        if ('' == model) {
            alert('请选择型号');
            return false;
        }
        //model = model.split('_');

        var production = $('#production').val();
        if ('' == production) {
            alert('请选生产日期');
            return false;
        }

        var type = $('.add_way:checked').val();
        var num = $('#num').val();
        if (1 == type) {
            var par = /^[0-9]+$/;
            if (par.test(num) && (num > 0)) {
                // 合法的
            } else {
                alert('请录入大于0的整数');
                return false;
            }
        }
        var uploadPath = $('#upload_path').val();
        if (2 == type) {
            if ("" == uploadPath || null == uploadPath) {
                alert('请上传文件');
                return false;
            }
        }

        var params = {
            manufacture: manufacture,
            project: project,
            model: model,
            production: production,
            num: num,
            type:type,
            upload_path:uploadPath
        }
        $.post(saveUrl, params, function(res) {
            if (!res.success) {
                alert(res.message);
                return false;
            }
            alert(res.message);
            window.location.href = '<?=Url::toRoute('/datas/device/index')?>'
        }, 'json')
    })

    $('#appendDeviceBtn').click(function(e){
        var batch_id = $('#batch_id').val();
        var append_num = $('#append_num').val();
        if ('' == batch_id || 0 == batch_id) {
            alert('请选择批次');
            return false;
        }

        var type = $('.append_way:checked').val();
        if (1 == type) {
            var par = /^[0-9]+$/;
            if (par.test(append_num) && (append_num > 0)) {
                // 合法的
            } else {
                alert('请录入大于0的整数');
                return false;
            }
            if (append_num*1 > 3000) {
                alert('追加数量不要超过3000');
                return false;
            }
        }
        var uploadPath = $('#append_upload_path').val();
        if (2 == type) {
            if ("" == uploadPath || null == uploadPath) {
                alert('请上传文件');
                return false;
            }
        }

        $.post(appendSerialUrl,{
            batch_id:batch_id,
            append_num:append_num,
            type:type,
            upload_path:uploadPath
        }, function(ajaxRes){
            if (ajaxRes.success) {
                alert(ajaxRes.message);
                window.location.href = '<?=Url::toRoute('/datas/device/index')?>'
            } else {
                alert(ajaxRes.message);
            }
        }, 'json')
    })

    $('.add_way').click(function(e){
        var type = $(this).val();
        if (1 == type) {
            $('#num').removeAttr('disabled');
            $('#file_num').prop('disabled', true);
        } else {
            $('#file_num').removeAttr('disabled');
            $('#num').prop('disabled', true);
        }
    })

    $('#file_num').change(function(){
        $.ajaxFileUpload({
            url: upLoadURL,
            fileElementId: "file_num",
            dataType: "json",
            success: function (res) {
                if (res.success) {
                    $('#upload_path').val(res.path);
                    alert('成功上传');
                } else {
                    $('#upload_path').val('');
                    alert(res.message);
                }
            },
        });
    })

    $('.append_way').click(function(e){
        var type = $(this).val();
        if (1 == type) {
            $('#append_num').removeAttr('disabled');
            $('#append_file_num').prop('disabled', true);
        } else {
            $('#append_file_num').removeAttr('disabled');
            $('#append_num').prop('disabled', true);
        }
    })

    $('#append_file_num').change(function(){
        $.ajaxFileUpload({
            url: upLoadURL,
            fileElementId: "append_file_num",
            dataType: "json",
            success: function (res) {
                if (res.success) {
                    $('#append_upload_path').val(res.path);
                    alert('成功上传');
                } else {
                    $('#append_upload_path').val('');
                    alert(res.message);
                }
            },
        });
    })
})
</script>