import React from "react";
import Taro from "@tarojs/taro";
import { View } from "@tarojs/components";
import { Button } from "@antmjs/vantui";
import { postTaIssue, postIssueAssigned } from "@/services/taissue";
import { warn } from "@/utils/message";
import { useModel } from "@/store";
import { ROLE_INSPECTOR } from "@/utils/user";
export default (props) => {
  const { formData } = props;
  const { duty } = useModel("user");
  const [loading, setLoading] = React.useState(false);

  function jump() {
    return Taro.reLaunch({
      url: "/pages/home/index",
    });
  }

  // 新增问题单
  const onSubmit = () => {
    console.log(formData);
    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);
    postTaIssue(formData)
      .then((res) => {
        const data = { ...res, ...formData };
        formData.orgId
          ? postIssueAssigned({ ...data, nextOrg: formData.orgId })
              .then(() => {
                setLoading(false);
                jump(); //跳转
              })
              .catch((e) => {
                setLoading(false);
              })
          : jump();
      })
      .catch(() => {
        setLoading(false);
      });
  };

  return (
    <Button block type="primary" loading={loading} onClick={onSubmit}>
      提交
    </Button>
  );
};
