<?php
class crmAction extends baseAction
{
	//跟进记录
	function follow()
	{
		LibRule::Check('JY00');
		$Params = mvc::$URL_PARAMS;

		$PjlBase= new PjlBase();
		if(!empty($_POST['fl']) && !empty($_POST['fl']['afl_title']))
		{
			$add = [
				'afl_by_uid'=>$_SESSION['admin_userid'],
				'afl_by_name'=>$_SESSION['admin_uname'],
				'afl_uid'=>(int) $_POST['fl']['afl_shop_id'],
				'afl_shop_id'=>(int) $_POST['fl']['afl_shop_id'],
				'afl_flt_id'=>(int) $_POST['fl']['afl_flt_id'],
				'afl_title'=>$_POST['fl']['afl_title'],
				'afl_txt'=>$_POST['fl']['afl_txt'],
				'afl_atime'=>time()
			];
			$PjlBase->add('crm_follow', $add);
			LibTpl::Success('保存成功');
		}
		$nowPage = isset($Params['page']) && $Params['page'] > 0 ? (int) $Params['page']:1;
		$perPage = isset($_COOKIE['perPage'])? $_COOKIE['perPage'] : 20;
		
		
		$res = $PjlBase->get('crm_follow_type', ['flt_id', 'flt_text'], ' order by flt_seq');
		LibTpl::Set('typeArr', array_column($res['data'], 'flt_text', 'flt_id'));

		$res = $PjlBase->get('pl_shop', ['shop_id', 'shop_name'], sprintf(" and shop_id = %d", $Params['shopid']), true);
		$shopArr = $res['data'];
		if(empty($shopArr)) LibTpl::Error('此用户尚未申请跑街令商铺');
		
		$where = sprintf(" and afl_shop_id=%d", $shopArr['shop_id']);
		$table = 'crm_follow join crm_follow_type on flt_id = afl_flt_id';
		$res = $PjlBase->get($table, ['count(*) as count'], $where, true);
		$total = $res['data']['count']; 
		$res = $PjlBase->get($table, ['afl_title', 'afl_title', 'afl_txt', 'afl_atime', 'flt_text', 'afl_by_name'], sprintf("%s order by afl_atime desc limit %d,%d", $where, ($nowPage-1)*$perPage, $perPage));
		$lists = $res['data'];
		$Page = LibPage::Show($total, $perPage);
		LibTpl::Set('Page', $Page);
		LibTpl::Set('shopArr', $shopArr);
		LibTpl::Set('lists', $lists);
		LibTpl::Set('title', '客情跟进-'.$shopArr['shop_name']);
		LibTpl::Put();
	}
}