
<script>
    var from_name = "<?php echo e($product->created_from); ?>"
    console.log(from_name)
    layui.use(['upload','form'],function () {
        var form = layui.form;
        var upload=layui.upload;


        //logo上传
        var uploadInst = upload.render({
            elem: '#logo'
            ,url: '/uploadImage'
            ,size:1024
            ,data:{"_token":"<?php echo e(csrf_token()); ?>"}
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#logoImg').attr('src', result); //图片链接（base64）
                    $('#logoRemove').show();
                });
                $('#logoRemove').on('click',function () {
                    $('#logoImg').attr('src',null);
                    $('#logoRemove').hide();
                    $('input[name="logo"]').val('');
                });
            }
            ,done: function(res){
                if(res.code === 0){
                    $('input[name="logo"]').val(res.url);
                }else {
                    return layer.msg('上传失败');
                }
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#logoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });
        form.verify({
            logo: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!value){
                    return '请上传logo';
                }
            },
            repay_min:function(value,item){
                if(value<1 || value>999){
                    return '取值范围为1~999';
                }
            },
            repay_max:function(value,item){
                if(value<1 || value>999){
                    return '取值范围为1~999';
                }
            },
            quota_min:function(value,item){
                if(value<0 || value>999999){
                    return '取值范围为0~999999';
                }
            },
            required:function (value,item) {
                if(!value){
                    var tagName=$(item)[0].tagName;
                    if(tagName==='INPUT' || tagName==='TEXTAREA'){
                        var str=$(item).closest('.layui-form-item').find('label').html();
                        var index=str.lastIndexOf("\>");
                        var label=str.substring(index+1,str.length);
                        var msg=label+'不能为空';
                    }
                    if(tagName==='SELECT'){
                        var msg=$(item).find('option:first-child').text();
                    }
                    return msg;
                }
            }
        });

        $('input[type="number"]').on('keyup',function () {

            if($(this).attr('name')!=='sort'){
                if($(this).val()<0){
                    $(this).val('');
                }
            }
        });

        function checkLabel(){
            var checked=$('.product_label:checked').length;
            if(checked>=3){
                $('.product_label').not('input:checked').each(function () {
                    $(this).attr("disabled",true);
                })
            }else {
                $('.product_label').not('input:checked').each(function () {
                    $(this).attr("disabled",false);
                })
            }
        }

        function checkCategory(){
            var checked=$('.product_category:checked').length;
            if(checked>=4){
                $('.product_category').not('input:checked').each(function () {
                    $(this).attr("disabled",true);
                })
            }else {
                $('.product_category').not('input:checked').each(function () {
                    $(this).attr("disabled",false);
                })
            }
        }

        function checkColumn(){
            var checked=$('.product_column:checked').length;
            if(checked>=4){
                $('.product_column').not('input:checked').each(function () {
                    $(this).attr("disabled",true);
                })
            }else {
                $('.product_column').not('input:checked').each(function () {
                    $(this).attr("disabled",false);
                })
            }
        }
        checkLabel();
        checkCategory();
        checkColumn();

        form.on('checkbox(label)', function(data){
            console.log('aaa00')
            checkLabel();
        });
        
        form.on('checkbox(category)', function(data){
            checkCategory();
        });
        
        form.on('checkbox(column)', function(data){
            checkColumn();
        });
        form.on('radio(resource_type)', function(data){
            if(data.value == 1){//软文
                $('.article').show().siblings('.wechat-sina').hide().siblings('.video').hide().siblings('.wechat-sina-video').hide().siblings('.zimeiti').hide()
            }else if(data.value == 2 || data.value == 3 || data.value == 5 || data.value == 6){//公众号||小红书||自媒体
                $('.article').hide().siblings('.wechat-sina').show().siblings('.video').hide().siblings('.wechat-sina-video').show().siblings('.zimeiti').hide()
                if(data.value == 6){
                    $('.zimeiti').show()
                }else{
                    $('.zimeiti').hide()
                }
            }else if(data.value == 4){//视频
                $('.article').hide().siblings('.wechat-sina').hide().siblings('.video').show().siblings('.wechat-sina-video').show().siblings('.zimeiti').hide()
            }


            if(data.value == 1  || data.value == 6)
            {
                $(".atricle_zimeiti") .show();
                $("input[name=success_rate]").attr('lay-verify','required|number|success_rate');
                $("input[name=fast_lend_unit ]").attr('lay-verify','required');
                $("input[name=fast_lend_value]").attr('lay-verify','required|fast_lend_value');
                $("input[name=slogan]").attr('lay-verify','required');
            }else{
                //

                $("input[name=success_rate]").removeAttr('lay-verify');
                $("input[name=fast_lend_unit ]").removeAttr('lay-verify');
                $("input[name=fast_lend_value]").removeAttr('lay-verify');
                $("input[name=slogan]").removeAttr('lay-verify');
                $(".atricle_zimeiti") .hide();

                $("input[name=success_rate]").val('')
                $("input[name=fast_lend_value]").val('')
                $("input[name=fast_lend_unit]").val('')
            }




            if(data.value == 1  || data.value == 6)
            {
                $("#one_price").text('价格')
                $(".other_cate").hide();


            }else if(data.value == 3  || data.value == 2)
            {
                $("#one_price").text('硬广价格')
                $(".other_cate").show();

            }else if(data.value == 5)
            {
                $("#one_price").text('笔记价格')
                $("#two_price").text('视频价格')
                $(".other_cate").show();

            }else if(data.value == 4)
            {
                $("#one_price").text('预约价格')
                $(".other_cate").hide();

            }
            if(data.value == 1 )
            {
                $(".article_attribute").show()
            }else{
                $(".article_attribute").hide()
            }
        });
        form.on('submit(save)', function(data){
            if($('#platform').find('.layui-form-checked').length===0){
                layer.msg('请选择上线平台',{icon:2});
                return false;
            }
            $.post($('#myForm').attr('action'),data.field,function (res) {
                if(res.code===0){
                    layer.msg(res.msg,{icon:1});
                    setTimeout(function () {
                        if(from_name == 'cloud')
                        {
                            window.location.href="<?php echo e(route('admin.product.cloud')); ?>";
                        }else if(from_name == 'media'){
                            window.location.href="<?php echo e(route('admin.product.media')); ?>";
                        }else {
                            window.location.href="<?php echo e(route('admin.product')); ?>";
                        }
                    },1500);
                }else {
                    layer.msg(res.msg,{icon:2});
                }
            }).error(function (data) {
                $.each(data.responseJSON.errors,function (key,value) {
                    layer.msg(value[0],{icon:2});
                    return false;
                })
            });
            return false;
        });

    });

    function words_deal()
    {
        var curLength=$('textarea[name="apply_condition"]').val().length;
        if(curLength>999)
        {
            var num=$('textarea[name="apply_condition"]').val().substr(0,999);
            $('textarea[name="apply_condition"]').val(num);
        }
        else
        {
            $("#textCount").text($('textarea[name="apply_condition"]').val().length);
        }
    }


</script>

