// 我的收货地址
import React from 'react';
import { List } from 'antd-mobile';
// import "../electronicPrescription/style/index.less";
// import "../patient/style/index.less";
import "./style/index.less";
import { $http } from "containers/config/https";

export default class MyHarvestAddress extends React.Component {
  constructor({history}) {
    super();
    let weChatParams = sessionStorage.getItem("weChatParams") ? JSON.parse(sessionStorage.getItem("weChatParams")) : {};
    this.state = {
      weChatParams,
      deliveryAddressListData: []
    };
    this.history = history;
  }

  componentDidMount() {
    this.getDeliveryAddressListData();
  }

  // 获取配送地址
  getDeliveryAddressListData = () => {
    let {weChatParams} = this.state;
		let data = {
			params: [{
				openID: weChatParams.openID || "",
				pubAccCode: weChatParams.pubAccCode || "",
			}]
		}
		$http.post("urlS", this, {
			code: "01020006",
			data: data,
			success: function(res) {
				if (+res.errorCode === 0) {
          let resData = res.result&&res.result.rows&&res.result.rows.length > 0 ? res.result.rows : [];
          this.setState({deliveryAddressListData: resData});
				} else {
          this.setState({deliveryAddressListData: []});
        }
			}
		});
  }

  // 添加配送地址
  handleAddAddress = () => {
    this.history.push({
      pathname: "/0004/00010002",
      state: {}
    });
  }

  // 修改地址
  handleModify = (item, e) => {
    e.stopPropagation();
    this.history.push({
      pathname: "/0004/00010002",
      state: {...item}
    });
  }

  render() {
    let {deliveryAddressListData} = this.state;
    return (
      <div className="my-harvest-address">
        <List>
          {deliveryAddressListData&&deliveryAddressListData.map((item, index) => (
            <List.Item key={index} thumb={<span className="thumb-span">{item.name.length > 2 ? item.name.slice(0,1) : item.name}</span>} extra={<span onClick={(e) => this.handleModify(item, e)}>编辑</span>}>
              <div className="di-popup-list-childiten">
                <p><span>{item.name}</span>{item.mobile}</p>
                <p>{item.provinceDesc + item.cityDesc + item.areaDesc + item.villageDesc + item.address}</p>
              </div>
            </List.Item>
          ))}
          <List.Item className="add-address" arrow="horizontal" onClick={this.handleAddAddress}><i className="iconfont icon-tianjia"></i>添加收货地址</List.Item>
        </List>
        {/* <div className="add-card-button">
          <Button type="primary" onClick={this.handleAddAddress}>添加收货地址</Button>
          <WhiteSpace />
          <p style={{textAlign: "center", color: "#999", padding: "0 12px"}}>如需删除可通过编辑按钮进入地址维护界面进行删除。</p>
        </div> */}
      </div>
    )
  }
}