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
{
    class AlterFileForm:CreateFileForm
    {

        protected FileListItem fileListItem = null;
        public AlterFileForm(FileListItem fileListItem)
        {
            if (fileListItem == null)
                this.Close();
            this.dirListItem = null;
            this.Load += new System.EventHandler(this.AlterFileFormLoad);
            this.fileListItem = fileListItem;
        }
        protected string gid = "0";
        protected void AlterFileFormLoad(object sender, EventArgs e)
        {
            this.t_filename.Text = fileListItem.name;
            if (fileListItem.name != "")
                this.p_create_but.Image = Properties.Resources.create_file_ok;
            this.label1.Text = "修改文件";
            gid = fileListItem.gimage;
            switch(fileListItem.gimage)
            {
                case "0":
                    this.p_select_group.Image = Properties.Resources.group3;
                    break;
                case "1":
                    this.p_select_group.Image = Properties.Resources.group4;
                    break;
                case "2":
                    this.p_select_group.Image = Properties.Resources.group2;
                    break;
                case "3":
                    this.p_select_group.Image = Properties.Resources.group1;
                    break;
                case "4":
                    this.p_select_group.Image = Properties.Resources.group5;
                    break;
                default:
                    this.p_select_group.Image = Properties.Resources.group3;
                    break;
            }
            loadGroupData();
            string pName = "";
            SqlDataReader read = db.ExecuteReader(
                String.Format("select name from dir where id={0}", fileListItem.did));
            if (read.Read())
            {
                pName = read["name"].ToString();
                for (int i = 0; i < this.cbox_file_dir.Items.Count; i++)
                {
                    if (pName == this.cbox_file_dir.Items[i].ToString())
                    {
                        this.cbox_file_dir.SelectedItem = this.cbox_file_dir.Items[i];
                        break;
                    }
                }
            }
            read.Close();
        }
        public override 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 (this.cbox_file_dir.SelectedIndex >= 0)
            {
                //获得文件夹id
                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})", this.t_filename.Text.Trim(),
                groupImage, did);
             * */
            sqlstr = String.Format("update [file] set name='{0}',gimage='{1}',did='{2}'",
                this.t_filename.Text.Trim(), groupImage, did);
            if (db.ExecuteNoneQuery(sqlstr) > 0)
            {
                MessageForm.show("文件修改成功");
                MainForm.self.queryAllFileViewData();
                this.Close();
            }
            else
                MessageForm.show("文件修改失败", MessageForm.ERROR);
        }
    }
}
