<#assign base=request.contextPath />
<#import "../common/defaultLayout.ftl" as defaultLayout>
<@defaultLayout.layout>
    <link rel="stylesheet" href="../assets/css/amazeui.switch.css"/>
    <div class="admin-content">
        <div class="am-cf am-padding" style="padding:1rem 1.6rem 1.6rem 1rem;margin:0px;">
            <!--  padding:1px 2px 3px 4px;上、右、下,和左   -->
            <div class="am-fl am-cf"><strong class="am-text-primary am-text-lg">人力成本管理</strong> /
                <small>人力成本管理</small>
            </div>
        </div>
        <div class="am-g">
            <div class="am-u-sm-12">
                <form class="am-form" id="listForm" action="${base}/humanCost/list" method="POST">
                    <input type="hidden" id="keywords" name="keywords" value='${keywords!""}'/>
                    <table class="am-table am-table-bordered am-table-radius table-main" style="padding:0;">
                        <tbody>
                        <tr>
                            <th class="am-text-middle">项目名称</th>
                            <td>
                                <div class="am-u-sm-10">
                                    <select data-am-selected id="projectId" name="projectId">
                                        <option value="-1">全部</option>
                                        <#list  projectList as project>
                                            <option value=${project.id!} <#if projectId! =="${project.id}" >
                                                    selected
                                                    </#if>>${project.name!}</option>
                                        </#list>
                                    </select>
                                </div>
                            </td>
                            <th class="am-text-middle">姓名</th>
                            <td colspan="4">
                                <div class="am-u-sm-10">
                                    <input type="text" id="userName" class="am-form-field am-input-sm"
                                           value="${userName!}"/>
                                </div>
                            </td>
                        </tr>

                        <#if deptVary gt 0>
                        <tr>
                            <th class="am-text-middle">一级部门</th>
                            <td>
                                <div class="am-u-sm-10">
                                    <select data-am-selected id="deptId" name="deptId">
                                        <option value="-1">全部</option>
                                        <#list  deptList as dept>
                                            <option value=${dept.id!} <#if deptId! =="${dept.id}" >
                                                    selected
                                            </#if>>${dept.name!}</option>
                                        </#list>
                                    </select>
                                </div>
                            </td>
                            <th class="am-text-middle">项目时间</th>
                            <td>
                                <div class="am-u-sm-10">
                                    <div class="am-form am-form-inline">
                                        <div class="am-form-group am-form-icon">
                                            <input type="text" id="time" autocomplete="off" readonly value="${time!}">
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td colspan="2">
                                <div align='right'>
<#--                                    <@shiro.hasPermission name="HUMAN_COST_QUERY">-->
                                    <button type="button" class="am-btn am-btn-default am-btn-sm am-text-secondary"
                                            id="submit-btn">搜索
                                    </button>
<#--                                    </@shiro.hasPermission>-->
<#--                                    <@shiro.hasPermission name="HUMAN_COST_QUERY">-->
                                    <button type="button" class="am-btn am-btn-default am-btn-sm am-text-secondary"
                                            id="submit-btn-export">导出
                                    </button>
<#--                                    </@shiro.hasPermission>-->
                                </div>
                            </td>
                        </tr>
                        <#else>
                            <tr>
                                <th class="am-text-middle">项目时间</th>
                                <td>
                                    <div class="am-u-sm-10">
                                        <div class="am-form am-form-inline">
                                            <div class="am-form-group am-form-icon">
                                                <input type="text" id="time" autocomplete="off" readonly value="${time!}">
                                            </div>
                                        </div>
                                    </div>
                                </td>
                                <td colspan="4">
                                    <div align='right'>
                                        <@shiro.hasPermission name="HUMAN_COST_QUERY">
                                        <button type="button" class="am-btn am-btn-default am-btn-sm am-text-secondary"
                                                id="submit-btn">搜索
                                        </button>
                                        <button type="button" class="am-btn am-btn-default am-btn-sm am-text-secondary"
                                                id="submit-btn-export">导出
                                        </button>
                                        </@shiro.hasPermission>
                                    </div>
                                </td>
                            </tr>
                        </#if>
                        </tbody>
                    </table>
                </form>
            </div>
            <div class="am-u-sm-12 am-u-md-12" style="padding:0 1.6rem 1.6rem 1rem;margin:0;">
                <div class="am-btn-toolbar" style="padding-left:.5rem;">
                    <div class="am-btn-group am-btn-group-xs">
                        <@shiro.hasPermission name="HUMAN_COST_IMPORT">
                        <div class="am-btn-group am-btn-group-xs am-form-file">
                            <button type="button" id="importAccount" onclick="windows()" class="am-btn am-btn-default"><span
                                        class="am-icon-archive"></span> 批量导入
                            </button>
                        </div>
                        <button type="button" id="importTemplate" class="am-btn am-btn-default" >
                            <span class="am-icon-arrow-circle-down"></span> 导入模板
                        </button>
                        </@shiro.hasPermission>
                        成本总计：${(total!0)?string("#,##0.##")}

                    </div>
                </div>
            </div>
        </div>

        <div class="am-g">
            <div class="am-u-sm-12">
                <div class="am-scrollable-horizontal">
                    <table class="am-table am-table-striped am-table-hover table-main">
                        <thead>
                        <tr class="am-text-nowrap">
                            <th class="table-title">项目名称</th>
                            <th class="table-title">人力总计</th>
                            <th class="table-title">成本总计</th>
                            <#if (staff)?exists>
                            <#list staff as person>
                                <th class="table-title">${person.userName!}
                                    <#if showSalary gt 0>(成本：${(person.userSalary!0)?string("#,##0.##")}元)</#if></th>
                            </#list>
                            </#if>
                        </tr>
                        </thead>
                        <tbody>
                        <#if (pager.list)?exists>
                        <#list  pager.list as list>
                            <tr>
                                <td>${list.projectName!}</td>
                                <td>${(list.costSum!0)?string("#,##0.##")}</td>
                                <td>${(list.salarySum!0)?string("#,##0.##")}</td>
                                <#if (staff)?exists>
                                <#list staff as person>
                                    <td>${((map[list.projectName + person.userId])!0)?string("#,##0.##")}</td>
                                </#list>
                                </#if>
                            </tr>
                        </#list>
                        </#if>
                        </tbody>
                    </table>
                </div>
                <div class="am-cf">
                    <!-- 分页 -->
                    <#if (pager.list)?exists && (pager.list?size>0) >
                        <div class="am-fr">
                            <#include "../common/common_pager.ftl">
                        </div>
                    <#else>
                        <div class="am-kai" align="center">
                            <h3>没有找到任何记录!</h3>
                        </div>
                    </#if>
                </div>

            </div>
        </div>

        <footer class="admin-content-footer">
            <hr>
        </footer>
    </div>
</@defaultLayout.layout>
<script src="../assets/js/jquery.ajaxfileupload.js"></script>
<script src="../assets/js/amazeui.switch.js"></script>
<script src="../layui/layui.js"></script>
<script type="text/javascript">
    layui.use('laydate', function(){
        var laydate = layui.laydate;

        laydate.render({
            elem: '#time',
            type: 'month',
            btns: ['confirm'],
            trigger: 'click',
            ready: function(date){
                console.log(date);
            }
        });
    });

    function windows() {
        layui.use('layer', function(){
            var layer = layui.layer;
            var html = '<input type="text" name="layTime" id="layTime" class="form-control layer-date" readonly required value="${time!}">\n' +
                    '<input id="doc-form-file" type="file" name="file" onChange="ajaxUploadFile(\'doc-form-file\',\'${base}/humanCost/batchImport\')">'
            layer.open({
                type: 1,
                title: "选择导入时间",
                offset: '0px',
                id: 'layerDemo',
                content: html,
                area: ['400px', '400px'],
                success: function (layero, index) {
                    layui.laydate.render({
                        elem: "#layTime",
                        type: 'month',
                        btns: ['confirm'],
                        trigger: 'click',
                        ready: function(date){
                            console.log(date);
                        }
                    });
                }
            });
        });
    };

    /**
     *导入模板下载
     */
    $("#importTemplate").on('click', function(event) {
        location.href = "${base}/humanCost/template";
    });

    function ajaxUploadFile(id, url) {
        var date = $("#layTime").val();
        console.log("date:" + date);
        $.ajaxFileUpload({
            url: url + '?date=' + date,
            secureuri: false,
            fileElementId: id,// file标签的id
            dataType: 'json',// 返回数据的类型
            success: function (data, status) {
                console.log("--------success---------" + data)
                if(data.status ==0) {
                    var list = data.data;
                    var content = '';
                    $.each(list, function (i, r) {
                        content += (i+1) + '、' + r + '<br>';
                    });
                    console.log('---> ' + content);
                    parent.layer.open({
                        title: '导入结果：' ,
                        content: data.msg + '<br><br>' + content
                    });
                }else{
                    parent.layer.msg(data.msg);
                }

                window.location.reload();
                $("#" + id).val("");
            },
            error: function (data, status, e) {
                console.log("--------error---------" + data)
                layer.alert("-----------------" + data);
                layer.alert(e);
                $("#" + id).val("");
            }
        });
    };


    $(function () {

        $("#submit-btn").on("click", function () {
            $("#pageNumber").val(1);
            setKeywords();
            $("#listForm").attr("action","${base}/humanCost/list");
            $("#listForm").submit();
        });

        $("#submit-btn-export").on("click", function () {
            setKeywords();
            $("#listForm").attr("action","${base}/humanCost/export");
            $("#listForm").submit();
        });


        function setKeywords() {
            var keywordsObj = {};
            if ($("#projectId").val())
                keywordsObj.projectId = $("#projectId").val();
            if ($("#userName").val())
                keywordsObj.userName = $("#userName").val();
            if ($("#deptId").val())
                keywordsObj.deptId = $("#deptId").val();
            if ($("#time").val())
                keywordsObj.time = $("#time").val();
            var keywords = "";
            if (!$.isEmptyObject(keywordsObj)) {
                keywords = JSON.stringify(keywordsObj);
            }
            console.log("keywords = " + keywords);

            $("#keywords").val(keywords);
        }


    });

</script>
















