using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

public partial class TaskList : System.Web.UI.UserControl
{

    static string userid = "0";
    static string TaksPlanAccAtt;
    static int TaksPlanAccNum;
    static long TaksPlanAccMaxSize;
    //static double TaksMinMoney=100;
    static string TaskViewURL=@"~/china/TaskView.aspx";

    public string OpenTaskViewURL
    {
        set
        {
            TaskViewURL = value.ToString().Trim();
        }
    }

    protected string CategoryLink(string id)
    {
        return Request.ServerVariables["URL"].ToString() + "?categoryid="+id;
    }

    protected bool ISUSER(string userid)
    {
        int st = 0;

            if (Session["Wtouser"] != null && Session["Wtouser"] != "")
            {
                if (Session["id"].ToString() == userid)
                {
                    st = 1;
                }
                else
                {
                    st = 0;
                }
            }
            if (st == 1)
            {
                return true;
            }
            else
            {
                return false;
            }
    }

    protected void readfilelist()
    {
     //显示单大图   
     Task.FUNC su = new Task.FUNC();
     PlanViewImage2.ImageUrl = su.filepath + CDatabase.DBReadData("select filename from TaksAcc where linkid=" + Session["PlanViewID"].ToString() + " and type='plan' order by id");
        
        //显示所有图片

    object RD2 = CDatabase.DBFillSelect("select * from Taksacc where linkid=" + Session["PlanViewID"].ToString() + " and type='plan' order by id");
    PlanViewDataList1.DataSource = RD2;
    PlanViewDataList1.DataBind();
    CDatabase.CloseConn();
    }

 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
        }
        else
        {


            if (Request["PlanID"] != null)
            {

                Session["PlanViewID"]=Request["PlanID"].ToString();
                Session["pid"] = Request["PlanID"].ToString();
            }
            else
            {
                Session["PlanViewID"] = "0";
            }
            string[] tv=new string[10];
            tv = CDatabase.DBReader("select subject,userid,tid,sdt,callnum,info,id,planSeledted from TaksPlan where id=" + Session["PlanViewID"].ToString() + "", 8);
            TaskViewLabe1.Text = tv[0];
            TaskID.Text = tv[2];
            PlanID.Text = Session["PlanViewID"].ToString();
            Task.FUNC su = new Task.FUNC();
            string userid=tv[1];
            CDatabase.CloseConn();
            INFO.Text = tv[5];
            NO.Text = tv[6];
            if (Session["Wtouser"] != null)
            {
                if (tv[7] == "true")
                {
                    SelectOKImage1.Visible = true;
                }
            }
            else
            {
                #region 如果本作品所投的任务的当前会员,同时此作品没有选为中标作品的，则此会员可以设本作品为中标作品

                string[] tk = new string[10];
                tk = CDatabase.DBReader("select userid from taksInfo where id=" + tv[2], 1);
                if (Session["id"] != null)
                {
                    if (tk[0] == Session["id"].ToString())
                    {
                        SelectOKButton.Visible = true;
                        if (Session["Wtouser"] != null)
                        {
                            string[] mvip = new string[3];
                            mvip = CDatabase.DBReader("select Telephone,MobilePhone,vipemail from MemberVIP where id=" + tv[1], 3);
                            this.tel.Text = mvip[0];
                            this.mob.Text = mvip[1];
                            this.email.Text = mvip[2];
                            this.TABLE2.Visible = true;
                        }
                    }
                }
                #endregion

            }


            string[] uv = new string[30];
            uv = CDatabase.DBReader("select vipname,vipright,id from MemberVIP where id=" + userid, 2);
            User.Text = uv[0];
            su.SetLAY();
            Lay.Text = su.LAY[Convert.ToUInt16(uv[1])];
            //=============显示所有图片
            readfilelist();
            RETaskViewKey.NavigateUrl = TaskViewURL+"?Taskid="+tv[2];

            #region  评论
            SqlConnection conn = CDatabase.wtoconn();
            conn.Open();
            SqlDataAdapter smdp = new SqlDataAdapter("select top 8 * from Comments where pid=" + Session["PlanViewID"].ToString() + " and isfp='y' order by FBdate desc", conn);
            DataSet ds = new DataSet();
            smdp.Fill(ds, "cu");
            this.pl.DataSource = Doadmin.psgb(ds, Table1, 5, 1);
            this.pl.DataBind();
            conn.Close();
            #endregion

            #region 如果是作品作者就许可编辑作品
            int st = 0;
            if (Session["Wtouser"] != null)
            {
                if (Session["vipright"].ToString() == uv[1])
                {
                    st = 1;
                }
                else
                {
                    st = 0;
                }
            }
              if (st==1)
              {
                PlanViewEditPanel.Visible = true;
                INFO.ReadOnly = true;
                string[] bi = new string[5];
                bi = CDatabase.DBReader("select planaccatt,planaccnum,planaccmaxsize from systembasicinformation", 3);
                TaksPlanAccAtt = bi[0];
                TaksPlanAccNum = Convert.ToInt16(bi[1]);
                TaksPlanAccMaxSize = Convert.ToInt64(bi[2]);
                UploadMaxFileText.Text = "最多可上传" + TaksPlanAccNum.ToString() + "个文件";

                string nowaccnum = "0";
                nowaccnum=CDatabase.DBReadData("select Count(*) from TaksAcc where linkid="+Session["PlanViewID"].ToString()+" and type='plan'");
                if (Convert.ToUInt16(nowaccnum) < TaksPlanAccMaxSize)
                {
                    PlanViewEditFilePanel.Visible = true;
                }
            }
            #endregion


        }
    }



    //========================================
    protected string VIEWIMG(string filename)
    {
        Task.FUNC v = new Task.FUNC();
        return v.filepath+filename;
    }
    //==============================================

    protected void TaskListDropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }


    protected void PlanViewDataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {

        //Message.Text = ((Label)e.Item.FindControl("IMGFILE")).Text;

        //Message.Text = e.CommandName;



        if (e.CommandName == "Del")
        {
            
            //CDatabase.DBUpdate("update taksinfo set isopen='true' where id=" + e.CommandArgument);
            //string id = GridView1.Rows[e.RowIndex].　　　IMGFILE
            Label IMGFILE=(Label)e.Item.FindControl("IMGFILE");

            Task.FUNC f = new Task.FUNC();
            string file = Server.MapPath(IMGFILE.Text);

            if (File.Exists(file))
            {
                File.Delete(file);
            }
            //Label1.Text = Server.MapPath(IMGFILE.Text);

            CDatabase.DBUpdate("delete from taksacc where id=" + e.CommandArgument);
            readfilelist();
            PlanViewEditFilePanel.Visible = true;
        }
        else
        {
            PlanViewDataList1.SelectedIndex = e.Item.ItemIndex;
            PlanViewImage2.ImageUrl = ((Label)e.Item.FindControl("IMGFILE")).Text;
        }




    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        CDatabase.DBUpdate("update taksplan set info='" + INFO.Text+ "' where id=" + Session["PlanViewID"]);
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        string size = "0";
        //UploadTaksAccMessage.Visible = true;
        Task.FUNC f = new Task.FUNC();

        if (!TaksFileUpload1.HasFile)
        {
            UploadTaksAccMessage.Text = "请选择上传[" + TaksPlanAccAtt + "]类型的文件．";
        }
        else
        {
            try
            {
                string ext = (System.IO.Path.GetExtension(TaksFileUpload1.FileName).ToLower()).Replace(".", "");
                if (TaksPlanAccAtt.IndexOf(ext) < 0 && TaksFileUpload1.HasFile)
                {
                    UploadTaksAccMessage.Text = "文类型不许可，请上传[" + TaksPlanAccAtt + "]类型的文件．";

                }
                else
                {
                    size = TaksFileUpload1.PostedFile.ContentLength.ToString();

                    if (Convert.ToInt64(size) > TaksPlanAccMaxSize)
                    {

                        UploadTaksAccMessage.Text = "文件超过" + TaksPlanAccMaxSize.ToString() + "b字节,请进行文件压缩．";
                    }
                    else
                    {
                        UploadTaksAccMessage.Visible = false;


                        DateTime dt = DateTime.Now;
                        string filename = dt.Ticks.ToString();//用公元以来的100毫秒为记录.
                        string savepath = Server.MapPath(f.filepath + filename + "." + ext);
                        TaksFileUpload1.SaveAs(savepath);
                        CDatabase.DBUpdate("insert into TaksAcc (UserID,linkid,sort,type,filename,fileAtt,filesize)values(" + userid + ","+Session["PlanViewID"].ToString()+",0,'Plan','" + filename + "." + ext + "','" + ext + "'," + size + ")");
                        string nownum;
                        nownum = CDatabase.DBReadData("select Count(*) as c from taksacc where userid=" + userid + " and linkid=0");
                        if (Convert.ToInt16(nownum) >= TaksPlanAccNum)
                        {
                            PlanViewEditFilePanel.Visible = false;
                        }
                        readfilelist();
                    }
                }
            }
            catch
            {
                UploadTaksAccMessage.Text = "文类上传失败，请上传[" + TaksPlanAccAtt + "]类型的文件,并且大小不超过" + TaksPlanAccMaxSize + "b．";
            }
        }
    }
    protected void SelectOKButton_Click(object sender, EventArgs e)
    {
        //选择中标作品
        CDatabase.DBUpdate("update taksplan set planSeledted='false' where tid=" + TaskID.Text);
        CDatabase.DBUpdate("update taksplan set planSeledted='true' where id=" + PlanID.Text);
        SelectOKImage1.Visible = true;
        SelectOKButton.Visible = false;

    }
}
