<?php
use yii\helpers\Url;
use home\assets\MainAsset;
use yii\helpers\ArrayHelper;
use home\modules\reimbursement\models\reimbursement;
use home\modules\reimbursement\models\Application;

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

$assetUrl = $this->getAssetManager()->getBundle(MainAsset::className())->baseUrl;
$this->registerJsFile($assetUrl . '/js/link.js', ['depends' => MainAsset::className()]);

if (empty($form_name)) {
	$this->registerJsFile($assetUrl . '/js/reimbursement.js', ['depends' => MainAsset::className()]);
} else {
	$this->registerJsFile($assetUrl . '/js/verify.js', ['depends' => MainAsset::className()]);
}

$this->title = '报销管理';

//默认值
if ($model->isNewRecord) {
	$request = Yii::$app->request;
	//若新建时已有报销关联信息，则关联选项可见并显示传递的关联信息
	if ($request->get('link_type') != '') {
		$model->showLink = true;
		//若用户拥有超级权限，则可以修改关联信息，否则不予修改权限
		if (!Yii::$app->session->get('isSuperAdmin')) {
			$model->isSelect = false;
		}
		$model->link_type = $request->get('link_type');
		$model->link_id = $request->get('link_id');
		$model->link_name = ArrayHelper::getValue(common\models\Common::getLinkData($model->link_type, $model->link_id), 'name');
	}
	//解决表单提交出现错误后已有申请单关联信息丢失的问题
	if ($request->post('Reimbursement') != '') {
		//$model->showAppli = true;
		$model->appli_id = $request->post('Reimbursement')['appli_id'];
		$model->appli_name = $request->post('Reimbursement')['appli_name'];
	}
	$model->showAppli = true; //申请单暂时显示，后期视要求更改
	//自动填充审核人信息
	$model->verify_names = Reimbursement::getDeptLeader()['leader_name'];
	$model->verify_ids = Reimbursement::getDeptLeader()['leader_id'];
} else {
	if (isset($model->application)) {
		$model->appli_name = $model->application->name;
		if ($model->application->isverify == Application::ISVERIFY_WAIT) {
            $model->appli_isverify = '<font color="green">待审核</font>';
        } elseif ($model->application->isverify == Application::ISVERIFY_ENABLE) {
            $model->appli_isverify = '<font color="green">通过</font>';
        } elseif ($model->application->isverify == Application::ISVERIFY_DISABLE) {
            $model->appli_isverify = '<font color="red">否决</font>';
        }
	} else {
		$model->appli_name = '';
		$model->appli_isverify = '';
	}
	$model->showLink = true;
	$model->showAppli = true;
	$model->reim_time = date('Y-m-d', $model->reim_time);
	$model->link_name = ArrayHelper::getValue(common\models\Common::getLinkData($model->link_type, $model->link_id), 'name');
	$model->verify_names = implode(',', ArrayHelper::getColumn(\common\models\User::getUserByDot($model->verify_ids), 'realname'));
	$model->verify_status = 1; //审核页面默认选择通过
}

//关联权限判断
$linkArr = [];
 
if (Reimbursement::readAccess('reimbursement/reimbursement/link-customer')) {
	$linkArr[] = 'customer';
}
if (Reimbursement::readAccess('reimbursement/reimbursement/link-opportunity')) {
	$linkArr[] = 'sale_opportunity';
}
?>

<div class="modal-window">
	<?= \home\widgets\Alert::widget(); ?>
	<?= !empty($form_name) ? $this->render($form_name, ['model' => $model, 'verify' => $verify, 'verifys' => $verifys, 'action' => $action]): $this->render('_form', ['model' => $model, 'action' => $action, 'linkArr' => $linkArr])?>
</div>

<script>
//var maxMoney = <?= $model->maxMoney ?>;
var getCheckMoney = "<?= Url::to(['get-check-money']) ?>"
</script>