{include file="app/manage/view/index/emlog_nav"}

<!-- 页面标题 -->
<h1 class="page-title">
    <i class="fas fa-images"></i>
    编辑广告
    <a href="index.php?r=manage/ad/huan" class="btn btn-sm btn-outline-secondary float-right">
        <i class="fas fa-arrow-left mr-1"></i>返回列表
    </a>
</h1>

<div class="row">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-header">
                <h5 class="mb-0"><i class="fas fa-edit"></i> 编辑广告</h5>
            </div>
            <div class="card-body">
                <form class="layui-form" id="form" action="index.php?r=manage/ad/edithuan" method="post">
                    <input type="hidden" name="id" value="<?php echo $ret['id']; ?>">
                    <div class="form-group">
                        <label class="font-weight-bold">标题 <span class="text-danger">*</span></label>
                        <input type="text" name="title" class="form-control" placeholder="幻灯广告标题" value="<?php echo $ret['title']; ?>">
                    </div>

                    <div class="form-group">
                        <label class="font-weight-bold">显示平台</label>
                        <select name="type" class="form-control mw-200">
                            <option value="0" <?php echo intval($ret['type'])==0 ? 'selected' : ''; ?>>网页端</option>
                            <option value="1" <?php echo intval($ret['type'])==1 ? 'selected' : ''; ?>>移动端</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label class="font-weight-bold">封面图片</label>
                        <div class="input-group">
                            <input type="text" name="pic" id="tupian" class="form-control" placeholder="封面图片地址" value="<?php echo $ret['pic']; ?>">
                            <div class="input-group-append">
                                <button type="button" class="btn btn-outline-secondary" onclick="upthis()">
                                    <i class="fas fa-upload"></i> 上传
                                </button>
                            </div>
                        </div>
                        <input type="hidden" name="file" value="0">
                        <input type="file" name="file" id="up_tupian" onChange="Uploadthis('tupian','up_tupian')" class="d-none">
                        <small class="form-text text-muted">建议尺寸：1900×449</small>
                        <?php if($ret['pic']): ?>
                        <div class="mt-2">
                            <img src="<?php echo $ret['pic']; ?>" class="img-thumbnail mw-300" style="max-height:80px">
                        </div>
                        <?php endif; ?>
                    </div>

                    <div class="form-group">
                        <label class="font-weight-bold">超级链接</label>
                        <input type="text" name="link" class="form-control" placeholder="https:// 或 http:// 开头" value="<?php echo $ret['link']; ?>">
                    </div>

                    <div class="form-group">
                        <label class="font-weight-bold">排序</label>
                        <input type="number" name="px" class="form-control mw-200" placeholder="0" value="<?php echo intval($ret['px']); ?>">
                        <small class="form-text text-muted">数字越小越靠前（按 px 升序展示）</small>
                    </div>

                    <div class="form-group border-top pt-3 mt-4">
                        <button type="submit" class="btn btn-primary btn-lg px-5" id="save">
                            <i class="fas fa-save"></i> 保存
                        </button>
                        <a href="index.php?r=manage/ad/huan" class="btn btn-outline-secondary btn-lg ml-2">
                            <i class="fas fa-times"></i> 取消
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

{include file="app/manage/view/index/emlog_footer"}

<script>
jQuery(function($){
    $('#form').on('submit', function(e){
        e.preventDefault();
        var btn = $('#save'), text = btn.html();
        btn.prop('disabled', true).html('<i class="fas fa-spinner fa-spin"></i> 保存中...');
        $.ajax({
            url: $(this).attr('action'),
            type: 'POST',
            data: $(this).serialize(),
            dataType: 'json',
            success: function(res){
                if(res.status == 'y'){
                    showToast(res.info, 'success');
                    setTimeout(function(){ window.location.href = 'index.php?r=manage/ad/huan'; }, 1500);
                } else {
                    showToast(res.info || '保存失败', 'danger');
                    btn.prop('disabled', false).html(text);
                }
            },
            error: function(){
                showToast('请求异常', 'danger');
                btn.prop('disabled', false).html(text);
            }
        });
    });
});

function showToast(message, type) {
  type = type || 'success';
  var toast = document.createElement('div');
  toast.className = 'toast align-items-center text-white bg-' + type + ' border-0 position-fixed show';
  toast.setAttribute('role', 'alert');
  toast.style.cssText = 'top: 20px; right: 20px; z-index: 9999; min-width: 250px; opacity: 1; transition: opacity .3s ease;';
  var icon = type === 'success' ? 'check-circle' : 'exclamation-circle';
  toast.innerHTML = '<div class="d-flex"><div class="toast-body"><i class="fas fa-' + icon + ' mr-2"></i>' + message + '</div><button type="button" class="btn-close btn-close-white me-2 m-auto" onclick="this.parentElement.parentElement.remove()"></button></div>';
  document.body.appendChild(toast);
  setTimeout(function() {
    toast.style.opacity = '0';
    setTimeout(function() {
      if(toast.parentNode) toast.parentNode.removeChild(toast);
    }, 350);
  }, 3000);
}

// 指定上传类型为 huan
window.ZhiCmsUploadType = 'huan';

function upthis(){
    document.getElementById("up_tupian").click();
}
</script>