import React, { useState, useEffect } from 'react';
import { Link, history } from 'umi';
import { getLeftMenu, getBtnText, nextNode, getLeader } from './service';
import { Menu, Button, Modal, message } from 'antd';
import { getRoomId, getSessionUserData } from '@/utils/session';

import './index.less';
const { SubMenu } = Menu;
let tips = '确认是否进入下一步';
let role = '';
const Index: React.FC<{}> = () => {
  const [data, setData] = React.useState<any>([]);

  // const [OpenKeys, setOpenKeys] = React.useState<any[]>([]);
  // const [selectedKeys, setSelectedKeys] = React.useState<any[]>([]);

  const [isModalVisible, setIsModalVisible] = useState(false);
  const [leader, setLeader] = useState('');
  const [btnText, btnTextSet] = useState('');
  const [count, countSet] = useState<any>(0);//触发useEffect
  const showModal = () => {
    setIsModalVisible(true);
  };


  const handleOk = async() => {
    setIsModalVisible(false);
    // if (btnText = '开启轮次') {
    //   openTurn({assessRoomId:getRoomId()}).then(res=>{
    //     alert('开启')
    //   }) 
    // }
    // if (btnText = '结束轮次') {
    //   finishTurn(getRoomId(), false).then(res=>{

    //   }) 
    // }
    if (btnText == '下一步') {
      await nextNode(getRoomId(), true).then(res => {
        console.log(res);

      })
    }
    countSet(count + 1);//触发useEffect
    // else {
    //   finishFlow(getRoomId()).then(res=>{

    //   })
    // }
  };

  const handleCancel = () => {
    setIsModalVisible(false);
  };

  useEffect(() => {

    if (getSessionUserData().roleIds == 'daili') {
      role = 'manager'
    } else if (getSessionUserData().roleIds == 'gys') {
      role = 'supplier'
    } else if (getSessionUserData().roleIds == 'zhuanjia') {
      role = 'jury';
      getLeader(getRoomId(), getSessionUserData().userId).then(res => {
        if (res.code == 200) {
          setLeader(res.data)
        }
      })
    }
    getLeftMenu(getRoomId(), role).then(res => {
      if (res.code == 200) {
        setData(res.data)
        // console.log(res.data.status);
        // const open = res.data.map(item=> item.status)
        // setSelectedKeys()
        // setOpenKeys(open)
      } else {
        message.info('请联系管理员')
      }

    })

    getBtnText(getRoomId()).then(res => {
      console.log(res.data)
      let btntxt = ''
      if (res.code == 200) {
        if (res.data.openTurnButton) {
          btntxt = '开启轮次'
        }
        else if (res.data.finishTurnButton) {
          btntxt = ''
        }
        else if (res.data.nextStepButton) {
          btntxt = '下一步'
        }
        else if (res.data.finishAllTurn) {
          btntxt = ''
        }
      }
      btnTextSet(btntxt);
    })

    if(role === 'jury'){
      setTimeout(()=>{
        countSet(count+1);
      },1000)
    }
  }, [count]);

  return (
    <>
      <div className="myselfContent">
        <Menu
          style={{ width: "180px", height: "100vh" }}
          defaultSelectedKeys={['1']}
          defaultOpenKeys={['1']}
          mode="inline"
          className="left-menu"
        >
          {
            data.map((item: any) => (
              <SubMenu key={item.status} title={item.instTurnName}>
                {item.nodeVOList.map((item2: any) => {
                  let url = item2.moduleUrl + leader + `?turnId=${item2.instTurnId}`;
                  // console.log(item2);
                  return <Menu.Item key={item2.status} disabled={item2.status == 3 ? true : false}>
                    {/* <Link to={url}>{item2.instNodeName}</Link> */}
                    <a onClick={() => { history.push(url) }}>{item2.instNodeName}</a>
                  </Menu.Item>
                })}
              </SubMenu>
            ))}
        </Menu>
        {//只有项目经理能控制流程
          role === 'manager' ?
            <Button onClick={showModal} style={{ position: "absolute", top: "400px", left: "60px" }}>{btnText}</Button>
            : null
        }
        <Modal title="确认是否进入下一步" visible={isModalVisible} onOk={handleOk} onCancel={handleCancel}>
          <p>{tips}</p>
        </Modal>
      </div>
    </>
  )
}
export default Index