app.controller('yieldReportCtrl', ['$scope', '$rootScope', '$http', '$state', '$filter', '$localStorage', 'HttpService', 'ngDialog', function ($scope, $rootScope, $http, $state, $filter, $localStorage, HttpService, ngDialog) {

    if ($rootScope.loginData.login === false) {
        $state.go("signin")
    } else {
        $state.go("layout.yieldReport")
    }
    $scope.userState = $rootScope.loginData.state;
    // $scope.userState=5;


    $scope.date = "date";

    //获得当前年月日
    var getNowFormatDetailDate = function () {
        var date = new Date();
        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 = date.getFullYear() + "" + month + strDate;
        return currentdate;
    }
    $scope.currentDetailDate = getNowFormatDetailDate();
    $scope.nowDate = new Date("yyyyMMdd");
    $scope.nowMonth = new Date("yyyyMM");

    var getMonthFirstDay = function () {
        var month_first = new Date("yyyyMMdd");
        month_first.setDate(1);
        return month_first;
    }
    var getMonthLastDay = function () {
        var date = new Date();
        var currentMonth = date.getMonth();
        var nextMonth = ++currentMonth;
        var nextMonthFirstDay = new Date(date.getFullYear(), nextMonth, 1);
        var oneDay = 1000 * 60 * 60 * 24;
        var month_last = new Date(nextMonthFirstDay - oneDay);
        return $filter('date')(month_last,'yyyyMMdd');
    }
    $scope.month_first = getMonthFirstDay();
    $scope.month_last = getMonthLastDay();

    var getQualityReport = function (startDate, endDate) {
        HttpService.getQualityReport(startDate, endDate, function (data) {
            $scope.dataCheckData = data.respData;
            console.log("初始化数据核验质量报表：", $scope.dataCheckData)
        })
    }
    //初始化数据核验报表
    if ($rootScope.loginData.state == 1 || $rootScope.loginData.state == 0) {
        console.log($scope.currentDetailDate, $scope.month_last)
        getQualityReport($scope.currentDetailDate, $scope.month_last)
    }
    $scope.myClickLlx = function () {
        var startDate = $("#wdate1").val();
        var endDate = $("#wdate2").val();
        console.log("时间")
        console.log(startDate, endDate)
        getQualityReport(startDate, endDate);
    }
    //  打印数据核验质量报表按钮
    $scope.printQualityReport = function () {
        var startDate = $("#wdate1").val();
        var endDate = $("#wdate2").val();
        console.log("时间")
        console.log(startDate, endDate)
        HttpService.printQulityReportData(startDate, endDate, function (data) {
            var blob = new Blob([data], {type: "application/vnd.ms-excel"});
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.href = URL.createObjectURL(blob);
            a.click();
        })
    }

    var getPrintDataReport = function (startDate, endDate) {

        if ($rootScope.loginData.state == 2) {
            HttpService.getPrintData(startDate, endDate, function (data) {
                $scope.printDataReport = data.respData;
                console.log("初始化打印卸载报表：", $scope.printDataReport)
            })
        }
        if ($rootScope.loginData.state == 0) {
            HttpService.getAdminPrintData(startDate, endDate, $scope.state.substring(1, 2), function (data) {
                $scope.printDataReport = data.respData;
                console.log("初始化打印卸载报表：", $scope.printDataReport)
            })
        }

    }
    if ($rootScope.loginData.state == 2 || $rootScope.loginData.state == 0) {
        console.log($scope.currentDetailDate, $scope.month_last)
        getPrintDataReport($scope.currentDetailDate, $scope.month_last)
    }
    //  打印  打印卸载报表按钮
    $scope.printPrintDataReport = function () {
        var startDate = $("#wdate1").val();
        var endDate = $("#wdate2").val();
        console.log("时间")
        console.log(startDate, endDate)
        if ($rootScope.loginData.state == 2) {
            HttpService.printDataPrintUninstalreportData(startDate, endDate, function (data) {
                var blob = new Blob([data], {type: "application/vnd.ms-excel"});
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.href = URL.createObjectURL(blob);
                a.click();
            })
        }
        if ($rootScope.loginData.state == 0) {
            HttpService.printAdminDataPrintUninstalreportData(startDate, endDate, $scope.state.substring(1, 2), function (data) {
                var blob = new Blob([data], {type: "application/vnd.ms-excel"});
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.href = URL.createObjectURL(blob);
                a.click();
            })
        }

    }

    var getQualityCheckReport = function (month) {
        if ($rootScope.loginData.state == 6) {
            HttpService.getQualityCheckReportData(month, function (data) {
                $scope.qulityCheckData = data.respData;
                console.log("质检报表：", $scope.qulityCheckData)
            })
        }
        if ($rootScope.loginData.state == 0) {
            HttpService.getAdminQualityCheckReportData(month, $scope.state.substring(1, 2), function (data) {
                $scope.qulityCheckData = data.respData;
                console.log("质检报表：", $scope.qulityCheckData)
            })
        }

    }
    if ($rootScope.loginData.state == 6 || $rootScope.loginData.state == 0) {
        getQualityCheckReport($scope.nowMonth)
    }

    $scope.printQualityCheckReport = function () {
        var month = $("#wdateZj").val()
        console.log("时间")
        console.log(month)
        if ($rootScope.loginData.state == 6) {
            HttpService.printQualityCheckReportDatas(month, function (data) {
                var blob = new Blob([data], {type: "application/vnd.ms-excel"});
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.href = URL.createObjectURL(blob);
                a.click();
            })
        }
        if ($rootScope.loginData.state == 0) {
            HttpService.printAdminQualityCheckReportDatas(month, $scope.state.substring(1, 2), function (data) {
                var blob = new Blob([data], {type: "application/vnd.ms-excel"});
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.href = URL.createObjectURL(blob);
                a.click();
            })
        }

    }

    var getPingReportByDay = function (date) {
        if ($rootScope.loginData.state == 4) {
            HttpService.getPingReportByDayData(date, function (data) {
                $scope.pingReportByDay = data.respData;
                console.log("车间日报表：", $scope.pingReportByDay)
            })
        }
        if ($rootScope.loginData.state == 0) {
            HttpService.getAdminPingReportByDayData(date, 'p' + $scope.state.substring(1, 2), function (data) {
                $scope.pingReportByDay = data.respData;
                console.log("车间日报表：", $scope.pingReportByDay)
            })
        }

    }
    if ($rootScope.loginData.state == 4 || $rootScope.loginData.state == 0) {
        getPingReportByDay($scope.nowDate);
    }

    $scope.printPingReportByDay = function () {
        var date = $("#wdateCj").val()
        console.log("时间")
        console.log(date)
        if ($rootScope.loginData.state == 4) {
            HttpService.printPingReportByDay(date, function (data) {
                var blob = new Blob([data], {type: "application/vnd.ms-excel"});
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.href = URL.createObjectURL(blob);
                a.click();
            })
        }
        if ($rootScope.loginData.state == 0) {
            HttpService.printAdminPingReportByDay(date, 'p' + $scope.state.substring(1, 2), function (data) {
                var blob = new Blob([data], {type: "application/vnd.ms-excel"});
                var a = document.createElement("a");
                document.body.appendChild(a);
                a.href = URL.createObjectURL(blob);
                a.click();
            })
        }

    }

    // var getCkReportByDay = function(date){
    //     HttpService.getYieldDailyReportByStorehouseData(date,function(data){
    //         $scope.storeRportData = data.respData;
    //         console.log("仓库产量日报表：",$scope.storeRportData)
    //     })
    // }
    // if($rootScope.loginData.state == 7){
    //     getCkReportByDay($scope.nowDate);
    // }

    // var getStorehouseReportDatas = function(start,end){
    //     HttpService.getCkReportData(start,end,function(data){
    //         $scope.StoreReportData= data.respData;
    //         for(var i=0;i<$scope.StoreReportData.length;i++){
    //             if(i==0){
    //                 $scope.StoreReportData[i].isActive=true;
    //             }else{
    //                 $scope.StoreReportData[i].isActive=false;
    //             }
    //         }
    //         console.log("初始化仓库报表：",data.respData)
    //     })
    // }
    // if($rootScope.loginData.state == 7||$rootScope.loginData.state == 0) {
    //     getStorehouseReportDatas($scope.month_first,$scope.month_last)
    // }

    $scope.showtable = -1;
    $scope.showTable = function (index) {
        if ($scope.showtable != index) {
            $scope.showtable = index;
        }
        else {
            $scope.showtable = -1;
        }
    }

    var getStorehouseReportDatas = function (start, end) {
        HttpService.getStorehouseReportDatas(start, end, function (data) {
            $scope.totalCount = data.respData.sumInfo;
            $scope.StoreReportData = data.respData.listInfo;
            for (var i = 0; i < $scope.StoreReportData.length; i++) {
                if (i == 0) {
                    $scope.StoreReportData[i].isActive = true;
                } else {
                    $scope.StoreReportData[i].isActive = false;
                }
            }
            console.log("初始化仓库出入库报表：", data.respData)
        })
    }
    if ($rootScope.loginData.state == 7 || $rootScope.loginData.state == 0 || $rootScope.loginData.state == 8) {
        getStorehouseReportDatas($scope.month_first, $scope.month_last)
    }


    $scope.printStoreDayReport = function () {
        var date = $("#wdateCj").val()
        console.log("时间")
        console.log(date)
        HttpService.printStoreReportByDay(date, function (data) {
            var blob = new Blob([data], {type: "application/vnd.ms-excel"});
            var a = document.createElement("a");
            document.body.appendChild(a);
            a.href = URL.createObjectURL(blob);
            a.click();
        })
    }


    $scope.myClick = function () {
        var start = $("#wdate1").val()
        var end = $("#wdate2").val()
        if (parseInt(start) <= parseInt(end)) {
            $scope.msgAlert = '';
            if ($rootScope.loginData.state == 2 || $scope.state == '02') {
                getPrintDataReport(start, end);
            }
            if ($rootScope.loginData.state == 7 || $scope.state == '07' || $rootScope.loginData.state == 8) {
                getStorehouseReportDatas(start, end)
            }
        } else {
            $scope.msgAlert = "起始日期不能大于结束日期，请重新选择！";
        }

    }

    $scope.myZjClick = function () {
        var month = $("#wdateZj").val()
        getQualityCheckReport(month)
    }
    $scope.myCjClick = function () {
        var date = $("#wdateCj").val()
        if ($rootScope.loginData.state == 4 || $rootScope.loginData.state == 0) {
            getPingReportByDay(date);
        }

    }

}]);