// 选择院区
import React from 'react';
import {List} from 'antd-mobile';
import './style/index.less';
import { $http } from "containers/config/https";

const Item = List.Item;

export default class SelectADistrict extends React.Component {
  constructor({history}) {
    super();
    let userData = localStorage.getItem("userData") ? JSON.parse(localStorage.getItem("userData")) : {};
    this.state = {
      userData,
      departmentList: [],
      latestRegistrationNum: 0
    }
    this.history=history;
  }

  componentDidMount() {
    this.getDepartmentList();
    this.getTheLatestRegistrationData();
  }

  // 获取科室列表
  getDepartmentList = () => {
    let {userData} = this.state;
    let data = {
			params: [{
        hospID: userData&&userData.hisHospID ? userData.hisHospID : "",
        userID: "",
        groupID: ""
      }]
		}
		$http.post("urlS", this, {
			code: "03040001",
			data: data,
			success: function(res) {
				if (+res.errorCode === 0) {
          let departmentList = res.result.Data&&res.result.Data.length > 0 ? res.result.Data : [];
          this.setState({departmentList});
				} else {
					this.setState({departmentList: []});
				}
			}
		});
  }

  // 获取最近挂号相关数据
  getTheLatestRegistrationData = () => {
    let {userData} = this.state;
    let data = {
			params: [{
        hospID: userData&&userData.hisHospID ? userData.hisHospID : "",
        patNo: "0000000002",
      }]
		}
		$http.post("urlS", this, {
			code: "03040005",
			data: data,
			success: function(res) {
				if (+res.errorCode === 0) {
          this.setState({latestRegistrationNum: res.result&&res.result.totalCount ? res.result.totalCount : 0});
				} else {
					this.setState({latestRegistrationNum: 0});
				}
			}
		});
  }

  // 跳转到预约挂号
  handleGoAppointmentRegistration = (item) => {
    this.history.push({
      pathname: "/0002/00040002",
      state: {...item}
    })
  }

  render() {
    let {departmentList, latestRegistrationNum} = this.state;
    let departmentListDom = departmentList&&departmentList.map((item, index) => {
      return <Item key={index} arrow="horizontal" onClick={() => this.handleGoAppointmentRegistration(item)}>{item.descripts}</Item>
    })
    return (
      <div className="electronic-health-card" style={{height: "100%"}}>
        <div style={{height: "100%", overflow: "auto"}} className="select-a-district">
          <div className="header-search">
            <span>
              <i className="iconfont icon-sousuo"></i>
              搜索
            </span>
          </div>
          <div className="content-loc">
            {/* <List renderHeader={() => '常用'} className="my-list">
              <Item extra={<div>共<span>{latestRegistrationNum}</span>名医生</div>} arrow="horizontal" onClick={() => {}}>最近挂号</Item>
            </List> */}
            <List className="my-list">
              <Item extra={<div>共<span>{latestRegistrationNum}</span>名医生</div>} arrow="horizontal" onClick={() => this.handleGoAppointmentRegistration({id: "最近挂号"})}>最近挂号</Item>
              {departmentListDom}
            </List>
          </div>
        </div>
      </div>
    )
  }
}