import React, { useEffect } from "react";
import { Text, TouchableOpacity,View,ScrollView } from "react-native";
import { customStyles,GetPageTheme } from "../styles/Global";
import { useRoute } from '@react-navigation/native';
import { AntDesign } from "./icons";
import { notify } from "./notify";

const SamplingHeaderTab = (props) => {
    const route = useRoute();
    const PageTheme=GetPageTheme(props.DarkTheme,route.name);
    const customStyle=customStyles(props.DarkMode,route.name);


    const currentMenu=props.currentMenu;
    const SamplingData=props.SamplingData || {};
    const SamplingTabObj=props.SamplingTabObj || {};
    const AllTabDisabled=props.AllTabDisabled!=null?props.AllTabDisabled:false;
    
    let storeData=props.storeData;
    let hasUnsavedChanges=props.hasUnsavedChanges
    let setShowAlert=props.setShowAlert
    let setSaveModalDObj=props.setSaveModalDObj
    let isSaleDependEnable=Boolean(SamplingTabObj.isSaleDependEnable)

    // const isCustomerConversionRequired=( SamplingData && SamplingData.CustomerConversionRequired!=""?SamplingData.CustomerConversionRequired : false);
    // const isCustomerTrackingRequired=SamplingData && SamplingData.CustomerTrackingRequired!=""?SamplingData.CustomerTrackingRequired : false;
    // const isSaleRequired=SamplingData && SamplingData.SaleRequired!=""?SamplingData.SaleRequired : false;
    // const isStockRequired=SamplingData && SamplingData.StockRequird!=""?SamplingData.StockRequird : false;
    // const isInventoryRequired=SamplingData && SamplingData.InventoryRequired!=""?SamplingData.InventoryRequired : false;

    const isInventoryRequired=SamplingData.InventoryRequired==true || SamplingData.InventoryRequired=="true"?1:0;
    const isCustomerTrackingRequired=SamplingData.CustomerTrackingRequired==true || SamplingData.CustomerTrackingRequired=="true"?1:0;
    const isCustomerConversionRequired=SamplingData.CustomerConversionRequired==true || SamplingData.CustomerConversionRequired=="true"?1:0;
    const isSaleRequired=SamplingData.SaleRequired==true || SamplingData.SaleRequired=="true"?1:0;
    const isStockRequired=SamplingData.StockRequird==true || SamplingData.StockRequird=="true"?1:0;
   
    
    // let inventoryDis= AllTabDisabled || SamplingTabObj ?false:(SamplingTabObj &&  SamplingTabObj.inventoryIsAvlbl==true );
    // let converDis=(SamplingTabObj &&  SamplingTabObj.allTabDisable) ?false:true;
    // let trackDis=(SamplingTabObj &&  SamplingTabObj.allTabDisable) ?false:(SamplingTabObj && SamplingTabObj.trackIsAvlbl==true );
    // let SaleDis=(SamplingTabObj &&  SamplingTabObj.allTabDisable) ?false:(SamplingTabObj && SamplingTabObj.SaleIsAvlbl==true );
    // let stockDis=(SamplingTabObj &&  SamplingTabObj.allTabDisable) ?false:(SamplingTabObj && SamplingTabObj.StockIsAvlbl==true );

    
    let inventoryDis= AllTabDisabled || SamplingTabObj.inventoryIsAvlbl==false?true:false;
    let converDis=AllTabDisabled;
    let trackDis= AllTabDisabled || SamplingTabObj.trackIsAvlbl==false?true:false;
    let SaleDis= AllTabDisabled || SamplingTabObj.SaleIsAvlbl==false?true:( isSaleDependEnable && SamplingTabObj.StockAvil==false && isStockRequired==true? true:false);
    // let SaleDis= AllTabDisabled || SamplingTabObj.SaleIsAvlbl==false?true:false;
    let stockDis=AllTabDisabled || SamplingTabObj.StockIsAvlbl==false?true:false;

    const commonPageParams={'SamplingData':SamplingData,'storeData':storeData,'menu':currentMenu};
    const SalePageParams={'SamplingData':SamplingData,'storeData':storeData,'menu':currentMenu,"SamplingSaleFlag":true};
    const ContactConvPageParams={'SamplingData':SamplingData,'storeData':storeData,'menu':currentMenu,"SamplingFlag":true,'showTotalSampled':true};
    
    function goToPage(screenName,params){
        if(hasUnsavedChanges){
            setSaveModalDObj({screenName,params});
            setShowAlert(true);
        }
        else{ 
            let movetoScreen=true;
            if(screenName=="SamplingStock"){
                if(SamplingTabObj.SaleIsDone==true){
                    movetoScreen=false;
                    notify("Sampling Sale has been filled! You cannot change stock now")
                }
            }

            if(movetoScreen){
                props.navigation.navigate(screenName,params);
            }
        }
    }
  
    return (
        <View style={[customStyle.storeTab_Con,customStyle.Sampling_HdrTWrap,customStyle.Samling_borderB,customStyle.pb10]}>
                {
                    (isInventoryRequired==true) &&
                    <TouchableOpacity style={[customStyle.storeTab_pill,{marginTop:10},(!inventoryDis?customStyle.storeTab_pillActive:{})]} disabled={inventoryDis} onPress={()=>{goToPage('SamplingInventory',commonPageParams); }}>
                        <Text style={[customStyle.storeTab_pillText,(!inventoryDis?customStyle.storeTab_pillActiveText:{color:PageTheme.$text_color})]}>{"INVENTORY"}</Text>
                        { ( SamplingTabObj.inventoryIsDone==true) &&
                            <View style={customStyle.Sampling_menu_done}><AntDesign color={(inventoryDis?PageTheme.$text_color_light:'green')} size={14} name='checkcircle'/></View>
                        }  
                    </TouchableOpacity>
                }
                {
                    (isCustomerTrackingRequired==true) &&
                    <TouchableOpacity style={[customStyle.storeTab_pill,{marginTop:10},(!trackDis?customStyle.storeTab_pillActive:{})]} disabled={trackDis} onPress={()=>{goToPage('SamplingCustTrack',commonPageParams);  }}>
                        <Text style={[customStyle.storeTab_pillText,(!trackDis?customStyle.storeTab_pillActiveText:{color:PageTheme.$text_color})]}>{"CUST-TRACK"}</Text>
                        { ( SamplingTabObj.trackIsDone==true) &&
                            <View style={customStyle.Sampling_menu_done}><AntDesign color={(trackDis?PageTheme.$text_color_light:'green')} size={14} name='checkcircle'/></View>
                        }  
                    </TouchableOpacity>
                }
                {
                    (isStockRequired==true) &&
                    <TouchableOpacity style={[customStyle.storeTab_pill,{marginTop:10},(!stockDis?customStyle.storeTab_pillActive:{})]} disabled={stockDis} onPress={()=>{goToPage('SamplingStock',commonPageParams); }}>
                        <Text style={[customStyle.storeTab_pillText,(!stockDis?customStyle.storeTab_pillActiveText:{})]}>{"STOCK"}</Text>
                        { ( SamplingTabObj.StockIsDone==true) &&
                            <View style={customStyle.Sampling_menu_done}><AntDesign color={(stockDis?PageTheme.$text_color_light:'green')} size={14} name='checkcircle'/></View>
                        } 
                    </TouchableOpacity>
                } 
                {
                    (isSaleRequired==true) &&
                    <TouchableOpacity style={[customStyle.storeTab_pill,{marginTop:10},(!SaleDis?customStyle.storeTab_pillActive:{})]} disabled={SaleDis} onPress={()=>{goToPage('SamplingStock',SalePageParams);  }}>
                        <Text style={[customStyle.storeTab_pillText,(!SaleDis?customStyle.storeTab_pillActiveText:{})]}>{"SALE"}</Text>
                        { ( SamplingTabObj.SaleIsDone==true) &&
                            <View style={customStyle.Sampling_menu_done}><AntDesign color={(SaleDis?PageTheme.$text_color_light:'green')} size={14} name='checkcircle'/></View>
                        } 
                    </TouchableOpacity>
                }
                
                {
                    (isCustomerConversionRequired==true) &&
                    <TouchableOpacity style={[customStyle.storeTab_pill,{marginTop:10},(!converDis?customStyle.storeTab_pillActive:{})]} disabled={converDis} onPress={()=>{goToPage('ContactConversion',ContactConvPageParams); }}>
                        <Text style={[customStyle.storeTab_pillText,(!converDis?customStyle.storeTab_pillActiveText:{})]}>{"CONVERSION"}</Text>
                        { ( SamplingTabObj.ContactIsDone==true) &&
                            <View style={customStyle.Sampling_menu_done}><AntDesign color={(converDis?PageTheme.$text_color_light:'green')} size={14} name='checkcircle'/></View>
                        } 
                    </TouchableOpacity>
                }  
        
            {/* </ScrollView> */}
        </View>
    );
};
export default SamplingHeaderTab;