<script language="javascript">

    function batch_update_amp(index) {
        var ic_id = $('#cache_ic_id_' + index).val();
        var msg_obj = "#cache" + index;
        var amp_code = '';
        $(msg_obj).html("\u7a0d\u7b49...");

        $.ajax({
            type: "get",
            dataType: "json",
            url: "<?php echo site_url('amp/auto_create/') ?>/" + ic_id,
            success: function (result, textStatus) {
				//当result为空的时候跳过
				if(result.length == 0 || result =='' || undefined || null){
					$(msg_obj).html("<a href='javascript:void(0);' onclick='create_amp_data(" + index + ");' >发生错误，请重试</a>");
					create_amp_data(++index);
					return;
				}
				
                $.each(result, function (i, field) {
                    if (i === 'data') {
                        amp_code = field.amp;
                    }
                });

                $(msg_obj).html("\u0041\u004d\u0050\u8f6c\u6362\u6210\u529f\u0021\u6b63\u5728\u4fdd\u5b58\u2026\u2026");
                //保存转换的AMP代码 begin
                $.ajax({
                    type: "post",
                    dataType: "json",
                    url: "<?php echo site_url('amp/edit_save/') ?>",
                    data: {
                        "ic_id": ic_id,
                        "textarea_htmlcode": amp_code,
                        "amp_status": 1
                    },
                    success: function (result, textStatus) {
                        if (result.name === 'no') {
                            error_msg='';
                            $.each(result.value, function (index, element) {
                                error_msg=error_msg+'line '+element.line+' col '+element.col+' :'+element.message+'&#10;';
                              }); 
                            $(msg_obj).html("保存失败，AMP语法错误!"+'<i class="glyphicon glyphicon-education" title="'+error_msg+'"></i>');
                        }else{
                            $(msg_obj).html("\u0041\u004d\u0050\u66f4\u65b0\u6210\u529f\u0021");
                        }
                        create_amp_data(++index);
                    },
                    error: function () {
                        $(msg_obj).html("<a href='javascript:void(0);' onclick='create_amp_data(" + index + ");' >发生错误，请重试</a>");
                        create_amp_data(++index);
                    }
                });
                //保存转换的AMP代码 end
            },
            error: function () {
                $(msg_obj).html("<a href='javascript:void(0);' onclick='create_amp_data(" + index + ");' >发生错误，请重试</a>");
                create_amp_data(++index);
            }
        });
    }

    function create_amp_data(index) {
        if ($('#cache_url' + index).length > 0) {
            if ($('#cache' + index).html() !== '\u0041\u004d\u0050\u66f4\u65b0\u6210\u529f\u0021') {
                if ($('#cache' + index).html() == '手动管理，不会自动更新') { //手动更新的项目跳过
                    create_amp_data(++index);
                } else {
					setTimeout("batch_update_amp("+index+")",5000);
                }
            }
        }
    }


</script>

<div class="container-fluid">
    <div class="row">
        <div class="col-sm-4">

        </div>

        <div class="col-sm-16">

            <table class="table table-striped">
                <thead>
                    <tr>
                        <th class="col-sm-2">#</th>
                        <th class="col-sm-2">发布</th>
                        <th class="col-sm-14">URL</th>
                        <th class="col-sm-6">更新进度</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $i = 0;
                    foreach ($all_information as $key => $info) {
                        if ($info->ic_status == 1 && !empty($info->ic_url)) {
                            $i++;
                            ?>
                            <tr>
                                <td><a href="<?php echo site_url('amp/edit/'.$info->ic_id); ?>" target="_blank"><?php echo $i; ?></a></td>
                                <td>
                                    <?php if($info->amp_status==='1'){echo '<i class="glyphicon glyphicon-flash"></i>'; } ?>
                                    <?php if($info->amp_status==='2'){echo '<i class="glyphicon glyphicon-hand-up" title="手动管理，不会自动更新"></i>'; } ?>
                                </td>
                                <td id="cache_url<?php echo $i; ?>" name="cache_url<?php echo $i; ?>" class="cache_url"><a href="<?php echo site_url('information/edit/'.$info->is_id) ?>" target="_blank"><?php echo $info->ic_url; ?></a></td>
                                <td id="cache<?php echo $i; ?>" name="cache<?php echo $i; ?>"><?php if($info->amp_status==='2'){echo '手动管理，不会自动更新'; } ?></td>
                        <input type="hidden" id="cache_ic_id_<?php echo $i; ?>" value="<?php echo $info->ic_id; ?>" />
                        </tr>
                        <?php
                    }
                }
                ?> 
                </tbody>
            </table>
        </div>

        <div class="col-sm-4">
            <a href="javascript:void(0);" class="btn btn-danger" onclick="create_amp_data(1);">开始更新</a>
        </div>


    </div>

</div>
