import React, { useState, useEffect } from "react";
import axios from "axios";
import { message } from "antd";
export default ({ isPrised, num, memo_id, container_type, current_login, props }) => {
  const [flag, setFlag] = useState(undefined);
  const [number, setNumber] = useState(undefined);

  useEffect(() => {
    setFlag(isPrised);
    setNumber(num);
  }, []);

  function priseForums() {
    if (current_login) {
      axios
        .post(`/memos/${memo_id}/plus.json`, {
          container_type: container_type,
          id: memo_id,
          type: flag ? 0 : 1,
        })
        .then((result) => {
          setNumber(result.data.praise_count);
          setFlag(!flag);
          message.success(flag ? "取消点赞" : "已点赞")
        })
        .catch((error) => {
          message.error(error);
        });
    } else {
      props.showLoginDialog();
      return;
      // window.open("/login", "_blank");
    }
  }
  return (
    <div className="priseBox">
      <span onClick={priseForums}>
        <i
          className={
            flag ? "iconfont icon-dianzan" : "iconfont icon-dianzan-xian"
          }
        ></i>
      </span>
      <span>{number}</span>
    </div>
  );
};
