using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SixGroup_Knowledge.PopView;
using SixGroup_Knowledge.Class;
using System.Data.SqlClient;
namespace SixGroup_Knowledge.PopView
{
    public partial class AlterDirFormcs : CreateDirForm
    {
        protected AlterDirFormcs()
        {
            InitializeComponent();
            isNew = false;
        }

        protected static new AlterDirFormcs self = null;

        //protected DirListItem dirListItem = null; 

        public static AlterDirFormcs getInstance(DirListItem dirListItem)
        {
            //判断是否有选中的文件夹
            if (!MainForm.self.dirListIsSelect())
            {
                MessageForm.show("请先选中要修改的文件夹！");
                return null;
            }

            if (self == null)
                self = new AlterDirFormcs();
            self.dirListItem = dirListItem;
            return self;
        }

        public static new AlterDirFormcs getInstance()
        {
            return null;
        }
        private void AlterDirFormcs_Load(object sender, EventArgs e)
        {
            setCreatButImage(Properties.Resources.alter_dir_def);
            
            this.t_box_dirname.KeyUp += new System.Windows.Forms.KeyEventHandler(this.t_box_dirname_KeyUp);
            //this.p_create_dir_but.Click += new System.EventHandler(this.p_create_dir_but_Click);
            this.t_box_dirname.Text = dirListItem.name;
            this.p_create_dir_but.Image = Properties.Resources.alter_dir_ok;
            this.loadPDirData();
            this.label1.Text = "修改目录";

            //根据父级目录的id 获得父级目录的名字
            string pName = "";
            SqlDataReader read = db.ExecuteReader(String.Format("select name from dir where id={0}",dirListItem.pid));
            if (read.Read())
            {
                pName = read["name"].ToString();

                for (int i = 0; i < this.cbox_pdir.Items.Count; i++)
                {
                    if (pName == this.cbox_pdir.Items[i].ToString())
                    {
                        this.cbox_pdir.SelectedItem = this.cbox_pdir.Items[i];
                        break;
                    }
                }
            }
            read.Close();
        }

        private void t_box_dirname_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.t_box_dirname.Text.Trim() == "")
                this.p_create_dir_but.Image = Properties.Resources.alter_dir_def;
            else
                this.p_create_dir_but.Image = Properties.Resources.alter_dir_ok;
        }

        protected override void p_create_dir_but_Click(object sender, EventArgs e)
        {
            if (this.t_box_dirname.Text.Trim() == "")
            {
                MessageForm.show("文件名不能为空", MessageForm.ERROR);
                return;
            }
            string sqlstr = "";
            string pid = dirListItem.pid;
            if (this.cbox_pdir.SelectedIndex > -1)
            {
                pid = this.cbox_pdir.SelectedItem.ToString();
                sqlstr = String.Format("select id from dir where name='{0}'", pid);
                SqlDataReader read = db.ExecuteReader(sqlstr);
                if (read.Read())
                {
                    pid = read["id"].ToString();
                }
                read.Close();
            }
            sqlstr = String.Format("update dir set name='{0}',pid={1} where id={2}", this.t_box_dirname.Text.Trim(), pid, dirListItem.id);
            int result = 0;
            try
            {
                result = db.ExecuteNoneQuery(sqlstr, null, null);
            }
            catch (Exception ex)
            {
                MessageForm.show(ex.Message, MessageForm.ERROR);
            }
            if (result > 0)
            {
                MainForm.self.queryAllDirViewData("");
                MessageForm.show("目录修改成功");
                this.Close();
            }
            else
                MessageForm.show("目录修改失败", MessageForm.ERROR);
        }


    }
}
