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;
using SixGroup_Knowledge.Model;

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

        protected string groupImage = "0";
        protected SQLHelper db = null;
        public DirListItem dirListItem = null;
        public CreateFileForm()
        {
            InitializeComponent();
            db = new SQLHelper();
        }
        public CreateFileForm(DirListItem dirListItem)
        {
            InitializeComponent();
            this.dirListItem=dirListItem;
        }

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

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

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void fileGroupIconHover(object sender, EventArgs e)
        {
            this.p_icon_text.Visible = true;
        }

        private void fileGroupIconLeave(object sender, EventArgs e)
        {
            this.p_icon_text.Visible = false;
        }

        private void fileGroupIconClick(object sender, EventArgs e)
        {
            PictureBox but = sender as PictureBox;
            this.p_select_group.Image = but.Image;
            groupImage = but.Tag.ToString();
        }


        private void t_filename_KeyUp(object sender, KeyEventArgs e)
        {
            if (this.t_filename.Text.Trim() == "")
                this.p_create_but.Image = Properties.Resources.create_file_def;
            else
                this.p_create_but.Image = Properties.Resources.create_file_ok;
        }

        public virtual void p_create_but_Click(object sender, EventArgs e)
        {
            if (this.t_filename.Text.Trim() == "")
            {
                MessageForm.show("文件名不能为空",MessageForm.ERROR);
                return;
            }
            string sqlstr = "";
            string did = "0";
            //通过右键创建文件夹
            if (dirListItem != null)
            {
                did = dirListItem.id;
            }
            else if(this.cbox_file_dir.SelectedIndex>=0)
            {   
                //通过选择
                did = this.cbox_file_dir.SelectedItem.ToString();
                sqlstr = String.Format("select id from dir where name='{0}'", did);
                SqlDataReader read = db.ExecuteReader(sqlstr);
                if (read.Read())
                {
                    did = read["id"].ToString();
                }
                else
                {
                    did = "0";
                }
                read.Close();
            }
            sqlstr = String.Format("insert into "
                + "[file](name,content,gimage,did) "
                + "values('{0}','{1}',{2},{3})", this.t_filename.Text.Trim(),
                CreateFileName(10), groupImage, did);
            if (db.ExecuteNoneQuery(sqlstr) > 0)
            {
                MessageForm.show("文件创建成功");
                MainForm.self.queryAllFileViewData();
                this.Close();
            }
            else
                MessageForm.show("文件创建失败", MessageForm.ERROR);
        }

        private void CreateFileForm_Load(object sender, EventArgs e)
        {
            if (dirListItem != null)
            {
                this.l_file_dir.Visible = false;
                this.cbox_file_dir.Visible = false;
            }
            else
            {
                this.l_file_dir.Visible = true;
                this.cbox_file_dir.Visible = true;
                loadGroupData();
            }
        }

        protected void loadGroupData()
        {
            SqlDataReader read = db.ExecuteReader("select * from dir");
            while(read.Read())
            {
                this.cbox_file_dir.Items.Add(read["name"]);
            }
            read.Close();
        }
        private int rep = 0;
        /// 
        /// 生成随机数字字符串
        /// 
        /// 待生成的位数
        /// 生成的数字字符串
        private string CreateFileName(int codeCount)
        {
            string str = string.Empty;
            long num2 = DateTime.Now.Ticks + this.rep;
            this.rep++;
            Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> this.rep)));
            for (int i = 0; i < codeCount; i++)
            {
                int num = random.Next();
                str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
            }
            return str;
        } 

    }
}
