using System;
using System.Collections.Generic;
using System.Windows.Forms;
using QMAPP.Entity;
using QMAPP.FJC.BLL.Basic;
using QMAPP.FJC.BLL.Dict;
using QMAPP.FJC.Entity;
using QMAPP.FJC.Entity.Basic;
using QMAPP.FJC.Entity.Equipment;
using QMAPP.FJC.Entity.MachineCheck;
using QMAPP.ServicesAgent;
using QMAPP.WinForm.Common;
using QMFrameWork.Data;

namespace QMAPP.WinForm.Forms.MachineCheck
{
    /// <summary>
    /// 模块编号:
    /// 作    用:设备点检
    /// 作    者:周晓东
    /// 编写日期:2017年11月29日
    ///</summary>
    public partial class MachineCheckEditForm : Form
    {
        //工位编码
        private string _worklocCode = "";
        //工序编码
        private string _workcellCode = "";
        //设备编码
        private string _machineCode = "";
        //设备点检页面url
        public string _machineCheckUrl = "";

        MachineCheckEntity recorder = new MachineCheckEntity();

        /// <summary>
        /// 新建 t
        /// </summary>
        public MachineCheckEditForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="pid"></param>
        public MachineCheckEditForm(string pid)
        {
            recorder.PID = pid;
            InitializeComponent();
        }
        /// <summary>
        /// 获得工位,工厂,班次等信息
        /// </summary>
        public void Init()
        {
            //根据配置文件获得工位,工序
            string locationfile = System.Configuration.ConfigurationManager.AppSettings["loactionfile"];
            if (System.IO.File.Exists(locationfile))
            {
                _machineCode = System.IO.File.ReadAllText(locationfile);
                if (!string.IsNullOrEmpty(_machineCode))
                {
                    //设置ip
                    MachineInfo machoneInofModel = new MachineInfo { MACHINECODDE = _machineCode };
                    QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
                    var list = agent.InvokeServiceFunction<List<MachineInfo>>(B9BasicService.MachineInfoBLL_GetAllList.ToString(), machoneInofModel);
                    if (list.Count > 0)
                    {
                        _worklocCode = list[0].WORKLOC_CODE;
                        _workcellCode = list[0].WORKCELL_CODE;
                        //_machineCheckUrl = list[0].MACHINECHECK_URL;
                    }
                    //else
                    //{
                    //    _machineCode = "";
                    //}
                }
            }

        }

        /// <summary>
        /// 窗体加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form_Load(object sender, EventArgs e)
        {
            Init();
            QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
            #region 绑定下拉列表
            BasicData basicData = new BasicData();
            //加载设备下拉列表
            this.comShift.DataSource = basicData.GetShiftComboxSource();
            this.comShift.DisplayMember = "SHIFT_NAME";
            this.comShift.ValueMember = "SHIFT_CODE";
            //加载点检类型
            this.comCHECKTYPE.DataSource = basicData.GetDictionaryList(DictKind.CHECKTYPE.ToString());
            this.comCHECKTYPE.DisplayMember = "VALUE";
            this.comCHECKTYPE.ValueMember = "KEY";
            #endregion
            //初始化
            //班次
            var shift = agent.InvokeServiceFunction<FJC.Entity.Basic.ProduceShift>(B9BasicService.ProduceShiftBLL_GetWorkingShift.ToString(), ClientContext.MachineInfo != null ? ClientContext.MachineInfo.WORKCENTER_CODE : "");
            if (shift != null)
            {
                comShift.SelectedValue = shift.PRODUCESHIFTTCODE;
            }
            //点检日期
            dtpCheck.ValueX = DateTime.Now;
            //设备
            this.comMachine.DataSource = basicData.GetMachineInfoMachineCheck();
            this.comMachine.DisplayMember = "MACHINENAME";
            this.comMachine.ValueMember = "MACHINECODDE";

            comMachine.SelectedValue = _machineCode;

            #region 编辑
            //判断是否为编辑加载编辑数据
            if (string.IsNullOrEmpty(recorder.PID) == false)
            {
                recorder = agent.InvokeServiceFunction<MachineCheckEntity>("MachineCheckBLL_Get", recorder);
                this.comShift.SelectedValue =   recorder.SHIFT_CODE;
                this.comCHECKTYPE.SelectedValue = recorder.CHECKTYPE;
                this.dtpCheck.ValueX = recorder.CHECKDATE;
            }
            #endregion
        }

        /// <summary>
        /// 窗体保存事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbSave_Click(object sender, EventArgs e)
        {
            //窗体数据校验
            if (string.IsNullOrEmpty(comMachine.Text))
            {
                MessageBox.Show("请选设备");
                return;
            }
            if (string.IsNullOrEmpty(comCHECKTYPE.Text))
            {
                MessageBox.Show("请选点检类型");
                return;
            }
            if (string.IsNullOrEmpty(this.dtpCheck.Text.Trim()))
            {
                MessageBox.Show("请选择点检日期");
                return;
            }
            #region 绑定保存信息
            //获取设备信息
            recorder.SHIFT_CODE = this.comShift.SelectedValue.ToString().Trim();
            recorder.CHECKTYPE = this.comCHECKTYPE.SelectedValue.ToString().Trim();
            recorder.CHECKDATE = Convert.ToDateTime(dtpCheck.ValueX);
            #endregion

            QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();

            //判断数据是否存在
            MachineCheckEntity searchModel = new MachineCheckEntity
            {
                SHIFT_CODE = recorder.SHIFT_CODE,
                MACHINECODE = _machineCode,
                STARTCHECKDATE = recorder.CHECKDATE,
                ENDCHECKDATE = recorder.CHECKDATE,
                CHECKTYPE = recorder.CHECKTYPE
            };
            List<MachineCheckEntity> mcheckList = agent.InvokeServiceFunction<List<MachineCheckEntity>>("MachineCheckBLL_GetListNew", searchModel);
            if (mcheckList != null && mcheckList.Count>0)
            {
                MessageBox.Show("当前点检日期已经存在该设备。");
                return;
            }
            #region 保存
            DataResult<int> result;//判断是否成功
            if (string.IsNullOrEmpty(recorder.PID) == true)
            {
                recorder.MACHINECODE = _machineCode;
                recorder.WORKCELL_CODE = _workcellCode;
                recorder.WORKLOC_CODE = _worklocCode;
                recorder.CHECKSTATUS = "0";
                result = agent.InvokeServiceFunction<DataResult<int>>("MachineCheckBLL_Insert", recorder);
            }
            else
            {
                result = agent.InvokeServiceFunction<DataResult<int>>("MachineCheckBLL_Update", recorder);
            }
            //保存成功
            MessageBox.Show(result.Msg);
            if (result.IsSuccess)
            {
                //查找_machineCheckUrl
                var temp = agent.InvokeServiceFunction<MachineCheckEntity>("MachineCheckBLL_GetMachineCheckUrl", recorder);
                if(temp!=null)
                    _machineCheckUrl = temp.MACHINECHECK_URL;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            #endregion
        }

        /// <summary>
        /// 窗体关闭
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsbClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comMachine_SelectedValueChanged(object sender, EventArgs e)
        {
            
        }

        private void comMachine_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comMachine.SelectedIndex > 0)
            {
                _machineCode = comMachine.SelectedValue.ToString();
                //设置ip
                MachineInfo machoneInofModel = new MachineInfo { MACHINECODDE = _machineCode };
                QMAPP.ServicesAgent.ServiceAgent agent = ClientContext.GetServiceAgent();
                var list = agent.InvokeServiceFunction<List<MachineInfo>>(B9BasicService.MachineInfoBLL_GetAllList.ToString(), machoneInofModel);
                if (list.Count > 0)
                {
                    _worklocCode = list[0].WORKLOC_CODE;
                    _workcellCode = list[0].WORKCELL_CODE;
                    _machineCheckUrl = list[0].MACHINECHECK_URL;
                }
                else
                {
                    _machineCode = "";
                }
            }
        }

    }
}