<?php
use home\assets\MainAsset;
use home\assets\MenuAsset;
use yii\helpers\Url;
use yii\helpers\Html;
use yii\widgets\Menu;
use yii\helpers\ArrayHelper;
use home\modules\reimbursement\models\reimbursement;
use yii\widgets\ActiveForm;
use yii\helpers\StringHelper;
use common\assets\ZTreeAsset;
use common\widgets\DatePicker;
use common\assets\My97DatePickerAsset;

/* @var $this \yii\web\View */
/* @var $form yii\bootstrap\ActiveForm */
/* @var $model \home\modules\reimbursement\models\Reimbursement */

My97DatePickerAsset::register($this);
MenuAsset::register($this);
ZTreeAsset::register($this);

$assetUrl = $this->getAssetManager()->getBundle(MenuAsset::className())->baseUrl;
$this->registerJsFile($assetUrl . '/js/reim.list.tree.js', ['depends' => ZTreeAsset::className()]);
$this->registerJsFile($assetUrl . '/js/reim.index.js', ['depends' => ZTreeAsset::className()]);

//$this->registerJsFile('@web/static/js/jquery/jquery.scrollto.js', ['depends' => [ZTreeAsset::className()]]);
//$this->registerJsFile($assetUrl.'/js/task.detail.js', ['depends' => [ZTreeAsset::className()]]);

$this->title = '报销列表';
?>

<!--container-->
<div class="container container-project">

    <div class="tab-column">
        <div class="tab-right">
            <button type="button" data-url='<?= Url::to([$this->context->id . '/create']) ?>' data-title="我要报销" data-width="600" data-height="380" class="btn btn-default btn-task open-dialog">
                <a href="javascript:;" >我要报销</a>
            </button>
            <?php if (Reimbursement::readAccess('reimbursement/reimbursement/export')) :?>
                <?php if ($selectTime) : ?>
                    <button type="button" class="btn btn-default btn-task" onclick="javascript:window.location.href='<?= Url::to([$this->context->id.'/export', 'beginTime' => $beginTime, 'endTime' => $endTime]) ?>'">
                        <a href="javascript:;">导出报表</a>
                    </button>
                <?php else : ?>
                    <button type="button" class="btn btn-default btn-task" onclick="javascript:alert('请选择报销时间段')">
                        <a href="javascript:;" style="color:#999;">导出报表</a>
                    </button>
                <?php endif; ?>
            <?php endif; ?>
        </div>
        <p class="caption">报销列表</p>
    </div>
    <?= $this->render('_search', ['model' => $model, 'get' => $get, 'action' => 'index', 'showVerify' => $showVerify,]) ?>
    <?= \home\widgets\Alert::widget(); ?>
    
    <!--mainbody-->
    <div class="mainbody fix">
        <div class="mainleft">
            <div class="labellink fix"> 
                <a href="javascript:;" id="labelLink" class="labelfocus">人员</a>
                <!-- <a href="javascript:;" id="labelLink">任务</a>  -->
                <a href="javascript:;" id="labelLink">类别</a> 
            </div>
            <div class="lefttree-user">
                <ul id="tree"  class="ztree"  ></ul>
            </div>
            <div class="lefttree-task" style="display: none;">
                <ul id="task-tree" class="ztree" data-tree-list-url="<? Url::to(['/task/task/ajax-get-tree-data']) ?>" ></ul>
            </div>
            <div class="lefttree-category" style="display: none;">
                <ul id="category-tree"  class="ztree"  ></ul>
            </div>
        </div>

        <div class="mainright">
            <div id="info" style="display:none;"></div>
            <!--task-table-->
            <?php $mainForm = ActiveForm::begin([ 'id' => 'main-form', 'action' => '']); ?>
            <table class="table task-table">
                <tr>
                    <th width="8%">报销人</th>
                    <th width="9%">报销日期</th>
                    <th width="18%">用途</th>
                    <th width="14%">任务/销售机会/客户</th>
                    <th width="8%">金额</th>
                    <th width="6%">张数</th>
                    <th width="9%">类别</th>
                    <th width="6%">状态</th>
                    <?php if ($showVerify) : ?><th width="9%">快捷通道</th><?php endif; ?>
                    <th width="13%">操作</th>
                </tr>
                <?php if (empty($models)) : ?>
                <tr><td colspan="11" class="tac red">还没有任何数据...</td></tr>
                <?php else : ?>
                    <?php foreach ($models as $key =>$model) : ?>
                        <tr>
                            <td><?= $model->reim_people ?></td>
                            <td><?= date('Y-m-d', $model->reim_time) ?></td>
                            <td class='theleft-min <?= empty($model->remark)?"":"show-div" ?>' data-title="<?= $model->name ?>" data-remark="<?= $model->remark ?>" >
                                <a target="_blank" href="<?= Url::to([$this->context->id . '/detail', 'id' => $model->reim_id, 'from' => Url::to('')]) ?>">
                                    <?= StringHelper::truncate($model->name, 10) ?>
                                </a>
                            </td>
                            <?php $data = common\models\Common::getLinkData($model->link_type, $model->link_id); ?>
                            <td class='theleft-min' title="<?= ArrayHelper::getValue($data, 'name') ?>">
                                <a target="_blank" href="<?= Url::to([ArrayHelper::getValue($data,'link_type')['view_url'][0], 'id' => ArrayHelper::getValue($data, ArrayHelper::getValue($data,'link_type')['pk'])]) ?>">
                                    <?= StringHelper::truncate(ArrayHelper::getValue($data, 'name'),7) ?>
                                </a>
                            </td>
                            <td class='theright-min'><?= number_format($model->reim_money, 2 , '.' , ',') ?></td>
                            <td><?= $model->bill_amount ?></td>
                            <td><?= isset($model->category->name)?$model->category->name:'' ?></td>
                            <td>
                                <?php 
                                    if ($model->isverify == Reimbursement::ISVERIFY_WAIT) {
                                        echo '<font color="#0066FF">待审核</font>';
                                    } elseif ($model->isverify == Reimbursement::ISVERIFY_ENABLE) {
                                        echo '<font color="green">通过</font>';
                                    } else {
                                        echo '<font color="red">否决</font>';
                                    }
                                ?>
                            </td>
                            
                            <?php if ($showVerify) : ?>
                            <td>
                                <?php if ($model->isverify == Reimbursement::ISVERIFY_WAIT) :?>
                                    <?php if (in_array(Yii::$app->user->identity->user_id, explode(',', $model->verify_ids)) && Reimbursement::readAccess('reimbursement/reimbursement/set-status')) :?>
                                        <a href="javascript:;" class="set-vreify-status" data-status="accept" data-set-url="<?= Url::to(['set-status', 'id' => $model['reim_id'], 'status' => Reimbursement::ISVERIFY_ENABLE]) ?>"><font color="green">通过</font></a>/ 
                                        <a href="javascript:;" class="set-vreify-status" data-status="veto" data-set-url="<?= Url::to(['set-status', 'id' => $model['reim_id'], 'status' => Reimbursement::ISVERIFY_DISABLE]) ?>"><font color="red">否决</font></a>
                                    <?php else : ?>
                                        <a href="javascript:;" class="gray" title="当前没有审核权限">通过</a>/ 
                                        <a href="javascript:;" class="gray" title="当前没有审核权限">否决</a>
                                    <?php endif; ?>
                                <?php else : ?>
                                    <font color="#0066FF">-- --</font>
                                <?php endif; ?>
                            </td>
                            <?php endif; ?>

                            <td>
                                <?php if ($model->isverify == Reimbursement::ISVERIFY_DISABLE && !(!empty($viewUser) && !in_array($model['user_id'], $viewUser))) : ?>
                                    <a href="javascript:;" class="open-dialog"  data-url="<?= Url::to([$this->context->id .'/view', 'id' => $model->reim_id]) ?>" data-title='报销编辑' data-width="600" data-height="380">编辑</a>
                                <?php else : ?>
                                    <a href="javascript:;" class="gray" title="当前没有编辑权限"><del>编辑</del></a>
                                <?php endif; ?>

                                <?php if ($showVerify) : ?>
                                <?php if ($model->isverify == Reimbursement::ISVERIFY_WAIT && in_array(Yii::$app->user->identity->user_id, explode(',', $model->verify_ids))) : ?>
                                    <a href="javascript:;" class="open-verify-dialog"  data-url="<?= Url::to([$this->context->id .'/verify', 'id' => $model->reim_id]) ?>" data-title='报销审核' data-width="650" data-height="450" <?= ($model->isverify == Reimbursement::ISVERIFY_WAIT) ? '':'data-submit=false' ?>>审核</a>
                                <?php else : ?>
                                    <a href="javascript:;" class="gray" title="当前没有审核权限"><del>审核</del></a>
                                <?php endif; ?>
                                <?php else : ?>
                                <?php endif; ?>

                                <?php if ((!empty($deleteUser) && !in_array($model['user_id'], $deleteUser)) || $model->isverify == Reimbursement::ISVERIFY_ENABLE) : ?>
                                    <a class="link-delete" href="<?= Url::to([$this->context->id . '/delete', 'id' => $model->reim_id]) ?>">删除</a>
                                <?php else : ?>
                                    <a class="link-delete" href="<?= Url::to([$this->context->id . '/delete', 'id' => $model->reim_id]) ?>">删除</a>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </table>
            <?php ActiveForm::end(); ?>
            <!--task-table--> 
            <!--分页标签-->
            <div class="pages">
                <span class="pagetotal">共<em><?= $pages->totalCount ?></em>条记录<em><?= $pages->page + 1 ?>/<?= $pages->pageCount ?></em>页 </span>
                <?= \yii\widgets\LinkPager::widget(['pagination' => $pages, 'prevPageLabel' => '上一页','nextPageLabel' => '下一页', 'firstPageLabel' => '首页', 'lastPageLabel' => '末页']); ?>
            </div>
            <!--分页标签--> 

        </div>
    </div>
    <!--mainbody-->
</div>
<script>
    var default_date = "<?= date('Y-m-d') ?>";
    var CSRF = "<?= Yii::$app->request->getCsrfToken() ?>";
    var getTreeDataUrl = "<?= Url::to(['/reimbursement/reimbursement/get-treedata', '_csrf' => Yii::$app->request->getCsrfToken()]) ?>";
    var index = "<?= Url::to(['index']) ?>";
    var getCategory = "<?= Url::to(['get-category']) ?>";

    function zTreeOnClick(event, treeId, treeNode) {
        window.location.href="<?= Yii::$app->request->getUrl() ?>"+"&ids="+treeNode.user_ids;
    }

    function categoryOnClick(event, treeId, treeNode) {
        window.location.href="<?= Url::to([$this->context->id.'/statistics']) ?>"+"&id="+treeNode.cat_id;
    }

    /*var assetUrl = "<?= $assetUrl ?>";
    var treeListUrl = "<?= Url::to(['/task/task/ajax-get-tree-data', 'id' => 15645, '_csrf' => Yii::$app->request->getCsrfToken()]) ?>";*/
    var disable = <?= Reimbursement::ISVERIFY_DISABLE ?>;
</script>
