<?php

namespace Encore\Admin\Grid\Tools;

class BatchDelete extends BatchAction
{
    public $icon="fa fa-trash";

    public $buttonClass="btn-danger";

    public function __construct($title)
    {
        $this->title = $title;
    }

    protected function resume_checkout()
    {
        $tip = trans('admin.delete_person_resume_confirm');
        $resume_checkout = "<label><input  type='checkbox' class='resume_checkout' name='resume_checkout' style='width: 18px;height: 13px;position: relative;top:2px;'>{$tip}</label>";
        return $resume_checkout;
    }

    /**
     * Script of batch delete action.
     */
    public function script()
    {
        $title = '';
        $url_pams = '';
        if(strstr($this->resource,'firm/company') && !(strstr($this->resource,'firm/companyimg')) && !(strstr($this->resource,'firm/companysetmeal'))) {
            $deleteConfirm = trans('admin.delete_company_confirm');
        }elseif (strstr($this->resource,'personal/members')) {
            $deleteConfirm = trans('admin.delete_person_confirm');
        }elseif (strstr($this->resource,'recycle/index')){
            $scope = array_get(request()->all(),'scope',1);
            $url_pams='?scope='.$scope;
            $deleteConfirm = trans('admin.delete_confirm');
        }elseif (strstr($this->resource,'personal/resume')){
            $deleteConfirm = trans('admin.delete_confirm');
            $title = $this->resume_checkout();
        } else {
            $deleteConfirm = trans('admin.delete_confirm');
        }
        $confirm = trans('admin.confirm');
        $cancel = trans('admin.cancel');

        return <<<EOT
$('{$this->getElementClass()}').on('click', function() {
    if(typeof {$this->grid->getSelectedRowsName()} == 'undefined'){
        swal('未选中操作项', '', 'error');
        return;
    }
    var id = {$this->grid->getSelectedRowsName()}().join();
    if (id.length==0){
        swal('未选中操作项', '', 'error');
        return;
    }
    swal({
        title: "$deleteConfirm",
        html: "$title",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "$confirm",
        showLoaderOnConfirm: true,
        cancelButtonText: "$cancel",
        preConfirm: function() {
         var is_resume=0;
         if($('.resume_checkout').is(':checked')){
                var is_resume=1;
         }
            return new Promise(function(resolve) {
                $.ajax({
                    method: 'post',
                    url: '{$this->resource}/' + id + '$url_pams'+'?is_resume='+is_resume,
                    data: {
                        _method:'delete',
                        _token:'{$this->getToken()}'
                    },
                    success: function (data) {
                        $.pjax.reload('#pjax-container');

                        resolve(data);
                    }
                });
            });
        }
    }).then(function(result) {
        var data = result.value;
        if (typeof data === 'object') {
            if (data.status) {
                swal(data.message, '', 'success');
            } else {
                swal(data.message, '', 'error');
            }
        }
    });
});

EOT;
    }
}
