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

export default (props) => {

  const { issue, formData } = props;

  const [loading1, setLoading1] = React.useState(false);
  
  // 交办
  const onAssigned = () => {
    try {
      warn(!formData.addr, '请填写地址')
      warn(!formData.locId, '请选择点位')
      warn(!formData.content, '请填写问题描述')
      warn(!formData.typeId, '请选择问题分类')
      warn(!formData.attachList || formData.attachList.length < 1, '请上传照片')
      warn(!formData.orgId, '请选择交办单位')
      warn(!formData.expireDate, '请选择办结时间')
    } catch (e) {
      return;
    }

    setLoading1(true)

    const data = { ...issue, ... formData };

    // 先提交修改
    putTaIssue(issue.issueId, data).then(() => {      
      // 再提交交办
      postIssueAssigned({ ... data, nextOrg: data.orgId}).then(() => {
        setLoading1(false);
        Taro.navigateBack({delta: 1});
      }).catch(() => {
        setLoading1(false);
      })
    }).catch(() => {
      setLoading1(false);
    })
  }
 
  return (
    <Button
      block
      type="primary"
      loading={loading1}
      onClick={onAssigned}
      style={{marginTop: '20px'}}
    >交办</Button>
  )
}
