import React, {useState} from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  Image,
  StyleSheet,
  KeyboardAvoidingView,
  ScrollView,
  Platform,
  Dimensions,
  Alert,
  TouchableWithoutFeedback,
  Keyboard,
} from 'react-native';
import TextField from '../../../components/Input/TextField';
import TextMulti from '../../../components/Input/TextMulti';
import {checkFormatArray, getFontXD} from '../../../Config/Functions';
import PickerDate from '../../../components/Picker/PickerDate';
import R from '../../../assets/R';
import Button from '../../../components/Button';
import PickerItem from '../../../components/Picker/PickerItem';
import {verifyAccountApi} from '../../../apis/Functions/users';
import {connect} from 'react-redux';
import {showLoading, hideLoading} from '../../../actions/loadingAction';
import {useNavigation} from '@react-navigation/native';
import I18n from '../../../helper/i18/i18n';
import {showAlert, TYPE} from '../../../components/DropdownAlert';

const {width} = Dimensions.get('window');
const dataType = [
  {
    value: '0',
    name: I18n.t('AccountNumber'),
  },
  {
    value: '1',
    name: I18n.t('CardAccount'),
  },
];

const BankInfor = (props) => {
  const [acc_type, setAccType] = useState();
  const [bank_no, setBackNo] = useState('');
  const [account_no, setAccountNo] = useState('');

  const createFormData = (identity_after, identity_before, sign_img, body) => {
    const data = new FormData();

    data.append('identity_after', {
      name: 'identity_after.jpg',
      type: 'image/jpg',
      uri:
        Platform.OS === 'android'
          ? identity_after
          : identity_after.replace('file://', ''),
    });

    data.append('identity_before', {
      name: 'identity_before.jpg',
      type: 'image/jpg',
      uri:
        Platform.OS === 'android'
          ? identity_before
          : identity_before.replace('file://', ''),
    });

    data.append('sign_image', {
      name: 'sign_img.jpg',
      type: 'image/jpg',
      uri:
        Platform.OS === 'android' ? sign_img : sign_img.replace('file://', ''),
    });

    Object.keys(body).forEach((key) => {
      data.append(key, body[key]);
    });
    return data;
  };

  const onPress = async () => {
    const titles = [
      I18n.t('CardType').toLowerCase(),
      I18n.t('BankCode').toLowerCase(),
      I18n.t('CardNumberOrAccount').toLowerCase(),
    ];
    const index = checkFormatArray([acc_type, bank_no, account_no]);

    if (index === true) {
      const data = createFormData(
        props.route.params.img.urlBack,
        props.route.params.img.urlFont,
        props.route.params.img.urlSign,
        {
          uid: props.user.uid,
          acc_type: 0,
          bank_no,
          account_no,
          ...props.route.params.body,
        },
      );
      props.showLoading();
      const res = await verifyAccountApi(data);
      props.hideLoading();

      if (res.data.code == 200) {
        showAlert(TYPE.SUCCESS, I18n.t('Notification'), res.data.message);

        setBackNo('');
        setAccountNo('');
        setAccType('');
      } else {
        showAlert(TYPE.ERROR, I18n.t('Notification'), res.data.message);
      }
    } else {
      showAlert(
        TYPE.WARN,
        I18n.t('Notification'),
        I18n.t('Please_fill_in') + titles[index],
      );
    }
  };

  return (
    <KeyboardAvoidingView
      behavior={Platform.Os === 'ios' ? 'padding' : 'height'}
      style={{flex: 1}}
      keyboardVerticalOffset={-50}>
      <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
        <ScrollView showsVerticalScrollIndicator={false}>
          <View
            style={{
              flex: 1,
              paddingHorizontal: 10,
              paddingTop: 10,
            }}>
            <Text
              style={{
                fontSize: getFontXD(42),
                color: R.colors.color777,
              }}>
              {I18n.t('CardType')}
            </Text>
            <PickerItem
              width={width - 20}
              data={dataType}
              onValueChange={(value, items) => {
                setAccType(items);
              }}
            />
            <TextField
              isNumber={true}
              title={I18n.t('BankCode')}
              onChangeText={(val) => setBackNo(val)}
            />
            <TextField
              title={I18n.t('CardNumberOrAccount')}
              isNumber={true}
              onChangeText={(val) => setAccountNo(val)}
            />

            <View style={styles.btnSend}>
              <Button title={I18n.t('Confirm')} onClick={onPress} />
            </View>
          </View>
        </ScrollView>
      </TouchableWithoutFeedback>
    </KeyboardAvoidingView>
  );
};

const styles = StyleSheet.create({
  btnSend: {
    width: '100%',
    marginTop: 50,
  },
});

const mapStateToProps = (state) => {
  return {
    user: state.userReducer,
  };
};
export default connect(mapStateToProps, {showLoading, hideLoading})(BankInfor);
