
layui.config({
    base: '../../lib/layui/lay/mymodules/'
    ,version: '101100'
}).use('eleTree');
layui.extend({
    admin: '{/}../../static/js/admin'
});
layui.use(['jquery','eleTree','table','laypage','admin','laydate','form'], function(){
    var $= layui.jquery,
        eleTree	= layui.eleTree,
        laypage	= layui.laypage,
        form = layui.form,
        laydate = layui.laydate,
        table	= layui.table;
        laydate.render({
            elem: '#startTime'
            ,type: 'datetime'
            ,range: '至'
            ,format: 'yyyy-M-d'
        });
    // 时间
    function DateFun(){
        var date = new Date();
        var seperator1 = "-";
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        var currentdate = year + seperator1 + month + seperator1 + strDate;
        return currentdate;
    }
    // 部门
    var belpID = 'root';
    delpFun();
    sessionStorage.setItem("ztreeDepid",'root');
    function delpFun(){
        $.ajax({
            url:'http://apptest.jieweizhineng.com/api/section/getlist',
            method:'get',
            dataType: 'json',
            data: {

            },
            success:function (res) {
                if(res.code === 0){
                    var data = [res.data];
                    eleTree.render({
                        elem: '.ele1',
                        data:data ,
                        defaultExpandAll:true,
                        // showCheckbox: true,
                    });
                    eleTree.on("nodeClick(data)",function(d) {
                        belpID =  d.data.currentData.id;
                        sessionStorage.setItem("ztreeDepid",d.data.currentData.id);
                        $('#changeSelect').val("");
                        $('#startTime').val("");
                        form.render('select');
                        var info={
                            startTime:getPreMonth(DateFun()),
                            endTime:DateFun(),
                            templateId: $("#changeSelect").val(),
                            departmentId: d.data.currentData.id,
                            pageId: '',
                            pageNumber: '1',
                            pageSize: '1',
                        };
                        getStateList(info);
                    })
                }else{
                    layer.msg('请检查网路', {icon: 2});
                }
            },
            error:function (err) {
                layer.msg('请检查网路', {icon: 2});
            }
        })

    }
    var info={
        startTime:getPreMonth(DateFun()),
        endTime:DateFun(),
        templateId: '',
        departmentId: belpID,
        pageId: ''
    };
    getStateList(info);
    function getStateList(res) {
        var index = layer.load(2);
        table.render({
            elem: '#OnSiteInspection',
            url: 'http://apptest.jieweizhineng.com/api/swagger/api/record/v1/getRecordList',
            id: 'OnSiteInspectionID',
            method: 'get',
            loading: true,
            page: true,
            cols: [[
                {field: 'title', title: '二维码名称', align: 'center'},
                {field: 'name', title: '记录添加人', align: 'center'},
                {field: 'createDate', title: '记录时间', align: 'center'},
                {field: 'templateName', title: '模板名称', align: 'center'},
                {title: ' 操作', align: 'center', toolbar: '#barDemo'}
            ]],
            where: {
                startTime: res.startTime,
                endTime: res.endTime,
                templateId: res.templateId,
                departmentId: res.departmentId,
                pageId: res.pageId
            },
            request: {
                pageName: 'pageNumber' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            response: {
                statusName: 'code'
                , statusCode: 200,
                countName: 'count',
                dataName: 'data'
            },
            done: function (res, curr, count) {
                layer.close(index);
                if (res.code === '200') {
                    $('.fr').html('共有数据：' + res.count + ' 条')
                }
            }
        })
    }
    // 监听状态table
    table.on('tool(OnSiteInspection)', function(obj) {
        if (obj.event === "delete"){
            var content ={
                _method:'DELETE'
            };
            layer.alert('确认要删除吗？',{icon: 8}, function(index){
                $.ajax({
                    url: 'http://apptest.jieweizhineng.com/api/swagger/api/record/v1/removeRecord/'+obj.data.id,
                    method: 'post',
                    dataType: 'json',
                    data: content,
                    success: function (res) {
                        if (res.code === '200'){
                            layer.msg('删除成功', {icon: 1});
                            table.reload('OnSiteInspectionID');
                        } else{
                            layer.msg('删除失败', {icon: 2});
                            table.reload('OnSiteInspectionID');
                        }
                        layer.close(index);
                    },
                    error:function () {
                        layer.msg('请检查网络', {icon: 2});
                        layer.close(index);
                    }
                });
            });
        }
    });
    // 查询
    form.on('submit(seachOnSiteins)', function() {
        var time = $("#startTime").val();
        var changeSelect = $("#changeSelect").val();
        var TimeArray =time.split("至");
        var Info = {
            startTime: time == ""? getPreMonth(DateFun()) : TimeArray[0],
            endTime: time == ""? DateFun() : TimeArray[1],
            templateId: changeSelect,
            departmentId: belpID,
            pageId: ''
        };
        getStateList(Info);
        return false;
    })
});