/*
'' ============================================================================================
'' 项目名称：{fyp_title}
'' 项目版本：{fyp_version}
'' 项目描述：{fyp_description}
'' 文件名称：{file_search_name}
'' 文件描述：信息搜索文件{value_match_check value1="{table_description}" value2="" extend_match="unequal"} （{table_description}）{/value_match_check}
'' 公司名称：{fyp_company}
'' 开发人员：{fyp_designer}
'' 创建日期：{fyp_created_date}
'' 修订日期：{system_date_time format="yyyy-MM-dd hh:mm:ss"}
'' 版权信息：{fyp_copyright}

'' 项目升级时需读取以下生成工具的版权信息
'' 生成工具：{fysoft_name}
'' 当前版本：{fysoft_version}
'' 官方网站：{fysoft_website}
'' ============================================================================================
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using FypSystem.Common;
using FypSystem.DBManager;

namespace {fyp_namespace}
{
    public partial class {file_search_name type="2"} : Form
    {
        public {file_search_name type="2"}()
        {
            InitializeComponent();

            Initialize();
            FillGrid();
        }

        /// <summary>
        /// 设置默认值、数据
        /// </summary>
        private void Initialize()
        {
            this.Text += " - " + FypSystem.Common.WebPage.GetConfig("fypAppName");
            {create_manage_check value="true" match="false"}
            this.btn_Add.Visible = false;
            this.btn_Edit.Visible = false;
            this.btn_Del.Visible = false;
            this.col_Checked.Visible = false;
            {/create_manage_check}
            {batch_manage_check value="true" match="false"}
            this.cbx_SelectAll.Visible = false;
            {/batch_manage_check}
        }

        /// <summary>
        /// 填充数据网格
        /// </summary>
        public void FillGrid()
        {
            this.dataGridView1.AutoGenerateColumns = false;

            {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="false"}
            using (FypSystem.DBManager.Access dBManager = new FypSystem.DBManager.Access())
            {/db_type_check}
            {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="true"}
            using (FypSystem.DBManager.SqlServer dBManager = new SqlServer())
            {/db_type_check}
            {
                //string strSql = "select {fields_loop type="5" separator=", "}[{field_name}]{/fields_loop} from [{table_name}]";
                //string strSql = "select {fields_loop type="5" separator=", "}{value_match_check value1="{field_seltable_value}" value2="null" extend_match="unequal" else="[{field_name}]"}(select [{field_seltable_text}] from [{field_seltable_name}] where [{field_seltable_value}]=[{field_name}]) as [{field_seltable_text}]{/value_match_check}{/fields_loop} from [{table_name}]";

                string strSql = "select {fields_loop type="5" separator=", "}{value_match_check value1="{field_seltable_value}" value2="null" extend_match="unequal" else="[{field_name}]"}[{field_seltable_text}]{/value_match_check}{/fields_loop} from {fields_loop}{value_match_check value1="{field_seltable_value}" value2="null" extend_match="unequal"}({/value_match_check}{/fields_loop}[{table_name}]{fields_loop suffix=")"}{value_match_check value1="{field_seltable_value}" value2="null" extend_match="unequal"} LEFT JOIN [{field_seltable_name}] ON [{field_seltable_name}].[{field_seltable_value}]=[{table_name}].[{field_name}]{/value_match_check}{/fields_loop}";
                DataTable tabSrouce = dBManager.GetDataTable(strSql);

                this.dataGridView1.DataSource = tabSrouce;
                tabSrouce.Dispose();
            }
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            FillGrid();
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            {file_add_name type="2"} Form_Add = new {file_add_name type="2"}();
            Form_Add.ShowDialog(this);
        }

        private void btn_Edit_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.CurrentCell != null && this.dataGridView1.CurrentCell.RowIndex > -1)
            {
                int iRowIdx = this.dataGridView1.CurrentCell.RowIndex;
            {fields_loop type="3"}
                string str{field_source_name} = this.dataGridView1.Rows[iRowIdx].Cells["col_{field_source_name}_PrimaryKey"].Value.ToString();
            {/fields_loop}
                {file_add_name type="2"} Form_Add = new {file_add_name type="2"}({fields_loop type="3" separator=", "}str{field_source_name}{/fields_loop});
                Form_Add.ShowDialog(this);
            }
        }

        private void btn_Del_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("是否删除所选择记录？", "提示：", MessageBoxButtons.YesNo) == DialogResult.No)
                return;

            {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="false"}
            using (FypSystem.DBManager.Access dBManager = new Access())
            {/db_type_check}
            {db_type_check value="sqlserver|sqlserver2005|sqlserver2008" match="true"}
            using (FypSystem.DBManager.SqlServer dBManager = new SqlServer())
            {/db_type_check}
            {
                {fields_loop type="3"}
                string strAllID_{field_source_name} = "";
                {/fields_loop}
                foreach (DataGridViewRow row in this.dataGridView1.Rows)
                {
                    if (row.Index >= 0 && row.Cells["col_Checked"].Value != null && row.Cells["col_Checked"].Value.ToString() == "True")
                    {
                        {fields_loop type="3"}
                        strAllID_{field_source_name} += "{field_single_quote}" + row.Cells["col_{field_source_name}_PrimaryKey"].Value.ToString() + "{field_single_quote},";
                        {/fields_loop}
                    }
                }
                if ({fields_loop type="3" separator=" && "}strAllID_{field_source_name}.Length > 0{/fields_loop})
                {
                    {fields_loop type="3"}
                    strAllID_{field_source_name} = FypSystem.Common.StringExtend.TrimRight(strAllID_{field_source_name}, ",");
                    {/fields_loop}
                    string strSql;
                    string strWhere = " where {fields_loop type="3" separator=" and "}[{field_name}] in (" + strAllID_{field_source_name} + ") {/fields_loop}";
                    strSql = "delete from [{table_name}] " + strWhere;
                    //MessageBox.Show(strSql);
                    dBManager.ExecNonQuery(strSql);
                }
                FillGrid();
            }
        }

        private void cbx_SelectAll_CheckedChanged(object sender, EventArgs e)
        {
            bool bChecked = this.cbx_SelectAll.Checked;
            foreach (DataGridViewRow row in this.dataGridView1.Rows)
            {
                if (row.Index >= 0)
                {
                    row.Cells["col_Checked"].Value = bChecked;
                }
            }
        }
    }
}