"use strict";
appCore.controller("MyDiaryLockedReport", ["$state", "$scope", "$http", "$timeout", "vServices", "$uibModal", "$document",
  function ($state, $scope, $http, $timeout, vServices, $uibModal, $document) {
    $scope.$root.page.title = "My Diary Unfilled Report";
    $scope.isAdmin = $scope.$root.gUser.id;
    $scope.project = [];
    $scope.selectAll = { selected: false };
    $scope.CheckedId = "";
    $scope.tblheader = [];
    $scope.tblheader2 = [];
    $scope.revenueCenters = [];
    $scope.showLockDiv = false;
    $scope.myDiary = { default: [], project: [], heroes: [], datatable: [], datatable2: [] };
    $scope.prebilled = [];
    $scope.consolidatedData = [];
    $scope.reportViewModel = '2';
    $scope.billingType = '';
    $scope.invoiceData = {};
    $scope.dTable = null;
    $scope.showBtn = true;
    $scope.numOfWeeks = 0;
    $scope.numOfWeeksArr = [];
    $scope.weekDays = [];
    $scope.lastDay = 0;
    $scope.isLocked = false;
    $scope.lockedReport = [];

    $scope.filter = {
      fromDate: moment().subtract(2, "days").format("DD-MM-YYYY"),
      toDate: moment().format("DD-MM-YYYY"),
      rc_name: 0,
      minHours: 0,
      project: 0,
      proj_id: 0,
      monthyear: '',
      pageName: "My Diary Report"
    };
    let numOr0 = n => isNaN(n) ? 0 : n;
    $scope.sum = (item, list) => list.map(o => o[item]).reduce((a, b) => parseFloat(numOr0(a)) + parseFloat(numOr0(b)));
    $scope.tempArry = [];

    $scope.dateChanged = () => {
      if ($scope.dTable) $scope.dTable.destroy();
      let f = moment($scope.filter.monthyear).clone().startOf('month').format('DD-MM-YYYY');
      let t = moment($scope.filter.monthyear).clone().endOf('month').format('DD-MM-YYYY');
      let bill_month = moment($scope.filter.monthyear).format('MMMM');
      let year = moment($scope.filter.monthyear).format('YYYY');
      vServices.callApi("mydiary/locked_report?bill_month=" + bill_month + "&year=" + year, "GET", {}).then(res => {

        if (res) {
          res.forEach(i => {
            i.is_locked = i.locked_at ? 'Yes' : 'No';
            i.locked_at = i.locked_at ? i.locked_at : '';
          })
          console.log(res);
          $scope.lockedReport = res;
          $scope.$root.callDataTable("#myDiaryDataTableView", 500, (t) => {
            $scope.dTable = t;
          }, { orientation: "landscape" });
        }
      });
    };

    const init = () => {


    };


    $scope.unlockMonthlyBilling = () => {
      if (confirm("Do you really want to unlock the data !")) {
        let f = moment($scope.filter.monthyear).clone().startOf('month').format('DD-MM-YYYY');
        let t = moment($scope.filter.monthyear).clone().endOf('month').format('DD-MM-YYYY');
        let p = $scope.project.find(x => x.id == $scope.filter.proj_id);
        let bill_month = moment($scope.filter.monthyear).format('MMMM');
        let year = moment($scope.filter.monthyear).format('YYYY');
        vServices.callApi("mydiary/unlock_monthly_billing?bill_month=" + bill_month + "&year=" + year + "&proj_id=" + $scope.filter.proj_id + '&projname=' + p.name, "GET", {}).then(res => {
          if (res) {
            $scope.isLocked = false;
          }
        });
      }
    }


  }]);
