public FormReqFieldCondEdit(RequiredField reqField, RequiredFieldName condType)
 {
     InitializeComponent();
     Lan.F(this);
     _reqField          = reqField;
     _originalFieldName = condType;
     _listReqFieldConds = reqField.ListRequiredFieldConditions.FindAll(x => x.ConditionType == _originalFieldName);
 }
        private void ListValuesSetIndices()
        {
            RequiredFieldName selectedType = _listIndexFieldNames[listConditionType.SelectedIndex];

            if (selectedType != _originalFieldName)
            {
                return;
            }
            if (selectedType == RequiredFieldName.Clinic &&
                _listReqFieldConds.Exists(x => x.ConditionValue == "0"))
            {
                listConditionValues.SelectedIndices.Add(0);                //Select 'Unassigned'
            }
            for (int i = 0; i < listConditionValues.Items.Count; i++)
            {
                if (selectedType == RequiredFieldName.BillingType &&
                    _listReqFieldConds.Exists(x => x.ConditionValue == _listBillingTypeDefs[i].DefNum.ToString()))
                {
                    listConditionValues.SelectedIndices.Add(i);
                    continue;
                }
                if (selectedType == RequiredFieldName.Clinic &&
                    i > 0 &&
                    _listReqFieldConds.Exists(x => x.ConditionValue == _listClinics[i - 1].ClinicNum.ToString()))                   //subtract 1 for 'Unassigned'
                {
                    listConditionValues.SelectedIndices.Add(i);
                    continue;
                }
                if (selectedType == RequiredFieldName.PrimaryProvider &&
                    _listReqFieldConds.Exists(x => x.ConditionValue == _listProvs[i].ProvNum.ToString()))
                {
                    listConditionValues.SelectedIndices.Add(i);
                    continue;
                }
                if (_listReqFieldConds.Exists(x => x.ConditionValue == listConditionValues.Items[i].ToString()))
                {
                    listConditionValues.SelectedIndices.Add(i);
                }
            }
            if (_listReqFieldConds.Count > 0)
            {
                if (_listReqFieldConds[0].Operator == ConditionOperator.Equals)
                {
                    listRelationships.SelectedIndex = 0;                  //'Is'
                }
                else
                {
                    listRelationships.SelectedIndex = 1;                  //'Is not'
                }
            }
        }
 ///<summary>Adds the RequiredFieldName to listConditionType, selects the appropriate SelectedIndex, and adds the RequiredFieldName to the
 ///list keeping track of indices.</summary>
 private void AddListConditionType(RequiredFieldName fieldName)
 {
     if (IsNew && _reqField.ListRequiredFieldConditions.Exists(x => x.ConditionType == fieldName))
     {
         return;                //Do not add to list if the required field already has a condition of that type
     }
     if (fieldName == RequiredFieldName.Birthdate)
     {
         listConditionType.Items.Add(Lan.g("enumRequiredFieldName", "Age"));
     }
     else
     {
         listConditionType.Items.Add(Lan.g("enumRequiredFieldName", fieldName.ToString()));
     }
     _listIndexFieldNames.Add(fieldName);
     if (fieldName == _originalFieldName)
     {
         listConditionType.SelectedIndex = listConditionType.Items.Count - 1;
     }
 }
        private void butOK_Click(object sender, EventArgs e)
        {
            if (listConditionType.SelectedIndex == -1)
            {
                MsgBox.Show(this, "Please select a condition type.");
                return;
            }
            RequiredFieldName      selectedField = _listIndexFieldNames[listConditionType.SelectedIndex];
            RequiredFieldCondition condition;

            switch (selectedField)
            {
            //Types that are continuous values
            case RequiredFieldName.Birthdate:
            case RequiredFieldName.AdmitDate:
            case RequiredFieldName.DateTimeDeceased:
                if (textConditionValue1.Text == "")
                {
                    MsgBox.Show(this, "Please enter a condition value.");
                    return;
                }
                if (comboOperator1.SelectedIndex == -1)
                {
                    MsgBox.Show(this, "Please select an operator from the drop down list.");
                    return;
                }
                //Construct the first condition
                RequiredFieldCondition condition1 = new RequiredFieldCondition();
                condition1.RequiredFieldNum = _reqField.RequiredFieldNum;
                condition1.ConditionType    = selectedField;
                int      agePlaceholder;
                DateTime datePlaceholder;
                if (selectedField == RequiredFieldName.Birthdate && !Int32.TryParse(textConditionValue1.Text, out agePlaceholder))
                {
                    MsgBox.Show(this, "Please enter a valid integer.");
                    return;
                }
                else if ((selectedField == RequiredFieldName.AdmitDate || selectedField == RequiredFieldName.DateTimeDeceased) &&
                         !DateTime.TryParse(textConditionValue1.Text, out datePlaceholder))
                {
                    MsgBox.Show(this, "Please enter a valid date.");
                    return;
                }
                condition1.ConditionValue = textConditionValue1.Text;
                condition1.Operator       = (ConditionOperator)(comboOperator1.SelectedIndex + 2);              //Plus 2 because Equals and NotEquals are not in the combo box
                RequiredFieldConditions.DeleteAll(_listReqFieldConds.Select(x => x.RequiredFieldConditionNum).ToList());
                if (textConditionValue2.Text == "")
                {
                    RequiredFieldConditions.Insert(condition1);
                    break;
                }
                //Construct the second condition if it is not blank
                if (comboOperator2.SelectedIndex == -1)
                {
                    MsgBox.Show(this, "Please select an operator from the drop down list.");
                    return;
                }
                if (listRelationships.SelectedIndex == -1)
                {
                    MsgBox.Show(this, "Please select 'And' or 'Or'.");
                    return;
                }
                RequiredFieldCondition condition2 = new RequiredFieldCondition();
                condition2.RequiredFieldNum = _reqField.RequiredFieldNum;
                condition2.ConditionType    = selectedField;
                if (selectedField == RequiredFieldName.Birthdate && !Int32.TryParse(textConditionValue2.Text, out agePlaceholder))
                {
                    MsgBox.Show(this, "Please enter a valid integer.");
                    return;
                }
                else if ((selectedField == RequiredFieldName.AdmitDate || selectedField == RequiredFieldName.DateTimeDeceased) &&
                         !DateTime.TryParse(textConditionValue2.Text, out datePlaceholder))
                {
                    MsgBox.Show(this, "Please enter a valid date.");
                    return;
                }
                condition2.ConditionValue = textConditionValue2.Text;
                condition2.Operator       = (ConditionOperator)(comboOperator2.SelectedIndex + 2); //Plus 2 because Equals and NotEquals are not in the combo box
                if (listRelationships.SelectedIndex == 0)                                          //'And' is selected
                {
                    condition1.ConditionRelationship = LogicalOperator.And;
                    condition2.ConditionRelationship = LogicalOperator.And;
                }
                else                          //'Or' is selected
                {
                    condition1.ConditionRelationship = LogicalOperator.Or;
                    condition2.ConditionRelationship = LogicalOperator.Or;
                }
                RequiredFieldConditions.Insert(condition1);
                RequiredFieldConditions.Insert(condition2);
                break;

            //Types that store the foreign key of the value
            case RequiredFieldName.BillingType:
            case RequiredFieldName.Clinic:
            case RequiredFieldName.PrimaryProvider:
                if (listRelationships.SelectedIndex == -1)
                {
                    MsgBox.Show(this, "Please select 'Is' or 'Is not'.");
                    return;
                }
                if (listConditionValues.SelectedIndices.Count == 0)
                {
                    MsgBox.Show(this, "Please select a condition value.");
                    return;
                }
                List <long> listFkNums = new List <long>();
                if (selectedField == RequiredFieldName.PrimaryProvider)
                {
                    for (int i = 0; i < listConditionValues.SelectedIndices.Count; i++)
                    {
                        listFkNums.Add(_listProvs[listConditionValues.SelectedIndices[i]].ProvNum);
                    }
                }
                else if (selectedField == RequiredFieldName.BillingType)
                {
                    for (int i = 0; i < listConditionValues.SelectedIndices.Count; i++)
                    {
                        listFkNums.Add(_listBillingTypeDefs[listConditionValues.SelectedIndices[i]].DefNum);
                    }
                }
                else                            //selectedField==RequiredFieldName.Clinic
                {
                    for (int i = 0; i < listConditionValues.SelectedIndices.Count; i++)
                    {
                        if (listConditionValues.SelectedIndices[i] == 0)                                //'Unassigned'
                        {
                            listFkNums.Add(0);
                        }
                        else
                        {
                            listFkNums.Add(_listClinics[listConditionValues.SelectedIndices[i] - 1].ClinicNum);                                  //Subtract 1 to account for 'Unassigned'
                        }
                    }
                }
                //Delete the original conditions
                RequiredFieldConditions.DeleteAll(_listReqFieldConds.Select(x => x.RequiredFieldConditionNum).ToList());
                //Insert the new conditions
                condition = new RequiredFieldCondition();
                condition.RequiredFieldNum = _reqField.RequiredFieldNum;
                condition.ConditionType    = selectedField;
                if (listRelationships.SelectedIndex == 0)                       //'Is' is selected
                {
                    condition.Operator = ConditionOperator.Equals;
                }
                else                          //'Is not' is selected
                {
                    condition.Operator = ConditionOperator.NotEquals;
                }
                for (int i = 0; i < listFkNums.Count; i++)
                {
                    condition.ConditionValue = listFkNums[i].ToString();
                    RequiredFieldConditions.Insert(condition);
                }
                break;

            //Types that store the value of listConditionValues
            case RequiredFieldName.StudentStatus:
            case RequiredFieldName.PatientStatus:
            case RequiredFieldName.Position:
            case RequiredFieldName.Gender:
            case RequiredFieldName.Language:
            case RequiredFieldName.MedicaidID:
            case RequiredFieldName.MedicaidState:
                if (listRelationships.SelectedIndex == -1)
                {
                    MsgBox.Show(this, "Please select 'Is' or 'Is not'.");
                    return;
                }
                if (listConditionValues.SelectedIndices.Count == 0)
                {
                    MsgBox.Show(this, "Please select a value.");
                    return;
                }
                //Delete the original conditions
                RequiredFieldConditions.DeleteAll(_listReqFieldConds.Select(x => x.RequiredFieldConditionNum).ToList());
                //Insert the new conditions
                condition = new RequiredFieldCondition();
                condition.RequiredFieldNum = _reqField.RequiredFieldNum;
                condition.ConditionType    = selectedField;
                if (listRelationships.SelectedIndex == 0)                       //'Is' is selected
                {
                    condition.Operator = ConditionOperator.Equals;
                }
                else                          //'Is not' is selected
                {
                    condition.Operator = ConditionOperator.NotEquals;
                }
                for (int i = 0; i < listConditionValues.SelectedIndices.Count; i++)
                {
                    condition.ConditionValue = listConditionValues.Items[listConditionValues.SelectedIndices[i]].ToString();
                    RequiredFieldConditions.Insert(condition);
                }
                break;
            }
            DialogResult = DialogResult.OK;
        }
        private void listConditionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Intentionally using SelectedIndexChanged so that this code will run when the form loads
            if (listConditionType.SelectedIndex == -1)
            {
                return;
            }
            RequiredFieldName selectedField = _listIndexFieldNames[listConditionType.SelectedIndex];

            switch (selectedField)
            {
            case RequiredFieldName.DateTimeDeceased:
            case RequiredFieldName.AdmitDate:
                SetFieldVisibleHelper(false);
                labelConditionValue2.Visible = true;
                if (selectedField != _originalFieldName)
                {
                    break;
                }
                if (_listReqFieldConds.Count > 0)
                {
                    textConditionValue1.Text = _listReqFieldConds[0].ConditionValue;
                    SetOperatorCombo(1, _listReqFieldConds[0].Operator);
                }
                if (_listReqFieldConds.Count > 1)
                {
                    textConditionValue2.Text = _listReqFieldConds[1].ConditionValue;
                    SetOperatorCombo(2, _listReqFieldConds[1].Operator);
                    if (_listReqFieldConds[1].ConditionRelationship == LogicalOperator.And)
                    {
                        listRelationships.SelectedIndex = 0;
                    }
                    else                               //'Or'
                    {
                        listRelationships.SelectedIndex = 1;
                    }
                }
                break;

            case RequiredFieldName.Birthdate:
                SetFieldVisibleHelper(false);
                if (selectedField != _originalFieldName)
                {
                    break;
                }
                if (_listReqFieldConds.Count > 0)
                {
                    textConditionValue1.Text = _listReqFieldConds[0].ConditionValue;
                    SetOperatorCombo(1, _listReqFieldConds[0].Operator);
                }
                if (_listReqFieldConds.Count > 1)
                {
                    textConditionValue2.Text = _listReqFieldConds[1].ConditionValue;
                    SetOperatorCombo(2, _listReqFieldConds[1].Operator);
                    if (_listReqFieldConds[1].ConditionRelationship == LogicalOperator.And)
                    {
                        listRelationships.SelectedIndex = 0;
                    }
                    else                               //'Or'
                    {
                        listRelationships.SelectedIndex = 1;
                    }
                }
                break;

            case RequiredFieldName.Gender:
                SetFieldVisibleHelper(true);
                PatientGender[] arrayPatientGenders = (PatientGender[])Enum.GetValues(typeof(PatientGender));
                for (int i = 0; i < arrayPatientGenders.Length; i++)
                {
                    listConditionValues.Items.Add(Lan.g("enumPatientGender", arrayPatientGenders[i].ToString()));
                }
                ListValuesSetIndices();
                break;

            case RequiredFieldName.Position:
                SetFieldVisibleHelper(true);
                PatientPosition[] arrayPatientPositions = (PatientPosition[])Enum.GetValues(typeof(PatientPosition));
                for (int i = 0; i < arrayPatientPositions.Length; i++)
                {
                    listConditionValues.Items.Add(Lan.g("enumPatientPosition", arrayPatientPositions[i].ToString()));
                }
                ListValuesSetIndices();
                break;

            case RequiredFieldName.PatientStatus:
                SetFieldVisibleHelper(true);
                PatientStatus[] arrayPatientStatuses = (PatientStatus[])Enum.GetValues(typeof(PatientStatus));
                for (int i = 0; i < arrayPatientStatuses.Length; i++)
                {
                    listConditionValues.Items.Add(Lan.g("enumPatientStatus", arrayPatientStatuses[i].ToString()));
                }
                ListValuesSetIndices();
                break;

            case RequiredFieldName.StudentStatus:
                SetFieldVisibleHelper(true);
                listConditionValues.Items.Add(Lan.g(this, "Nonstudent"));
                listConditionValues.Items.Add(Lan.g(this, "Parttime"));
                listConditionValues.Items.Add(Lan.g(this, "Fulltime"));
                ListValuesSetIndices();
                break;

            case RequiredFieldName.Language:
                SetFieldVisibleHelper(true);
                listConditionValues.Items.Add(Lan.g(this, "none"));                       //regardless of how many languages are listed, the first item is "none"
                for (int i = 0; i < _listLanguages.Count; i++)
                {
                    if (_listLanguages[i] == "")
                    {
                        continue;
                    }
                    CultureInfo culture = CodeBase.MiscUtils.GetCultureFromThreeLetter(_listLanguages[i]);
                    if (culture == null)                           //custom language
                    {
                        listConditionValues.Items.Add(_listLanguages[i]);
                    }
                    else
                    {
                        listConditionValues.Items.Add(culture.DisplayName);
                    }
                }
                ListValuesSetIndices();
                break;

            case RequiredFieldName.Clinic:
                SetFieldVisibleHelper(true);
                listConditionValues.Items.Add(Lan.g(this, "Unassigned"));
                for (int i = 0; i < _listClinics.Count; i++)
                {
                    listConditionValues.Items.Add(_listClinics[i].Abbr);
                }
                ListValuesSetIndices();
                break;

            case RequiredFieldName.BillingType:
                SetFieldVisibleHelper(true);
                for (int i = 0; i < _listBillingTypeDefs.Count; i++)
                {
                    listConditionValues.Items.Add(_listBillingTypeDefs[i].ItemName);
                }
                ListValuesSetIndices();
                break;

            case RequiredFieldName.PrimaryProvider:
                SetFieldVisibleHelper(true);
                butPickProv.Visible = true;
                for (int i = 0; i < _listProvs.Count; i++)
                {
                    listConditionValues.Items.Add(_listProvs[i].GetLongDesc());                            //Only visible provs added to combobox.
                }
                ListValuesSetIndices();
                break;

            case RequiredFieldName.MedicaidID:
                SetFieldVisibleHelper(true);
                listConditionValues.Items.Add(Lan.g(this, "Blank"));
                ListValuesSetIndices();
                break;

            case RequiredFieldName.MedicaidState:
                SetFieldVisibleHelper(true);
                listConditionValues.Items.Add(Lan.g(this, "Blank"));
                ListValuesSetIndices();
                break;
            }
        }