using DevExpress.XtraEditors;
using System;
using System.Windows.Forms;
using ZJ_BYD.DB;
using ZJ_BYD.Model;

namespace ZJ_BYD.UserControls.BranchInfo
{
    public partial class EditBranch : XtraForm
    {
        private static string _stationCode;
        private static string _productSfcCode;
        private static T_BranchInfo branchInfo;

        public EditBranch(string stationCode, string stationName, string productSfcCode, bool view = true)
        {
            InitializeComponent();
            txtStation.Text = $"{stationCode}-{stationName}";
            txtProductSfcCode.Text = productSfcCode;
            _stationCode = stationCode;
            _productSfcCode = productSfcCode;
            btnSave.Enabled = view;
            branchInfo = BranchInfoHelper.GetBranchInfoByStationCodeAndProductSfcCode(_stationCode, _productSfcCode);
            if (branchInfo != null)
            {
                txtPara.Text = branchInfo.BranchPara;
                txtSortIndex.Text = branchInfo.SortIndex.ToString();
                txtBranchSite.Text = branchInfo.BranchSite;
                txtBranchResource.Text = branchInfo.BranchResource;
                txtBranchProcedure.Text = branchInfo.BranchProcedure;                
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            int row;
            if (branchInfo != null)
            {
                branchInfo.BranchPara = txtPara.Text.Trim();
                int.TryParse(txtSortIndex.Text.Trim(), out int sort);
                branchInfo.SortIndex = sort;
                branchInfo.BranchSite = txtBranchSite.Text.Trim();
                branchInfo.BranchResource = txtBranchResource.Text.Trim();
                branchInfo.BranchProcedure = txtBranchProcedure.Text.Trim();
                branchInfo.UpdatedTime = DateTime.Now;
                row = BranchInfoHelper.UpdateBranchInfo(branchInfo);
            }
            else
            {
                int.TryParse(txtSortIndex.Text.Trim(), out int sort);                
                T_BranchInfo t_BranchInfo = new T_BranchInfo
                {
                    Id = Guid.NewGuid().ToString(),
                    StationCode = _stationCode,
                    ProductSfcCode = _productSfcCode,
                    BranchPara = txtPara.Text.Trim(),
                    SortIndex = sort,
                    BranchSite = txtBranchSite.Text.Trim(),
                    BranchResource = txtBranchResource.Text.Trim(),
                    BranchProcedure = txtBranchProcedure.Text.Trim(),                    
                    CreatedTime = DateTime.Now
                };
                var result = BranchInfoHelper.AddBranchInfo(t_BranchInfo);
                row = result ? 1 : 0;
            }
            if (row <= 0)
            {
                this.DialogResult = DialogResult.No;
            }
            else
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}