using System;
using System.Collections;
using System.Data;
using System.Text;
using Business.FileFlow.Entity;
using Business.FileFlow.Vo;
using Common;
using LogisticsSystem.Control;
using LogisticsSystem.Control.Helper;

namespace LogisticsSystem.SysMain.FileFlow
{
    public partial class ActionSrv : SrvBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var type = Request.QueryString["type"];

            if (!CheckUserLogin()) return;

            if (!CheckUserDo("用户执行--流程管理", type))
            {
                DataRow[] row = InitHelper.Ops.Select("code='" + type + "'");
                if (row.Length <= 0)
                {
                    Response.Write("用户没有权限操作");
                    return;
                }
                Response.Write("用户没有权限操作:" + row[0]["name"]);
                return;
            }

            //根据类型 执行相关操作
            switch (type)
            {
                case "actionSave":        //新增流程操作
                    ActionSave();
                    break;
                case "GetAllAction":      //获取全部流程操作
                    GetAllAction();
                    break;
                case "actionUpdate":       //更新流程操作
                    ActionUpdate();
                    break;
                case "ActionDel":         //删除流程操作
                    ActionDel();
                    break;
                case "SelectAc":           //获取流程操作选择框
                    SelectAc();
                    break;
                default:
                    Response.Write("");
                    break;
            }
        }


        /// <summary>
        /// 获取流程表头
        /// </summary>
        /// <returns>流程表头</returns>
        private StringBuilder ActionHead()
        {
            var tableHead = new StringBuilder();
            tableHead.Append("<table class=\"standerTable\" id=\"actionSTD\"><thead><td></td>");
            tableHead.Append("<td>名称</td>");
            tableHead.Append("<td>备注</td>");
            tableHead.Append("<td>审批人</td>");
            tableHead.Append("<td>最后修改时间</td>");
            tableHead.Append("<td>添加人</td>");
            tableHead.Append("<td>修改人</td></thead>");
            return tableHead;
        }


        private void GetAllAction()
        {
            //获取表头
            StringBuilder table = ActionHead();
            //获取流程数据
            DataTable data = new ActionVo().AllAction(Request.QueryString["sT"]);

            //带属性td的哈希表
            var hash = new Hashtable();
            hash["approvename"] = "pid";
            hash["remark"] = "depid";

            //整合数据表
            StanderTable(table, data, "id", hash);
            //返回数据
            Response.Write(table.ToString());
        }

        /// <summary>
        /// 新增流程操作
        /// </summary>
        private void ActionSave()
        {
            var action = new ActionEty();
            if (!GetActionEty(action))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new ActionVo().InsertAction(action);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 修改流程操作
        /// </summary>
        private void ActionUpdate()
        {
            var action = new ActionEty();
            if (!GetActionEty(action))
            {
                Response.Write("请输入正确的时间");
                return;
            }
            var result = new ActionVo().UpdateAction(action);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }

        /// <summary>
        /// 删除流程操作
        /// </summary>
        private void ActionDel()
        {
            string id = "'";
            string[] ids = Request.QueryString["id"].Split(',');
            id += string.Join("','", ids) + "'";
            var result = new ActionVo().DelAction(id);
            Response.Write(result.Equals(string.Empty) ? "true" : result);
        }


        /// <summary>
        /// 实体类ActionEty 赋值
        /// </summary>
        /// <returns>bool</returns>
        private bool GetActionEty(ActionEty action)
        {

            action.Name = Request.QueryString["name"];
            action.Remark = Request.QueryString["remark"];
            action.ApproveId = Request.QueryString["approveid"];
            action.Id = Request.QueryString["id"] == "" ? Com.NewGuid() : Request.QueryString["id"];
            action.AddUserId = Session["Userid"].ToString();
            action.AddTime = DateTime.Now;
            action.UpdateUserId = Session["Userid"].ToString();
            action.LastTime = DateTime.Now;
            action.DelFlag = false;
            return true;
        }


        /// <summary>
        /// 获取操作选择框
        /// </summary>
        private void SelectAc()
        {
            var options = new StringBuilder();

            //获取部门数据
            DataTable data = new ActionVo().AllAction("");

            //打包成option
            foreach (DataRow row in data.Rows)
            {
                options.Append("<option value=\"" + row["id"] + "\">" + row["name"] + "</option>");
            }
            if (options.ToString() == "")
            {
                Response.Write("");
                return;
            }

            Response.Write(options.ToString());
        }
    }
}