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 hukpiWinApp
{
    public partial class PartnerContractForm : BaseForm
    {
        //decimal partnerId;
        decimal partnerContractId;
        
        public PartnerContractForm(decimal partnerId, decimal partnerContractId)
        {
            InitializeComponent();

            this.partnerId = partnerId;
            this.partnerContractId = partnerContractId;

            loadPartnerDataToHeader(partnerId);
            loadPartnerContractDataToForm(partnerContractId);
        }

        private void loadPartnerDataToHeader(decimal partnerId)
        {
            hukpiDatabaseDataSetTableAdapters.PartnerDataViewTableAdapter PartnerDataAdapter = new hukpiWinApp.hukpiDatabaseDataSetTableAdapters.PartnerDataViewTableAdapter();
            hukpiDatabaseDataSet.PartnerDataViewDataTable partnerData = PartnerDataAdapter.GetDataByPartnerId(partnerId);

            partnerHeaderControl.setUCTitle(partnerData[0]["Name"].ToString());
            partnerHeaderControl.setUCPartnerName(partnerData[0]["Name"].ToString());
            partnerHeaderControl.setUCPartnerNumber(partnerData[0]["PartnerNumber"].ToString());
            partnerHeaderControl.setUCOrderNumber(partnerData[0]["OrderNumber"].ToString());
            partnerHeaderControl.setUCPlace(partnerData[0]["Place"].ToString());
            partnerHeaderControl.setUCContractType(partnerData[0]["ContractTypeName"].ToString());
            partnerHeaderControl.setUCHeaderGroupBoxTitle("Partner adatok");

            partnerData.Dispose();
            PartnerDataAdapter.Dispose();
        }

        private void loadPartnerContractDataToForm(decimal partnerContractId)
        {
            hukpiDatabaseDataSetTableAdapters.ContractTableAdapter contracts = new hukpiWinApp.hukpiDatabaseDataSetTableAdapters.ContractTableAdapter();
            hukpiDatabaseDataSet.ContractDataTable contractData = contracts.GetDataByContractId(partnerContractId);

            string[] bankAccParts = contractData[0]["BankAccount"].ToString().Split('-');

            if (bankAccParts.Length > 0)
            {
                textBoxBankAccountPart1.Text = bankAccParts[0];
            }
            if (bankAccParts.Length > 1)
            {
                textBoxBankAccountPart2.Text = bankAccParts[1];
            }
            if (bankAccParts.Length > 2)
            {
                textBoxBankAccountPart3.Text = bankAccParts[2];
            }

            textBoxTradeRegister.Text = contractData[0]["TradeRegister"].ToString();
            textBoxKSHNumber.Text = contractData[0]["KSHNumber"].ToString();
            textBoxKUJNumber.Text = contractData[0]["KUJNumber"].ToString();
            textBoxTaxNumber.Text = contractData[0]["TaxNumber"].ToString();
            textBoxTaxIdentifier.Text = contractData[0]["TaxIdentifier"].ToString();
            textBoxMotherName.Text = contractData[0]["MotherName"].ToString();
            textBoxIdCardNumber.Text = contractData[0]["IdentityCardNumber"].ToString();
            textBoxTownShipID.Text = contractData[0]["TownShipIdentifier"].ToString();
            textBoxMVHNumber.Text = contractData[0]["MVHNumber"].ToString();
            textBoxAddress.Text = contractData[0]["Address"].ToString();

            contractData.Dispose();
            contracts.Dispose();
        }

        public override void refreshData()
        {
            loadPartnerDataToHeader(partnerId);
            loadPartnerContractDataToForm(partnerContractId);
        }

        private void PartnerContractForm_Load(object sender, EventArgs e)
        {
            Utils.setRolesToButtons(flowLayoutPanelOnPartnerContractForm, this.MdiParent);
        }

        private void buttonEditPartnerContract_Click(object sender, EventArgs e)
        {
            ((MainForm)this.MdiParent).openNewMDIChildForm(new AddEditPartnerContractForm(this.partnerId, this.partnerContractId));
        }
    }
}
