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.Class;
using System.Data.SqlClient;
using SixGroup_Knowledge.PopView;
using SixGroup_Knowledge.Model;

namespace SixGroup_Knowledge.PopView
{
    public partial class CreateDirForm : PopModel
    {

        protected static CreateDirForm self = null;
        protected SQLHelper db = null;

        public DirListItem dirListItem = null;
        protected Boolean isNew = true;

        public static CreateDirForm getInstance()
        {
            if (self == null)
                self = new CreateDirForm();
            return self;
        }
        protected CreateDirForm()
        {
            db = new SQLHelper();
            InitializeComponent();
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            self = null;
            this.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.create_dir_def;
            else
                this.p_create_dir_but.Image = Properties.Resources.create_dir_ok;
        }

        protected virtual 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 = "";
            if (dirListItem != null)
            {
                pid = dirListItem.id;
            }
            else 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();
                }
                else
                {
                    pid = "";
                }
                read.Close();
            }
            sqlstr = String.Format("insert into dir(name,pid) values('{0}','{1}')", this.t_box_dirname.Text.Trim(), pid);
            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);
        }

        private void NewDirForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            self = null;
        }

        private void p_close_but_MouseHover(object sender, EventArgs e)
        {
            PictureBox but = sender as PictureBox;
            but.Image = Properties.Resources.close_but_hover;
        }

        private void p_close_but_MouseLeave(object sender, EventArgs e)
        {
            PictureBox but = sender as PictureBox;
            but.Image = Properties.Resources.close_but_def;
        }

        public void setCreatButImage(Bitmap image)
        {
            this.p_create_dir_but.Image = image;
        }

        private void NewDirForm_Load(object sender, EventArgs e)
        {
            loadPDirData();
            if (dirListItem != null && isNew)
            {
                this.l_dir.Visible = false;
                this.cbox_pdir.Visible = false;
            }
        }

        protected void loadPDirData()
        {
            string sqlStr = String.Format("select * from dir");
            SqlDataReader read = db.ExecuteReader(sqlStr);
            this.cbox_pdir.Items.Clear();
            while(read.Read())
            {
                this.cbox_pdir.Items.Add(read["name"].ToString());
            }
            read.Close();
        }
    }
}
