using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Tourism
{
    public partial class SchemeCreation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindFinYear();

                BindDistrict();
            }
        }

        private void BindDistrict()
        {
            string constr = ConfigurationManager.ConnectionStrings["constrTou"].ConnectionString;

            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = @"SELECT distarea_name,
                                dist_name_hindi,
                                dist_census_code,
                                division_code
                         FROM TDistrict_master
                         ORDER BY dist_name_hindi";

                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        da.Fill(dt);

                        ddlDistrict.DataSource = dt;
                        ddlDistrict.DataTextField = "dist_name_hindi";      // Display Name
                        ddlDistrict.DataValueField = "dist_census_code";    // Value
                        ddlDistrict.DataBind();

                        ddlDistrict.Items.Insert(0, new ListItem("-- जिला चुनें --", "0"));
                    }
                }
            }
        }

        private void BindBlock(string DistCode)
        {
            string constr = ConfigurationManager.ConnectionStrings["constrTou"].ConnectionString;

            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = @"SELECT Block_Code,
                                Block_HName,
                                Block_EName
                         FROM BlockMaster where District_Code = '"+DistCode+"' ORDER BY Block_HName";

                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        da.Fill(dt);

                        ddlBlock.DataSource = dt;
                        ddlBlock.DataTextField = "Block_HName";      // Display Name
                        ddlBlock.DataValueField = "Block_Code";    // Value
                        ddlBlock.DataBind();

                        ddlBlock.Items.Insert(0, new ListItem("-- प्रखंड चुनें --", "0"));
                    }
                }
            }
        }
        private void BindFinYear()
        {
            string constr = ConfigurationManager.ConnectionStrings["constrTou"].ConnectionString;

            using (SqlConnection con = new SqlConnection(constr))
            {
                string query = @"SELECT [ID]   ,[FinYear]
                         FROM [TFinYear]
                         ORDER BY [ID]";

                using (SqlCommand cmd = new SqlCommand(query, con))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable dt = new DataTable();
                        da.Fill(dt);

                        ddlYear.DataSource = dt;
                        ddlYear.DataTextField = "FinYear";      // Display Name
                        ddlYear.DataValueField = "ID";    // Value
                        ddlYear.DataBind();

                        ddlYear.Items.Insert(0, new ListItem("-- जिला चुनें --", "0"));
                    }
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;

            string conStr = ConfigurationManager.ConnectionStrings["constrTou"].ConnectionString;

            using (SqlConnection con = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand("USP_ProjectMaster_Save", con))
                {
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@ApprovedYear", ddlYear.SelectedValue);
                    cmd.Parameters.AddWithValue("@DistrictId", ddlDistrict.SelectedValue);
                    cmd.Parameters.AddWithValue("@BlockId", ddlBlock.SelectedValue);
                    cmd.Parameters.AddWithValue("@ProjectName", txtProjectName.Text.Trim());

                    cmd.Parameters.AddWithValue("@ContractorName", txtContractorName.Text.Trim());
                    cmd.Parameters.AddWithValue("@ContractorMobile", txtContractorMobile.Text.Trim());

                    cmd.Parameters.AddWithValue("@AgencyName", txtAgencyName.Text.Trim());
                    cmd.Parameters.AddWithValue("@AgencyMobile", txtAgencyMobile.Text.Trim());

                    cmd.Parameters.AddWithValue("@ApprovalAmount",
                        string.IsNullOrWhiteSpace(txtApprovalAmount.Text)
                        ? (object)DBNull.Value
                        : Convert.ToDecimal(txtApprovalAmount.Text));

                    cmd.Parameters.AddWithValue("@ApprovalDate",
                        string.IsNullOrWhiteSpace(txtApprovalDate.Text)
                        ? (object)DBNull.Value
                        : Convert.ToDateTime(txtApprovalDate.Text));

                    cmd.Parameters.AddWithValue("@LetterNo", txtLetterNo.Text.Trim());

                    cmd.Parameters.AddWithValue("@AgreementNo", txtAgreementNo.Text.Trim());

                    cmd.Parameters.AddWithValue("@AgreementDate",
                        string.IsNullOrWhiteSpace(txtAgreementDate.Text)
                        ? (object)DBNull.Value
                        : Convert.ToDateTime(txtAgreementDate.Text));

                    cmd.Parameters.AddWithValue("@WorkStartDate",
                        string.IsNullOrWhiteSpace(txtWorkStartDate.Text)
                        ? (object)DBNull.Value
                        : Convert.ToDateTime(txtWorkStartDate.Text));

                    cmd.Parameters.AddWithValue("@WorkEndDate",
                        string.IsNullOrWhiteSpace(txtWorkEndDate.Text)
                        ? (object)DBNull.Value
                        : Convert.ToDateTime(txtWorkEndDate.Text));

                    cmd.Parameters.AddWithValue("@AgreementAmount",
                        string.IsNullOrWhiteSpace(txtAgreementAmount.Text)
                        ? (object)DBNull.Value
                        : Convert.ToDecimal(txtAgreementAmount.Text));

                    con.Open();

                    int ProjectId = Convert.ToInt32(cmd.ExecuteScalar());

                    con.Close();

                    ScriptManager.RegisterStartupScript(this, GetType(),
                        "msg",
                        "alert('Record Saved Successfully. Scheme Code : " + ProjectId + "');",
                        true);

                    ClearForm();
                }
            }
        }

        private void ClearForm()
        {
            ddlYear.SelectedIndex = 0;
            ddlDistrict.SelectedIndex = 0;
            ddlBlock.SelectedIndex = 0;

            txtProjectName.Text = "";

            txtContractorName.Text = "";
            txtContractorMobile.Text = "";

            txtAgencyName.Text = "";
            txtAgencyMobile.Text = "";

            txtApprovalAmount.Text = "";
            txtApprovalDate.Text = "";
            txtLetterNo.Text = "";

            txtAgreementNo.Text = "";
            txtAgreementDate.Text = "";

            txtWorkStartDate.Text = "";
            txtWorkEndDate.Text = "";

            txtAgreementAmount.Text = "";
        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindBlock(ddlDistrict.SelectedValue.ToString());
        }
    }
}