@extends('admin.master')
@section('style')
    @include('admin.email.common.style')
@endsection

@section('top')
    <div class="top-breadcrumb">
        <h5>邮件客户端</h5>
    </div>
@endsection

@section('content')
    <div class="emial-loading" style="display: flex;"><img src="/data/assets/images/email-loading.gif" alt=""></div>
    <div class="email-client">
        @include('admin.email.common.top', ['mail' => $mail->email, 'mails'=>$mails])
        <div class="email-client-body">
            @include('admin.email.common.left', ['mail' => $mail->email])
            <div class="email-client-body__right">
                <div class="email-client-detail__tool">
                        <span id="email-detail-back" title="返回">
                            <i class="iconfont icon-back"></i>
                        </span>
                    @if($on == 'inbox')
                        <span id="email-detail-reply" title="回复">
                            <i class="iconfont icon-reply"></i>
                        </span>
                        <span id="email-detail-forward" title="转发">
                            <i class="iconfont icon-share"></i>
                        </span>
                    @endif
                    <span id="email-detail-delete" title="删除">
                            <i class="iconfont icon-pinhetuxing1"></i>
                        </span>
                </div>
                <div class="email-client-detail">
                    <div class="email-client-detail__header">
                        <div class="email-client-detail__header-subject">{{ $message->subject }}</div>
                        <ul class="email-client-detail__header-list">
                            <li>
                                <span>发件人：</span>
                                @if($on == 'sent')
                                    <strong>{{ $message->from[0] }}</strong>
                                @else
                                    <strong>{{ $message->from['personal'] }}</strong>
                                    <span>&lt;{{ $message->from['mail'] }}	&gt;</span>
                                @endif
                            </li>
                            <li>
                                <span>收件人：</span>
                                @if($on == 'sent')
                                    <strong>{{ $message->to[0] }}</strong>
                                @else
                                    <strong>{{ $message->to['personal'] }}</strong>
                                    <span>&lt;{{ $message->to['mail'] }}&gt;</span>
                                @endif
                            </li>
                        </ul>
                        <div class="email-client-detail__header-success">
                            <i class="iconfont icon-pinhetuxing"></i>
                            <span>发送成功</span>
                        </div>
                        @if(!empty($message->attachments))
                        <ul class="email-client-detail__header-list">
                            <li>
                                <span>附件{{count($message->attachments)}}个：</span>
                                <span><a href="#detail__attachment">查看附件</a></span>
                            </li>
                        </ul>
                        @endif
                    </div>
                    <div class="email-client-detail_html">
                        <iframe id="detail-iframe" src="{{url('/admin/mail-message-iframe/'.$message->id)}}"></iframe>
                    </div>
                    @if(!empty($message->attachments))
                    <div class="email-client-detail__attachment" id="detail__attachment">
                        <h3>
                            <i class="iconfont icon-fujian"></i>
                            <span>附件{{count($message->attachments)}}个</span>
                        </h3>
                        <div>
                            @foreach($message->attachments as $attachment)
                                <a href="{{url('/admin/mail-message/download/'.$message->id.'?name='.$attachment['name'])}}" target="_blank">{{$attachment['name']}}</a>
                            @endforeach
                        </div>
                    </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
@endsection

@section('foot')
@include('admin.email.common.script')
<script>
    document.getElementById("detail-iframe").addEventListener('load', function () {
        $(".email-client-detail_html").height(this.contentWindow.document.documentElement.clientHeight + 20)
        $(".emial-loading").hide()
    })
    // 返回上一页
    $("#email-detail-back").click(function(){
        window.history.go(-1)
    })
    // 删除邮件
    $("#email-detail-delete").click(function () {
        if (!confirm('确定删除此邮件吗？')) return
        $(".emial-loading").css('display', 'flex')
        $.ajax({
            type: 'post',
            url: "{{ url('/admin/mail-message/del/') }}/{{ $message->id }}",
            success: function (res) {
                window.location.href = "{{url('/admin/email/inbox')}}?email={{$mail->email}}"
            },
            error: function (err) {
                window.location.reload()
            }
        })
    })

    @if($on == 'inbox')
    $("#email-detail-reply").click(function (){
        emailUe.ready(function(){
            var html = '<p></p><p></p><hr>' + $("#detail-iframe")[0].contentWindow.document.documentElement.innerHTML
            emailUe.setContent(html, true)
        })
        $('#send-email-modal').modal({backdrop: 'static', keyboard: false});
        formData.to.push("{{ $message->from['mail'] }}")
        renderEmailList(formData.to, $(".enter-email-list[data-key='to']"))
    })
    $("#email-detail-forward").click(function (){
        emailUe.ready(function(){
            var html = '<p></p><p></p><hr>' + $("#detail-iframe")[0].contentWindow.document.documentElement.innerHTML
            emailUe.setContent(html, true)
        })
        $('#send-email-modal').modal({backdrop: 'static', keyboard: false});
    })
    @endif
</script>
@endsection