<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>现金记录</title>
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/css/common.css}">
    <!-- 引入组件库 -->
    <script th:src="@{/layui/jquery-3.4.1.min.js}"></script>
    <script th:src="@{/layui/layui.js}"></script>
    <script th:src="@{/layui/lay/xmSelect/xm-select.js}"></script>
    <script th:src="@{/js/common.js}"></script>

    <style>
        body{background: #f2f2f2}
        #boxDiv{background: #ffffff}
        #titleDiv{overflow: visible;}
        .content{padding:0 8px;}
        .tableTitle{height:20px;margin-top: 10px;margin-bottom: 10px;}
        .tableTitle>*{height: 20px;line-height: 20px;display: inline-block;margin-right: 5px;}
        .tableName{border-left:3px solid #0000FF;padding-left:5px;font-size: 16px;font-weight: bold;}
        .selectDate{font-size: 14px;color: rgba(0,0,0,0.65)}
    </style>
</head>
<body class="layui-layout-body">
<div id="boxDiv" style="left:8px;right: 8px;bottom:8px;top:8px;position:absolute;">
    <div class="toolbar" id="titleDiv">
        <div style="display: inline-block;">
            <form class="layui-form">
                <div class="layui-inline">
                    <label class="layui-form-label">日期选择</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input formWidthTwo" id="searchDate" placeholder=" ~ " th:value="${startTime+' ~ '+endTime}">
                    </div>
                </div>
            </form>
        </div>
        <div class="layui-inline">
            <button class="layui-btn layui-btn-sm layui-btn-normal" data-type="search" id="search" onclick="search()"><i class="layui-icon layui-icon-search"></i>查询</button>
            <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="showInport()"><i class="layui-icon layui-icon-upload"></i>导入</button>
<!--
            <button class="layui-btn layui-btn-sm layui-btn-normal" onclick="exportExcel()"><i class="layui-icon">&#xe67d;</i>导出</button>
-->
        </div>
    </div>
    <div class="content">
        <div class="tableTitle">
            <span class="tableName">现金账单记录</span>
            <span class="selectDate">&nbsp;</span>
        </div>
        <table id="demo" lay-filter="test"></table>
    </div>

</div>
<!-- 表格操作栏 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="delete">删除</a>
</script>

<!--导入现金记录-->
<div id="inputCash" style="display:none;">
    <div class="layui-inline">
        <button  class="layui-btn layui-btn-sm" onclick="$('#loadfile').click();"><i class="layui-icon"></i>添加文件</button>
        <input tyle="text" id="upload_fileName" class="layui-input formWidthTwo">
    </div>
    <form action="" id="uploadForm" method="post" enctype="multipart/form-data">
        <input type="file" name="spareFile" id="loadfile" style="display:none;" onchange="showUploadFileName(this)">
    </form>
    <div class="layui-inline">
        <button style="margin-top: 10px;" class="layui-btn layui-btn-sm" id="modelDownload" onclick="modelDownload()">模板下载</button>
    </div>
</div>

<!--修改记录-->
<div id="editCash" style="display:none;">
    <form id="updateUser" class="layui-form" style="padding: 20px;">
        <div class="layui-form-item">
            <label class="layui-form-label">操作员号:</label>
            <div class="layui-input-inline">
                <input type="text"  id="mdfCzyh" required lay-verify="required" autocomplete="off" readonly class="layui-input formWidthTwo" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">交易日期:</label>
            <div class="layui-input-inline">
                <input type="text"  id="mdfTrade_date" required lay-verify="required" autocomplete="off" readonly class="layui-input formWidthTwo" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">交易金额:</label>
            <div class="layui-input-inline">
                <input type="text" id="mdfJyje" required lay-verify="required" autocomplete="off"  class="layui-input formWidthTwo">
            </div>
        </div>
    </form>
</div>
</body>
<script th:inline="javascript">
    let layer,laydate,table,tree,form;

    layui.use(['element','table','laydate','layer','form','tree'], function() {
        layer = layui.layer;
        table = layui.table;
        laydate = layui.laydate;
        tree = layui.tree;
        form = layui.form;

        //表格加载
        table.render({
            elem: '#demo',
            height: 'full-'+($(".toolbar").height()+60),
            title: '现金账单记录',
            page: true ,//开启分页
            limit:20,
            limits:[20,30,50],
            //toolbar: '', //开启工具栏，此处显示默认图标，可以自定义模板
            defaultToolbar: [],
            id:'test',
            //skin:'line',
            even:'true',
            //size:'lg', 默认普通尺寸 sm 小 lg 大
            cols: [
                [ //表头
                    {type: 'numbers', title: '序号', width: 50},
/*
                    {field: 'SJLX', title: '类别', sort: false},
*/
                    {field: 'CZYH', title: '操作员号', sort: false},
                    {field: 'TRADE_DATE', title: '交易日期', sort: false},
                    {field: 'JYJE', title: '交易金额', sort: false},
                    { title: '操作', align:'center',width:150, toolbar: '#barDemo'}
                ]
            ],
            data:[]
        });

        //时间控件
        laydate.render({
            elem: '#searchDate'
            ,type: 'date'
            ,range: '~'
        });

        search();

        table.on('tool(test)', function(obj){
            selectData=obj.data;
            let data = obj.data;

            if(obj.event === 'edit') {
                editCash(data);
            }else if (obj.event === 'delete'){
                deleteCash(data);
            }
        });
    });

    //查询
    function search(num){
        num=num===null?1:num;
        let url="/cashLog/findCashList";
        let param={};

        let date=$("#searchDate").val();
        if(date!==''){
            let time = date.split("~");
            let startTime=time[0].trim();
            let endTime=time[1].trim();

            param.startTime=startTime;
            param.endTime=endTime;

            $(".selectDate").text(startTime+" ~ "+endTime);
        }

        table.reload('test', {
            method: 'get',
            url:url,
            where: param,
            page: {
                curr: num
            }
        });

    }

    function showInport(){
        layer.open({
            type:1,
            title:"导入现金记录",
            shade:0,
            area: ['350px', '300px'],
            offset:'auto',
            content: $("#inputCash"),
            btn: ['导入', '取消'],
            btnAlign: 'c',
            yes: function (index, layero) {
                inportExcel();
            },
            btn2:function(){

            }
        });
    }

    //显示文件名称
    function showUploadFileName(obj){
        $('#upload_fileName').val(obj.files[0].name);
    }

    //模板下载
    function modelDownload(){
        window.location.href='/downloadFile/cashModel.xlsx';
    }

    function inportExcel(){
        let fileObj = document.getElementById("loadfile").files[0];
        if(fileObj === undefined){
            layer.alert('请先选择上传文件');
            return false;
        }
        layer.msg("上传中,请等待",{time:1000});
        let formData = new FormData($('#uploadForm')[0]);
        $.ajax({
            url:"/cashLog/uploadCash",
            type:"post",
            data:formData,
            processData:false,
            contentType:false,
            success:function(data){
                if(data.errCode=="0"){
                    layer.alert("现金记录导入成功");
                    window.location.reload();
                }else {
                    let errNumList = data.errNumList;
                    let errStr="";
                    for(let i=0;i<errNumList.length;i++){
                        errStr=errStr+errNumList[i]+",";
                    }
                    layer.alert(data.errMsg+"\n错误所在行："+errStr);
                }
            }
        });
    }

    //修改金额
    function editCash(data){
        $("#mdfCzyh").val(data.CZYH);
        $("#mdfTrade_date").val(data.TRADE_DATE);
        $("#mdfJyje").val(data.JYJE);

        layer.open({
            type:1,
            title:"修改金额",
            shade:0,
            area: ['380px', '350px'],
            offset:'auto',
            content: $("#editCash"),
            btn: ['保存', '取消'],
            btnAlign: 'c',
            yes: function (index, layero) {
                let czyh = $("#mdfCzyh").val();
                let trade_date = $("#mdfTrade_date").val();
                let jyje = $("#mdfJyje").val();

                let url="/cashLog/editCash";
                let param={};
                param.czyh=czyh;
                param.trade_date=trade_date;
                param.jyje=jyje;
                AjaxPostJson(url,param,function (result){
                    if (result.errCode==='0'){
                        layer.alert("修改成功");
                        layer.close(index);
                        search();
                    }else {
                        layer.alert(result.errMsg);
                    }
                });
            },
            btn2:function(){
            }
        });
    }

    //删除
    function deleteCash(data){
        layer.confirm('确认删除该条记录？',{title:'删除记录'}, function(index){
            let url="/cashLog/deleteCash";
            let param={};
            param.czyh=data.CZYH;
            param.trade_date=data.TRADE_DATE;

            AjaxPostJson(url,param,function (result){
                if (result.errCode==='0'){
                    layer.alert("删除成功");
                    layer.close(index);
                    search();
                }else {
                    layer.alert(result.errMsg);
                }
            });
        });
    }
</script>
</html>