'use strict';

/**
 *
 *
 * @author Mai
 * @date 2019/2/27
 * @version
 */

$(document).ready(function () {
    // 获取审核相关url
    function getUrlPre () {
        const path = window.location.pathname.split('/');
        return _.take(path, 6).join('/');
    }

    // 搜索审批人
    $('#searchAccount').click(() => {
        const data = {
            keyword: $('#searchName').val(),
        };
        postData('/search/user', data, (data) => {
            const resultDiv = $('#searchResult');
            $('h5>span', resultDiv).text(data.name);
            $('#addAuditor').attr('auditorId', data.id);
            $('h6', resultDiv).text(data.role);
            $('p', resultDiv).text(data.company);
            resultDiv.show();
        }, () => {
            $('#searchResult').hide();
        });
    });
    // 添加审批人
    $('#addAuditor').click(() => {
        postData(getUrlPre() + '/audit/add', { auditorId: $('#addAuditor').attr('auditorId') }, (data) => {
            const html = [];
            html.push('<li class="list-group-item" auditorId="'+ data.aid +'"><a href="javascript: void(0)" class="text-danger pull-right">移除</a>');
            html.push('<span>');
            html.push(data.order + ' ');
            html.push(data.name + ' ');
            html.push('</span>');
            html.push('<small class="text-muted">');
            html.push(data.role);
            html.push('</small></li>');
            $('#auditors').append(html.join(''));
        });
    });
    // 删除审批人
    $('li>a', '#auditors').bind('click', function () {
        const li = $(this).parent();
        const data = {
            auditorId: parseInt(li.attr('auditorId')),
        };
        postData(getUrlPre() + '/audit/delete', data, (result) => {
            li.remove();
            for (const rst of result) {
                const aLi = $('li[auditorId=' + rst.aid + ']');
                $('span', aLi).text(rst.order + ' ' + rst.name + ' ');
            }
        });
    });
});