using DC_A95.MES_A95;
using MesWork;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Configuration.ConfigurationManager;

namespace PlanExecute_Query
{
    /// <summary>
    /// 计划执行情况查询
    /// </summary>
    public partial class PlanExecute : Form
    {
        /// <summary>
        /// 构造
        /// </summary>
        public PlanExecute()
        {
            InitializeComponent();
            //this.WindowState = FormWindowState.Maximized;

            var end = DateTime.Now.AddDays(5);
            var start = end.AddDays(-8);
            dtp_Start.Value = start;
            dtp_End.Value = end;

            cb_Area.SelectedItem = "内装线";
        }
        /// <summary>
        /// 钩子
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void PlanExecute_Query_Load(object sender, EventArgs e)
        {
            Refresh_PlanInfo();
        }
        /// <summary>
        /// 刷新日志的显示
        /// </summary>
        private void Refresh_PlanInfo()
        {
            var dt = Select_PlanInfo();
            dgv_PlanDetail.DataSource = dt;
        }

        /// <summary>
        /// 查询日志
        /// </summary>
        /// <returns></returns>
        private DataTable Select_PlanInfo()
        {
            //var area= cb_Area.Text;
            var start = dtp_Start.Value;
            var end = dtp_End.Value;
            var tblName = "";
            //内装线
            //活塞连杆分线
            //缸盖线
            //if (area == "内装线") tblName = "大柴_接口_SAP生产计划_执行计划";
            //else if (area == "缸盖线") tblName = "大柴_接口_SAP生产计划_执行计划_缸盖";
            //else if (area == "活塞连杆分线") tblName = "大柴_接口_SAP生产计划_执行计划_活塞连杆";
            var whereStr = "";
            whereStr = $" WHERE 计划开始时间>='{start.Date}' and 计划开始时间<='{end.Date}'";
            if(cmb_State.Text.Trim()=="未上线")
            {
                whereStr += $" AND [执行状态] = {PlanExec_State.Waiting}";
            }
            else if (cmb_State.Text.Trim() == "已上线")
            {
                whereStr += $" AND [执行状态] = {PlanExec_State.HasExec}";
            }

            if (txt_OrderNum.Text != "")
            {
                whereStr += $" AND [订单号] = '{txt_OrderNum.Text}'";
            }
            //var sql = $"SELECT Top(200) a.[订单类型],a.[生产日期],a.[上线序号],a.[上线总排序],a.[发动机流水号] as 发动机号,a.[机型号],a.[订单号],a.[订单数量],a.[创建时间],a.[执行状态],a.[是否冷试],a.[是否热试],b.当前工位"
            //    + $" FROM [大柴_接口_SAP生产计划_执行计划] a LEFT JOIN [大柴_上线记录_内装线] b ON a.发动机流水号=b.发动机流水号 {whereStr}"
            //    + " order by  a.[上线总排序]";
            var sql = $"SELECT TOP (1000) [AID] ,[发动机编号],[订货号],[订货号ID] ,[机型号] ,[工单ID] ,[工单号] ,[BOM头ID] ,[MES计划顺序] ,[计划开始时间] ,[总装上线扫描时间] ,[计划同步时间] ,[计划状态] ,[执行状态] ,[执行时间] ,[LineID]" +
                $" FROM[WC_WP12T_13L].[dbo].[潍柴_接口_生产计划_执行计划] { whereStr}"
                + " order by  [MES计划顺序]";
            DataLinkMesWork.SQLCommon.ExecuteDataTable(sql, MesWorkForm.ConnectionString,out DataTable dt, out string err2);
            return dt;

        }

        /// <summary>
        /// 手动刷新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            var area =  cb_Area.Text;
            var start = dtp_Start.Value;
            var end = dtp_End.Value;

            var dt = Select_PlanInfo();
            dgv_PlanDetail.DataSource = dt;
        }
    }
}
