import { followUpAProjectManager, followUpAProjectSupplier, getSessionRoleData } from "@/utils/session";
import { Button, Form, Input, Modal } from "antd"
import React, { useEffect, useState } from "react"
import { describeSiteMsgDetail, selectMsgRead, getProjectById } from '../service'
import { history } from 'umi';

const { TextArea } = Input;

interface filesManageComponents {
  modalVisible: boolean;
  messId: string;
  onCancel: () => void;
}

const layout = {
  labelCol: { span: 6 },
  wrapperCol: { span: 15 },
};

const filesManageComponents: React.FC<filesManageComponents> = (props) => {
  const { modalVisible, messId, onCancel } = props;
  const [form] = Form.useForm();
  const [knowVisible, setKnowVisible] = useState<any>(false); // 知道了禁用
  const [detailId, setDetailId] = useState<any>(); // 详情id
  const [projectId, setProjectId] = useState<any>(); // 项目id
  const [roomType, setRoomType] = useState<any>(); // 判断资审候审
  const [routeType, setRouteType] = useState<any>(); // 判断澄清提疑
  const [disFollowUp, setDisFollowUp] = useState<boolean>(false) //跟进按钮隐藏
  const role = getSessionRoleData().roleCode;

  useEffect(() => {
    Int();
  }, [messId]);
  const Int = () => {
    describeSiteMsgDetail(messId).then(res => {
      // if(res.code == 200){
      res.authorizestate == '0' ? setKnowVisible(false) : setKnowVisible(true)
      form.setFieldsValue({
        "title": res.title,
        "createtime": res.createtime,
        "content": res.content
      });
      setDetailId(res.msgId)
      if (res?.servicecode) {
        let detailMess = JSON.parse(res?.servicecode)
        setProjectId(detailMess.tp_id)
        setRoomType(detailMess.room_type)
        setRouteType(detailMess.type)
      } else {
        // message.error('项目数据错误，无法获取流程，请联系管理员！')
        setDisFollowUp(true)
      }
      // }
    });
  };

  const getRead = () => { // 知道了
    selectMsgRead(detailId).then(res => {
      setKnowVisible(true)
    });
  }

  const getFollow = async () => { // 跟进
    if (role == "ebtp-supplier") { // 供应商
      await getProjectById(projectId).then(async response => {
        if (response?.code == 200 && response?.success == true) {
          const resData = response?.data
          await followUpAProjectSupplier(resData);

          if (routeType == 'clarify') {
            if (roomType == '1') {
              history.push('/ProjectLayout/ZYuShen/Tender/supplier/SupplierClarificationList?roomType=1')
            } else {
              history.push('/ProjectLayout/Tender/supplier/SupplierClarificationList?roomType=2')
            }
          } else {
            history.push('/ProjectLayout/Tender/supplier/SupplierQuestionsOrObjections')
          }
        }
      })
    } else if (role == "ebtp-agency-project-manager" || role == 'ebtp-purchase') { // 项目经理、采购经理
      await getProjectById(projectId).then(async response => {
        if (response?.code == 200 && response?.success == true) {
          const resData = response?.data
          await followUpAProjectManager(resData);

          if (routeType == 'clarify') {
            if (roomType == '1') {
              history.push('/ProjectLayout/ZYuShen/Tender/ProjectManager/ClarifyTheList?roomType=1')
            } else {
              history.push('/ProjectLayout/Tender/ProjectManager/ClarifyTheList?roomType=2')
            }
          } else {
            history.push('/ProjectLayout/Tender/ProjectManager/MentionDoubtReply')
          }
        }
      })
    }
  }

  return (
    <>
      <Modal
        destroyOnClose={true}
        getContainer={false}
        title='消息详情'
        visible={modalVisible}
        onCancel={() => onCancel()}
        width={800}
        centered
        footer={[
          <Button onClick={onCancel}>关闭</Button>,
          <Button disabled={knowVisible} type="primary" onClick={() => getRead()}>知道了</Button>,
          <Button disabled={disFollowUp} onClick={() => getFollow()} type="primary">跟进</Button>
        ]}
      >
        <Form
          {...layout}
          name="basic"
          form={form}
        >

          <Form.Item
            label="标题"
            name="title"
          >
            <Input readOnly bordered={false} />
          </Form.Item>
          <Form.Item
            label="创建时间"
            name="createtime"
          >
            <Input readOnly bordered={false} />
          </Form.Item>
          <Form.Item
            label="内容"
            name="content"
          >
            <TextArea readOnly bordered={false} />
          </Form.Item>
        </Form>
      </Modal>
    </>
  )
}

export default filesManageComponents
