import React, {useState} from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  Image,
  StyleSheet,
  KeyboardAvoidingView,
  ScrollView,
  Platform,
  Dimensions,
  Alert,
} 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';
const {width} = Dimensions.get('window');
const dataType = [
  {
    value: '0',
    name: 'Tài khoản ngân hàng',
  },
  {
    value: '1',
    name: 'Tài khoản thẻ',
  },
];

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

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

    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 = ['loại thẻ', 'mã ngân hàng', 'số thẻ/tài khoản'];
    const index = checkFormatArray([type, acc_type, account_no]);
    console.log(props.route.params.body);
    if (index === true) {
      const data = createFormData(
        props.route.params.img.urlBack,
        props.route.params.img.urlFont,
        props.route.params.img.urlSign,
        {
          uid: 17,
          acc_type: 0,
          account_no: '123465789',
          bank_no: '970423',
          ...props.route.params.body,
        },
      );
      const res = await verifyAccountApi(data);

      console.log('Response', res.data);
    } else {
      Alert.alert('Vui lòng điền ' + titles[index]);
    }
  };

  return (
    <KeyboardAvoidingView
      behavior={Platform.Os === 'ios' ? 'padding' : 'height'}
      style={{flex: 1}}
      keyboardVerticalOffset={-50}>
      <ScrollView showsVerticalScrollIndicator={false}>
        <View
          style={{
            flex: 1,
            paddingHorizontal: 10,
            paddingTop: 10,
          }}>
          <Text
            style={{
              fontSize: getFontXD(42),
              color: R.colors.color777,
            }}>
            Loại thẻ
          </Text>
          <PickerItem
            width={width - 20}
            data={dataType}
            onValueChange={(value, items) => {
              setType(items);
            }}
          />
          <TextField
            isNumber={true}
            title={'Mã ngân hàng'}
            onChangeText={(val) => setAccType(val)}
          />
          <TextField
            title={'Số thẻ/tài khoản'}
            isNumber={true}
            onChangeText={(val) => setAccountNo(val)}
          />

          <View style={styles.btnSend}>
            <Button title={'Xác minh'} onClick={onPress} />
          </View>
        </View>
      </ScrollView>
    </KeyboardAvoidingView>
  );
};

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

export default BankInfor;
