import * as WebBrowser from 'expo-web-browser';
import * as React from 'react';
import { Div, Button, Image, Text, Avatar } from 'react-native-magnus';
import { ScrollView } from 'react-native-gesture-handler';
import { InputItem } from '@ant-design/react-native';
import { useRequest, useCreation } from '@umijs/hooks';
import { useTranslation } from 'react-i18next';
import useModel from 'flooks';
import User from '../../flooks/User'; // detail模块通用方法
import Toast from '../../flooks/Toast'; // detail模块通用方法

import Header from '../../components/Header';
import request from '../../Utils/RequestUtils';

function saveRequest(data) {
  return request.post('/cooperateApply/save', {
    data,
  });
}

export default function MerchatApplyScreen({ navigation }) {
  const { t } = useTranslation();
  const { userInfo } = useModel(User, ['id']);
  const { warnning, success } = useModel(Toast, []);

  const [name, setname] = React.useState('');
  const [phone, setphone] = React.useState('');
  const [merName, setmerName] = React.useState('');
  const [category, setcategory] = React.useState('');
  const [address, setaddress] = React.useState('');

  const allpyRequest = useRequest((data) => saveRequest(data), {
    manual: true,
    onError: (e) => {
      warnning(e.error);
    },
    onSuccess: () => {
      success(t('shen-qing-ti-jiao-cheng-gong'));
      navigation.goBack();
    },
  });

  const canSubmit = useCreation(() => {
    if (name && phone && merName && category && address) {
      return true;
    } else {
      return false;
    }
  }, [name, phone, merName, category, address]);

  return (
    <>
      <Header title={t('kai-dian-shen-qing')} />
      <ScrollView
        contentContainerStyle={{
          backgroundColor: '#fff',
          flexGrow: 1,
        }}
      >
        <Div bg="white">
          <InputItem
            clear
            type="phone"
            value={name}
            onChange={setname}
            placeholder={t('shu-ru-lian-xi-ren')}
            style={{ fontSize: 14 }}
          >
            <Text fontSize="sm">{t('lian-xi-ren')}</Text>
          </InputItem>

          <InputItem
            clear
            type="phone"
            value={phone}
            onChange={setphone}
            placeholder={t('shu-ru-lian-xi-dian-hua')}
            style={{ fontSize: 14 }}
          >
            <Text fontSize="sm">{t('lian-xi-dian-hua')}</Text>
          </InputItem>

          <InputItem
            clear
            value={merName}
            onChange={setmerName}
            placeholder={t('shu-ru-dian-pu-ming-cheng')}
            style={{ fontSize: 14 }}
          >
            <Text fontSize="sm">{t('dian-pu-ming-cheng')}</Text>
          </InputItem>

          <InputItem
            clear
            value={category}
            onChange={setcategory}
            placeholder={t('shu-ru-jing-ying-lei-xing')}
            style={{ fontSize: 14 }}
          >
            <Text fontSize="sm">{t('jing-ying-lei-xing')}</Text>
          </InputItem>
          <InputItem
            clear
            value={address}
            onChange={setaddress}
            placeholder={t('qing-shu-ru-dian-pu-di-zhi')}
            style={{ fontSize: 14 }}
          >
            <Text fontSize="sm">{t('dian-pu-di-zhi')}</Text>
          </InputItem>
        </Div>

        <Button
          my={30}
          mx={130}
          fontSize="sm"
          block
          bg="brand500"
          disabled={!canSubmit}
          onPress={() => {
            allpyRequest.run({
              name,
              phone,
              merName,
              category,
              address,
              userId: userInfo.id,
            });
          }}
        >
          {t('ti-jiao')}
        </Button>
      </ScrollView>
    </>
  );
}
