using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using UAS_MES_NEW.DataOperate;
using UAS_MES_NEW.Entity;
using UAS_MES_NEW.PublicMethod;

namespace UAS_MES_NEW.Special
{
    public partial class Special_AfterSaleOut : Form
    {

        DataHelper dh = SystemInf.dh;

        LogStringBuilder sql = new LogStringBuilder();

        AutoSizeFormClass asc = new AutoSizeFormClass();

        public Special_AfterSaleOut()
        {
            InitializeComponent();
        }

        private void Special_CancelProdinout_Load(object sender, EventArgs e)
        {
            cu_code.TableName = "customer";
            cu_code.DBTitle = "出货单查询";
            cu_code.SelectField = "cu_id # ID,cu_code  # 客户编号,cu_name # 客户名城";
            cu_code.SetValueField = new string[] { "cu_code", "cu_name" };
            cu_code.FormName = Name;

            InDate.Value = DateTime.Now;
            asc.controllInitializeSize(this);
        }

        private void Confirm_Click(object sender, EventArgs e)
        {
            //正常录入资料的时候
            if (!Cancel.Checked)
            {
                if (cu_code.Text == "")
                {
                    OperateResult.AppendText(">>客户编号不能为空\n", Color.Red, barcode);
                    return;
                }
                if (!dh.CheckExist("aftersalerecord", "afr_barcode='" + barcode.Text + "'"))
                {
                    OperateResult.AppendText(">>条码" + barcode.Text + "不存在\n", Color.Red, barcode);
                    return;
                }
                if (!dh.CheckExist("aftersalerecord", "afr_barcode='" + barcode.Text + "' and afr_outdate is null"))
                {
                    OperateResult.AppendText(">>条码" + barcode.Text + "已出库\n", Color.Red, barcode);
                    return;
                }
                if (dh.getFieldDataByCondition("aftersalerecord", "afr_cucode", "afr_barcode='" + barcode.Text + "'").ToString() != cu_code.Text)
                {
                    OperateResult.AppendText(">>条码" + barcode.Text + "不允许出库给客户" + cu_name.Text + "\n", Color.Red, barcode);
                    return;
                }
                sql.Clear();
                sql.Append("update aftersalerecord set afr_outdate=sysdate,afr_outcucode='" + cu_code.Text + "' where afr_barcode='" + barcode.Text + "'");
                dh.ExecuteSql(sql.GetString(), "update");
                OperateResult.AppendText(">>条码" + barcode.Text + "出库成功\n", Color.Green, barcode);
                LoadGridData();
            }
            else
            {
                if (!dh.CheckExist("aftersalerecord", "afr_barcode='" + barcode.Text + "'"))
                {
                    OperateResult.AppendText(">>条码" + barcode.Text + "不存在\n", Color.Red, barcode);
                    return;
                }
                if (dh.CheckExist("aftersalerecord", "afr_barcode='" + barcode.Text + "' and afr_outdate is null"))
                {
                    OperateResult.AppendText(">>条码" + barcode.Text + "未出库\n", Color.Red, barcode);
                    return;
                }
                sql.Clear();
                sql.Append("update aftersalerecord set afr_outdate=null,afr_outcucode=null where afr_barcode='" + barcode.Text + "'");
                dh.ExecuteSql(sql.GetString(), "update");
                OperateResult.AppendText(">>条码" + barcode.Text + "取消出库成功\n", Color.Green, barcode);
                LoadGridData();
            }
        }

        private void Clean_Click(object sender, EventArgs e)
        {
            OperateResult.Clear();
        }

        private void LoadGridData()
        {
            sql.Clear();
            sql.Append("select * from aftersalerecord left join product on afr_prodcode=pr_code where trunc(afr_outdate)=trunc(to_date('" + InDate.Value.ToString("yyyy-MM-dd hh:mm:ss") + "','yyyy-mm-dd hh24:mi:ss')) and afr_outdate is not null ");
            if (cu_code.Text != "")
            {
                sql.Append(" and afr_outcucode='" + cu_code.Text + "'");
            }
            DataTable dt = (DataTable)dh.ExecuteSql(sql.GetString(), "select");
            BaseUtil.FillDgvWithDataTable(Info, dt);
        }

        private void Special_AfterSaleIn_SizeChanged(object sender, EventArgs e)
        {
            asc.controlAutoSize(this);
        }

        private void barcode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Confirm.PerformClick();
            }
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            LoadGridData();
        }

        private void cu_code_UserControlTextChanged(object sender, EventArgs e)
        {
            LoadGridData();
        }
    }
}