import React, { Fragment, useState, useRef, useEffect } from 'react';
import Styled from 'styled-components/native';
import SearchList, { SearchListContainer } from '~/Components/Listing/SearchList';
import EmptyView from '~/Components/Listing/EmptyView';
import { CreateButton, ListStyle, RightArrow } from '~/Components/Listing/Search';
import EmptyLayout from '~/Components/Layouts/EmptyLayout';
import { Util } from '~/Utils/utils';
import { usePopup } from '~/Components/UIControls/Popup';
import { TouchableOpacity, View, Text } from 'react-native';
import Checkbox from '~/Components/UIControls/Checkbox';
import Icon from '~/Components/UIControls/Icon';

const Item = ({item, index, onDelete=(item)=>{}, editable}) => {
    const zebra = index % 2 == 0 ? true : false;
    const complaint = item.FmsComplaints;
    return (
        <View style={{
            ...ListStyle.itemContainer, 
            backgroundColor: (zebra?'#fdfdfd':'#fff'),
        }}>
            <View style={{ ...ListStyle.vertical, flex:1}}>
                <View style={{ ...ListStyle.horizon,width:'100%',justifyContent:"space-between"}}>
                    <Text style={{ ...ListStyle.text,color:"#0b83e6" }}>
                        {Util.get(complaint, "ComplaintTypeName")}
                    </Text>
                    <Text style={{ ...ListStyle.text,paddingLeft:10,flex:1 }}>
                        {Util.get(complaint, "Name")}
                    </Text>
                </View>
                <View style={{ ...ListStyle.horizon,width:'100%',marginTop:10, justifyContent:"space-between"}}>
                    <Text style={{ ...ListStyle.textGray, lineHeight: 18 }}>
                        {'민원발생일'}
                    </Text>
                    <Text style={{ ...ListStyle.textGray, textAlign: "left", paddingRight:15, lineHeight: 18  }}>
                        {Util.get(complaint, "Dates.StartDate")}
                    </Text>
                </View>
                <View style={{ ...ListStyle.horizon,width:'100%',marginTop:5, justifyContent:"space-between"}}>
                    <Text style={{ ...ListStyle.textGray, lineHeight: 18 }}>
                        {'조치완료일'}
                    </Text>
                    <Text style={{ ...ListStyle.textGray, textAlign: "left", paddingRight:15, lineHeight: 18  }}>
                        {Util.get(complaint, "Dates.EndDate")}
                    </Text>
                </View>
            </View>
            {editable && (
                <TouchableOpacity onPress={() => {onDelete(item)}} style={{marginRight:15}}>
                    <Icon iconFamily="AntDesign" iconName="closecircle" color="#9aa3ac" size={40} />
                </TouchableOpacity>
            )}
        </View>
    )
}

export default Screen = ({navigation, keys, complaints, setComplaints, editable}) => {
    const { openPopup } = usePopup();

    const appendComplaints = (list) => {
        let appendList = [];
        for(let add of list) {
            if(typeof complaints.find(item => item.ComplaintId == add.ComplaintId) === 'undefined') {
                appendList.push({
                    ...keys,
                    ComplaintId:add.ComplaintId,
                    FmsComplaints: add
                });
            }
        }

        if(appendList.length > 0) {
            setComplaints([...complaints, ...appendList]);
        }
    }

    const removeComplaint = (remove) => {
        const copied = [...complaints];
        const found = copied.find(item => item.ComplaintId == remove.ComplaintId);
        const index = copied.indexOf(found);
        if(index != -1) copied.splice(index, 1);
        setComplaints([...copied]);
    }

    return (
        <EmptyLayout>
            <SearchListContainer>
                <SearchList data={complaints} renderItem={({ item, index }) => {
                        return (
                            <Item item={item} index={index} onDelete={removeComplaint} editable={editable} />
                        )
                    }}
                    ListEmptyComponent={<EmptyView list={complaints} emptyTitle="민원목록을 추가하세요." />}
                />
                {editable && (
                    <CreateButton onPress={() => {
                        openPopup('/Popup/Complaint/Index', {siteId: keys.SiteId}, (results) => {
                            appendComplaints(results);
                        });
                    }} />
                )}
            </SearchListContainer>
        </EmptyLayout> 
    );
}