﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SPS.Model;
using System.Web.UI.WebControls;
using SPS.BusinessLayer;
using System.Text;
using CS.SL.Common.Extensions;

namespace SPS.Web.Controls
{
    public class UserPermissionDLL : CheckBoxList
    {
        public UserPermissionDLL()
        {
            foreach (DocumentInfo site in DocumentBusinessLayer.SelectSiteList())
            {
                this.Items.Add(new ListItem(site.DocumentTitle, site.DocumentID.ToString()));
            }
        }
        const char KEY_SplitValue = ',';
        public override string SelectedValue
        {
            get
            {
                string[] list = new string[this.Items.Count];
                int i = 0;
                foreach (ListItem item in this.Items)
                {
                    if (item.Selected)
                    {
                        list[i++] = item.Value;
                    }
                }
                return list.JoinWith(KEY_SplitValue);
            }
            set
            {
                if (value == null)
                {
                    return;
                }
                foreach (ListItem item in this.Items)
                {
                    bool selected = false;
                    foreach (string id in value.Split(KEY_SplitValue))
                    {
                        if (item.Value == id)
                        {
                            selected = true;
                            break;
                        }
                    }
                    item.Selected = selected;
                }
            }
        }
    }
}