<?php
include_once('../common/base.php');

function getTree($depid)
{
    global $conn;
    $data=array();
    //读取部门数据
    $sql='SELECT count(*) FROM [dbo].[Department] where [PDepId]='.$depid;
    $stmt=sqlsrv_query($conn, $sql);
    $row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_NUMERIC);
    if ($row[0]>0) {
        $sql='SELECT [DepId],[DepCode],[DepName],[PDepId] FROM [dbo].[Department] where [PDepId]='.$depid.' Order By [Sequence] ';
        $stmt=sqlsrv_query($conn, $sql);
        if ($stmt) {
            while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
                $row['children']=getTree($row['DepId']);
                $row['label']=$row['DepName'];
                $row['type']='Dep';
                $row['id']='d-'.$row['DepId'];
                $data[]=$row;
            }
        }

        $sql = 'SELECT [WorkCenter].[CenterId],[WorkCenter].[CenterNo],[WorkCenter].[CenterName] FROM [WorkCenter] INNER JOIN [DepWorkCenter] ON [DepWorkCenter].[CenterId]=[WorkCenter].[CenterId]  WHERE [DepWorkCenter].[DepId]='.$depid;
        $stmt=sqlsrv_query($conn, $sql);
        if ($stmt) {
            while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
                $row['label']=$row['CenterName'];
                $row['type']='Center';
                $row['id']='c-'.$row['CenterId'];
                $data[]=$row;
            }
        }
    } else {
        $sql = 'SELECT [WorkCenter].[CenterId],[WorkCenter].[CenterNo],[WorkCenter].[CenterName] FROM [WorkCenter] INNER JOIN [DepWorkCenter] ON [DepWorkCenter].[CenterId]=[WorkCenter].[CenterId]  WHERE [DepWorkCenter].[DepId]='.$depid;
        $stmt=sqlsrv_query($conn, $sql);
        if ($stmt) {
            while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
                $row['label']=$row['CenterName'];
                $row['type']='Center';
                $row['id']='c-'.$row['CenterId'];
                $data[]=$row;
            }
        }
    }

    if ($depid==0) {
        $sql = 'SELECT [WorkCenter].[CenterId],[WorkCenter].[CenterNo],[WorkCenter].[CenterName] FROM [dbo].[WorkCenter] LEFT JOIN  [DepWorkCenter]   ON [DepWorkCenter].[CenterId]=[WorkCenter].[CenterId] WHERE [DepWorkCenter].[DepId] IS NULL ';
        $stmt=sqlsrv_query($conn, $sql);
        if ($stmt) {
            while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
                $row['label']=$row['CenterName'];
                $row['type']='Center';
                $row['id']='c-'.$row['CenterId'];
                $data[]=$row;
            }
        }
    }
    
    return $data;
}

$tree=array();
$tree=getTree(0);

// getWorkCenters(0, $workcenters);
// getDepartments(0, '');

$data=array();
// $data['workcenters']=array_merge(array(array('CenterName'=>'无','CenterId'=>0)), $workcenters);
// $data['departments']=array_merge(array(array('DepName'=>'无','DepId'=>0)), $departments);
$data['tree']=array(array('children'=>$tree,'label'=>'工作站','id'=>'x-1'));

echo json_encode($data);
