import React from 'react';
import Taro from '@tarojs/taro';
import { View } from '@tarojs/components';
import { Button } from '@antmjs/vantui';
import { putTaIssue } from '@/services/taissue';
import { warn } from '@/utils/message';

export default (props) => {

  const { issue, formData } = props;

  const [loading, setLoading] = React.useState(false);
  
  // 新增问题单
  const onSubmit = () => {
    try {
      warn(!formData.addr, '请填写地址')
      warn(!formData.locId, '请选择点位')
      warn(!formData.content, '请填写问题描述')
      warn(!formData.typeId, '请选择问题分类')
      warn(!formData.attachList || formData.attachList.length < 1, '请上传照片')
    } catch (e) {
      return;
    }

    setLoading(true)
    putTaIssue(issue.issueId, { ...issue, ...formData }).then(() => {
      setLoading(false);
      Taro.navigateBack({
        delta: 1,
        fail: () => {
          Taro.reLaunch({
            url: '/pages/home/index'
          })
        } 
      });
    }).catch(() => {
      setLoading(false);
    })
  }
  
  return (
    <Button
      block
      plain
      hairline
      type="primary"
      loading={loading}
      onClick={onSubmit}
    >修改</Button>
  )
}
