/*
 * Create:      柿子
 * CreateDate:  2023/11/13
 * Describe：   备忘录
 * */
import React, { Component } from 'react';
import { List, Icon, Popconfirm, message } from 'antd';
import AddMemorandum from 'pages/nurse/BedChart/AddMemorandum';
import './style/index.less';

class Memorandum extends Component {
  constructor(props) {
    super(props);
    this.state = {
      userData: React.$getSessionData(),
      admID: '',
      locID: '',
      memoList: [], // 备忘录列表
      rowData: {},
    }
  };

  static getDerivedStateFromProps(nextProps, prevState) {
    if (nextProps.admID !== prevState.admID || nextProps.locID !== prevState.locID || nextProps.reloading !== prevState.reloading) {
      return {
        admID: nextProps.admID,
        locID: nextProps.locID,
        reloading: nextProps.reloading
      }
    }
    return null
  }

  componentDidMount() {
    this.props.onRef && this.props.onRef(this);
  };

  componentDidUpdate(nextProps, prevState) {
    if (prevState.admID !== this.state.admID || prevState.locID !== this.state.locID || prevState.reloading !== this.state.reloading) {
      this.getMemoList();
    }
  }

  componentWillUnmount() {
    this.setState = (state, callback) => {
      return
    }
  }

  // 获取备忘录列表
  getMemoList = async () => {
    try {
      const { userData, admID, locID } = this.state;
      if (!admID) {
        this.props.updateMemoDataFlag && this.props.updateMemoDataFlag('N'); // 更新备忘录数据标志
        this.setState({ memoList: [] });
        return;
      }
      const memoList = [{
        title: '测试',
        patName: '张三',
        descripts: '我要测试一下备忘录',
        id: '82'
      }, {
        title: '测试123',
        patName: '张三',
        descripts: '我要测试一下备忘录',
        id: '82'
      }, {
        title: '我是对',
        patName: '张三',
        descripts: '我要测试一下备忘录',
        id: '82'
      }, {
        title: '测撒地方试',
        patName: '张三',
        descripts: '撒地方士大夫但是说到底',
        id: '82'
      }];
      this.props.updateMemoDataFlag && this.props.updateMemoDataFlag(memoList && memoList.length > 0 ? 'Y' : 'N'); // 更新备忘录数据标志
      this.setState({ memoList });
      // let data = {
      //   params: [{
      //     admID,
      //     locID,
      //     userID: userData?.userID || '',
      //     active: 'Y',
      //   }],
      //   pagination: [{
      //     pageSize: 99999,
      //     currentPage: 1
      //   }]
      // }
      // let res = await React.$asyncPost(this, '04020016', data);
      // let memoList = res.result?.data || [];
      // this.props.updateMemoDataFlag && this.props.updateMemoDataFlag(memoList && memoList.length > 0 ? 'Y' : 'N'); // 更新备忘录数据标志
      // this.setState({ memoList });
    } catch (error) {
      console.log(error);
    }
  };

  // 删除
  handleDelete = async (e, item) => {
    try {
      React.$stopPropagation(e);
      let data = {
        params: [{
          ...item
        }]
      }
      let res = await React.$asyncPost(this, '123', data);
      message.success(res.errorMessage || '删除成功');
      this.getMemoList();
    } catch (error) {
      console.log(error);
    }
  };

  // 双机备忘录
  handleDoubleClickListItem = (e, record) => {
    React.$stopPropagation(e);
    this.setState({ rowData: record }, () => {
      this.addMemoRef && this.addMemoRef.editModal();
    });
  };

  // 添加备忘录
  modifyVisible = (visible) => {
    this.setState({ rowData: {} }, () => {
      this.addMemoRef && this.addMemoRef.showModal();
    })
  };

  render() {
    const { memoList, rowData, locID, admID } = this.state;
    return (
      <div className="ns-memorandum">
        <List
          itemLayout="horizontal"
          dataSource={memoList}
          renderItem={(item, index) => (
            <List.Item key={item?.id || index} onDoubleClick={(e) => this.handleDoubleClickListItem(e, item)}>
              <List.Item.Meta
                // avatar={<Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />}
                title={(
                  <div title={item?.title || ''} className="memo-list-item-ellipsis two-lines-ellipsis one-lines-ellipsis">
                    {item?.title || ''}
                  </div>
                )}
                description={(
                  <div title={item?.descripts || ''} className="memo-list-item-ellipsis two-lines-ellipsis">
                    {item?.descripts || ''}
                  </div>
                )}
              />
              <Popconfirm
                placement="topRight"
                title="删除不可恢复，你确定要删除吗?"
                onCancel={(e) => React.$stopPropagation(e)}
                onClick={(e) => React.$stopPropagation(e)}
                onConfirm={(e) => this.handleDelete(e, item, index)}
              >
                <div className="span common-record-delete-span">
                  <Icon type="delete" />
                </div>
              </Popconfirm>
            </List.Item>
          )}
        />

        {/* 添加备忘录 */}
        <AddMemorandum
          locID={locID}
          admID={admID}
          record={rowData}
          onRef={ref => this.addMemoRef = ref}
          componentType={this.props.componentType ? this.props.componentType : 'Delivery'}
          getMemoList={this.getMemoList}
        />
      </div>
    )
  }
};

export default Memorandum;