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;

namespace Aram_CRM_Project.UIL
{
    public partial class frmSetting : Form
    {
        public frmSetting()
        {
            InitializeComponent();
        }

        private void frmSetting_Load(object sender, EventArgs e)
        {
            Aram_CRM_Project.BLL.SettingCRUD SettingCrud = new Aram_CRM_Project.BLL.SettingCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            Aram_CRM_Project.BLL.Entity.Setting SettingVar = new Aram_CRM_Project.BLL.Entity.Setting();
            frmMessage fMessage = new frmMessage();

            SettingVar = SettingCrud.ReadSetting();
            if (SettingVar != null)
            {
                txtReserveComment.Text = SettingVar.ReserveComment;
                txtOrderComment.Text = SettingVar.OrderComment;
                picBill.Image = utility.ConvertByteArrayToImage(SettingVar.ByteArray);
            }
            else
            {
                fMessage.ErrorOnDatabase();
                return;
            }
        }

        private void btnSelectPic_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                picBill.ImageLocation = openFileDialog1.FileName.ToString();
            }
        }

        private void btnRemovePic_Click(object sender, EventArgs e)
        {
            picBill.Image = null;
        }

        private void btnEnter_Click(object sender, EventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            Aram_CRM_Project.BLL.SettingCRUD SettingCrud = new Aram_CRM_Project.BLL.SettingCRUD();
            Aram_CRM_Project.BLL.Utility utility = new Aram_CRM_Project.BLL.Utility();
            Aram_CRM_Project.BLL.Entity.Setting SettingVar = new Aram_CRM_Project.BLL.Entity.Setting();
            frmMessage fMessage = new frmMessage();
            int Result;

            SettingVar.ReserveComment = txtReserveComment.Text.Trim();
            SettingVar.OrderComment = txtOrderComment.Text.Trim();
            SettingVar.ByteArray = utility.ConvertImageToByteArray(picBill.Image, System.Drawing.Imaging.ImageFormat.Bmp);

            Result = SettingCrud.UpdateSetting(SettingVar);
            if (Result == 0)
            {
                fMessage.RegUnSuccess();
                return;
            }
            else if (Result == -1)
            {
                fMessage.RepeatingRecord();
                return;
            }
            else
            {
                fMessage.RegSuccess();
            }

            this.Cursor = Cursors.Default;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
    }
}
