crmApp.controller("OutboundHistoryController", OutboundHistoryController);

function OutboundHistoryController($scope, $window, $compile, $q, $http, $location, DTOptionsBuilder, DTColumnBuilder, DTColumnDefBuilder, $filter, OutboundSI, GeneralSI, AdminSI, GeneralService, AntiForgeryService, $mdDialog) {
    var vm = this;
    vm.history = {};
    AntiForgeryService.cookieToken = $window.cookietoken;
    AntiForgeryService.formToken = $window.formtoken;
    vm.historyDetailID = $window.historydetailid;
    vm.dtInstanceOutboundHistoryDetail = [];
    vm.isHCLEmailBlast = $window.isHCLEmailBlast;

    //start init
    vm.init = function () {
        if (!vm.isHCLEmailBlast)
            vm.isHCLEmailBlast = false;

        getOutboundHistoryDetailListByID();
        getOutboundHistoryByID();
    };

    function getOutboundHistoryDetailListByID() {
        var ajaxCallback = function (data, callback, settings) {

            data.OutboundHistID = vm.historyDetailID;

            if (vm.isHCLEmailBlast) {
                OutboundSI.getPagedEmailBlastHistoryDetailListByID(data).then(function (r) {
                    callback(r.data);
                });
            }
            else {
                OutboundSI.getPagedOutboundHistoryDetailListByID(data).then(function (r) {
                    callback(r.data);
                });
            }
        }

        //Bind table - start
        if (vm.isHCLEmailBlast) {
            vm.dtColumnsOutboundHistoryDetail = [
                DTColumnBuilder.newColumn('ID').withTitle('No.').notSortable(),
                DTColumnBuilder.newColumn('NamaPembayarCukai').withTitle('Nama').renderWith(function (data, type, row) {
                    return '<a href="/Outbound/HCLEmailBlastProfile/Detail/' + row.JenisKempen + '/' + row.EmailDataID + '" target="_blank">' + row.TPName + '</a>';
                }),
                DTColumnBuilder.newColumn('NoRujukanCukai').withTitle('No. Rujukan Cukai').renderWith(renderTPlink),
                DTColumnBuilder.newColumn('CallStatus').withTitle('Status')//,
                //DTColumnBuilder.newColumn('IsTestingEmailBlast').withTitle('Testing Email ?')
            ];
        }
        else {
            vm.dtColumnsOutboundHistoryDetail = [
                DTColumnBuilder.newColumn('ID').withTitle('No.').notSortable(),
                //DTColumnBuilder.newColumn('ID').withTitle('ID').renderWith(renderOutboundlink).withOption('defaultContent', '-').notSortable(),
                //DTColumnBuilder.newColumn('ID').withTitle('ID').renderWith(renderOutboundlink),
                DTColumnBuilder.newColumn('TPName').withTitle('Nama').renderWith(function (data, type, row) {
                    return '<a href="/Outbound/Profile/Detail/' + row.JenisKempen + '/' + row.ReMSDataID + '" target="_blank">' + row.TPName + '</a>';
                }),
                DTColumnBuilder.newColumn('NoRujukanCukai').withTitle('No. Rujukan Cukai').renderWith(renderTPlink),
                DTColumnBuilder.newColumn('CallStatus').withTitle('Status')
            ];
        }
        vm.dtOptionsOutboundHistoryDetail = DTOptionsBuilder.newOptions()
			.withOption('ajax', ajaxCallback)
			.withDataProp('data')
			.withOption('processing', true)
			.withOption('searching', true)
			.withOption('serverSide', true)
			.withOption('autoWidth', false)
			.withOption('createdRow', function (row, data, dataIndex) {
			    $compile(angular.element(row).contents())($scope);
			})
			.withPaginationType('full_numbers')
			.withDisplayLength(10);
        //Bind Table - end

        function renderOutboundlink(data, type, row, meta) {
            return '<a href="/Outbound/Profile/detail/' + row.JenisKempen + '/' + row.ReMSDataID + '" target="_blank">' + row.ID + '</a>';
        };
        function renderTPlink(data, type, row, meta) {
            //return '<a href="/Taxpayer/Profile/detail/' + row.TPID + '" target="_blank">' + row.NoCukaiCode + '' + row.NoRujukanCukai + '</a>';
            //return row.NoRujukanCukai;
            return vm.isHCLEmailBlast ? (data ? row["NoCukaiCode"] + ' ' + data : '-') : data;
        };


    };

    function getOutboundHistoryByID() {
        if (vm.isHCLEmailBlast) {
            OutboundSI.getEmailBlastHistoryByID(vm.historyDetailID).then(function (r) {
                vm.history = r.data;
                console.log('History', vm.history);
            });
        }
        else {
            OutboundSI.getOutboundHistoryByID(vm.historyDetailID).then(function (r) {
                //$scope.activity = r.data;
                vm.history = r.data;
                console.log('History', vm.history);
            });
        }
    };

}