import React, { useState, useRef, useEffect } from 'react';
import { Modal, Tabs, message } from 'antd';
import PlatformTest from './components/PlatformTest';
import ShortMessageTest from './components/ShortMessageTest';
import WeChatTest from './components/WeChatTest';
import EnterpriseWeChatTest from './components/EnterpriseWeChatTest';

import { getWebModuleTree } from '@/services/webConfig/api';
import { getMiniAppModuleTree } from '@/services/mobileConfig/api';
import { PushTest } from '@/services/messagemanage/messagemanage';
import styles from './schemeTest.less';
const SchemeTest = props => {
  const { visible, onCancel, onOk, schemeMsg, testPersons } = props;
  const [menuWebList, setMenuWebList] = useState([]); // web菜单列表
  const [menuMoblieList, setMenuMoblieList] = useState([]); // app菜单列表
  const PlatformTestRef = useRef();
  const ShortMessageTestRef = useRef();
  const WeChatTestRef = useRef();
  const EnterpriseWeChatTestRef = useRef();
  useEffect(() => {
    if (visible) {
      console.log(schemeMsg);
      // getAppTRee();
      // getWebTree();
    }
  }, [visible]);

  // 获取apptree数据
  const getAppTRee = () => {
    getMiniAppModuleTree({
      userMode: 'super',
    })
      .then(res => {
        if (res.code === 0) {
          let list = res.data.find(item => item.id === 'MiniAppSite');
          let applist = list.children.map(item => ({
            ...item,
            children: item.children[1].children,
          }));
          setMenuMoblieList(applist);
        } else {
          message.error(res.msg);
        }
      })
      .catch(err => {
        message.error('网络异常请稍后再试');
      });
  };
  const getWebTree = () => {
    getWebModuleTree('super')
      .then(res => {
        const { code, data } = res;
        if (code === 0) {
          let list = data.find(item => item.id === 'Web4SingleStation');
          let weblist = list.children.map(item => ({
            ...item,
            children: item.children[1].children,
          }));
          setMenuWebList(weblist);
        } else {
          message.error(res.msg);
        }
      })
      .catch(err => {
        message.error('网络异常请稍后再试');
      });
  };
  const onFinish = () => {
    console.log(testPersons.join(','), 'testPersons');
    let obj = {
      id: schemeMsg.Id,
      tousers: testPersons.join(','),
      ...PlatformTestRef?.current?.getParmar(),
      ...ShortMessageTestRef?.current?.getParmar(),
      ...WeChatTestRef?.current?.getParmar(),
      ...EnterpriseWeChatTestRef?.current?.getParmar(),
    };
    console.log(obj);

    PushTest(obj).then(res => {
      if (res.code === 0) {
        message.success('发送成功');
        onCancel();
      } else {
        message.error(res.msg);
      }
    });
  };
  return (
    <div>
      <Modal
        visible={visible}
        onCancel={onCancel}
        onOk={onFinish}
        title="消息测试"
        width={900}
        maskClosable={false}
        destroyOnClose
      >
        <div className={styles.schemeTestContent}>
          <Tabs>
            {schemeMsg?.Platform?.PlatformStatus ? (
              <Tabs.TabPane tab="平台消息" key="1">
                <div style={{ height: '100%', overflowY: 'scroll', paddingRight: '5px' }}>
                  <PlatformTest
                    ref={PlatformTestRef}
                    id={schemeMsg ? schemeMsg?.Id : ''}
                    menuWebList={menuWebList}
                    menuMoblieList={menuMoblieList}
                    formValue={schemeMsg?.Platform}
                  />
                </div>
              </Tabs.TabPane>
            ) : null}
            {schemeMsg?.SMS?.SMSStatus ? (
              <Tabs.TabPane tab="短信消息" key="2">
                <div style={{ height: '100%', overflowY: 'scroll' }}>
                  <ShortMessageTest ref={ShortMessageTestRef} formValue={schemeMsg?.SMS} />
                </div>
              </Tabs.TabPane>
            ) : null}

            {schemeMsg?.Wechat?.WechatStatus ? (
              <Tabs.TabPane tab="微信公众号" key="3">
                <div
                  style={{
                    height: '100%',
                    overflowY: 'scroll',
                  }}
                >
                  <WeChatTest
                    ref={WeChatTestRef}
                    id={schemeMsg ? schemeMsg?.Id : ''}
                    menuWebList={menuWebList}
                    menuMoblieList={menuMoblieList}
                    formValue={schemeMsg?.Wechat}
                  />
                </div>
              </Tabs.TabPane>
            ) : null}
            {schemeMsg?.WorkWechat?.WorkWechatStatus ? (
              <Tabs.TabPane tab="企业微信" key="4">
                <div style={{ height: '100%', overflowY: 'scroll' }}>
                  <EnterpriseWeChatTest
                    ref={EnterpriseWeChatTestRef}
                    id={schemeMsg ? schemeMsg?.Id : ''}
                    menuWebList={menuWebList}
                    menuMoblieList={menuMoblieList}
                    formValue={schemeMsg?.WorkWechat}
                  />
                </div>
              </Tabs.TabPane>
            ) : null}
          </Tabs>
        </div>
      </Modal>
    </div>
  );
};

export default SchemeTest;
