/**
 * 个人中心--意见反馈
 */

let $ = require('yoho-jquery'),
    Hammer = require('yoho-hammer'),
    lazyLoad = require('yoho-jquery-lazyload');

let diaLog = require('js/plugin/dialog');
let saveImage = require('./save-image');
let imgTemplate = require('hbs/home/suggest.hbs');
let $uploadImgList = $('.upload-img-list'),
    headerNavHammer,
    formHammer,
    $likeBtn = $('.suggest-item .like-btn'),
    $disLikeBtn = $('.suggest-item .dislike-btn'),
    $imgAdd = $('.img-add'),
    imgStr = '';

require('./jquery.upload');
require('js/common');

$('#upload-img').upload({
    auto: true,
    fileType: 'image/*',
    uploadScript: '/api/upload/image',
    fileObjName: 'filename',
    fileSizeLimit: 300,
    height: '100%',
    width: '100%',
    multi: false,
    formData: {
        bucket: 'suggest'
    },
    onAddQueueItem: function() {

        // TODO
        $uploadImgList.html(imgTemplate({
            imgList: true
        }));
    },
    onUploadComplete: function(file, data) {
        data = saveImage.saveImage(data);

        $uploadImgList.html('');
        imgStr = data.imgList[0].imgRelUrl;
        $uploadImgList.html(imgTemplate(data));
        $imgAdd.hide();
    }
});

lazyLoad();

headerNavHammer = new Hammer(document.getElementById('yoho-header'));

function submit() {
    let suggestText = $('#suggest-textarea').val(),
        textReg = /\S+/;

    if (!textReg.test(suggestText)) {
        diaLog.showDialog({
            autoHide: true,
            dialogText: '意见不能为空'
        });

        return;
    }

    $.ajax({
        method: 'post',
        url: '/home/savesuggest',
        data: {
            content: suggestText,
            image: imgStr
        }
    }).then(function(data) {
        if (data.code === 200) {
            diaLog.showDialog({
                autoHide: true,
                dialogText: '提交成功'
            });
            setTimeout(function() {
                location.pathname = 'home/suggest';
            }, 2000);
        } else {
            diaLog.showDialog({
                autoHide: true,
                dialogText: '提交失败~'
            });
        }
    }).fail(function() {

        diaLog.showDialog({
            autoHide: true,
            dialogText: '网络错误~'
        });
    });
}

headerNavHammer.on('tap', function(e) {
    if ($(e.target).hasClass('nav-btn')) {
        submit();
    }
});

$('.submit').on('click', function() {
    submit();
});

if (document.getElementById('img-form') !== null) {
    formHammer = new Hammer(document.getElementById('img-form'));

    formHammer.on('tap', function(e) {
        if ($(e.target).hasClass('upload-img-remove')) {
            $uploadImgList.html('');
            imgStr = '';
            setTimeout(function() {
                $imgAdd.show();
            }, 50);

        }
    });
}

// 点赞与取消点赞
$likeBtn.bind('click', function() {

    let id = $(this).closest('.suggest-item').attr('data-id'),
        $that = $(this);

    $.ajax({
        method: 'post',
        url: '/home/upAndDown',
        data: {
            suggest_id: id,
            reliable: 1
        }
    }).then(function(data) {
        if (data.code === 200) {
            $that.closest('.suggest-type').removeClass('show');
            $that.closest('.suggest-item').find('.suggest-good').addClass('show');
        }
    }).fail(function() {

        // TODO

        diaLog.showDialog({
            autoHide: true,
            dialogText: '网络错误~'
        });
    });
});

$disLikeBtn.bind('click', function() {

    let id = $(this).closest('.suggest-item').attr('data-id'),
        $that = $(this);

    $.ajax({
        method: 'post',
        url: '/home/upAndDown',
        data: {
            suggest_id: id,
            reliable: 2
        }
    }).then(function(data) {
        if (data.code === 200) {
            $that.closest('.suggest-type').removeClass('show');
            $that.closest('.suggest-item').find('.suggest-bad').addClass('show');
        }
    }).fail(function() {

        // TODO

        diaLog.showDialog({
            autoHide: true,
            dialogText: '网络错误~'
        });
    });
});