/*
 * Creator:     ML
 * CreatDate:   2020-03-21
 * descripts：  患者中心 预约记录
 * */
import React from 'react';
import { Toast, Button, List, Modal, WhiteSpace, } from 'antd-mobile';
import { $http } from "containers/config/https";
// import { Util } from 'tools';
import store from 'store';
import './style/index.less';
import AppointmentItem from './componts/AppointmentItem';
import PayTips from './componts/payTips'
const ListItem = List.Item;
const alert = Modal.alert;
class AppointmentRecord extends React.Component {
    constructor({ history }) {
        super();
        let userData = localStorage.getItem("userData") ? JSON.parse(localStorage.getItem("userData")) : {};
        this.state = {
            userData,
            smallClassModal: false,
            itemID: ""
        };
        this.history = history;
    }

    componentDidMount = () => {
        this.handleGetItem()
        if(store.getState().homeRedux && store.getState().homeRedux.treatmentItemsData){
            this.setState({
                info:store.getState().homeRedux && store.getState().homeRedux.treatmentItemsData
            })
        }
    }

    componentWillUnmount() {
        // 组件销毁前将静止让setState修改state的状态
        this.setState = (state, callback) => { return; }
    }

    //查询预约记录
    handleGetItem = () => {
        const { userData } = this.state
        let data = {
            params: [{
                hospCode: userData && userData.hospCode ? userData.hospCode : "",
                // cureAppID: "227"
                cureAppID: store.getState().homeRedux && store.getState().homeRedux.treatmentItemsData.cureAppID
            }],
        }
        $http.post("urlS", this, {
            code: "01040006",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({
                        projectArr: res.result
                    })
                }else{
                    Toast.info(res.errorMessage)
                }
            }
        });
    }
    //进入治疗记录 详情
    handleTreatmentRecord = (item) => {
        const { userData } = this.state
        let data = {
            params: [{
                bookScheduleID: item.bookScheduleID,
                hospCode: userData && userData.hospCode ? userData.hospCode : "",
            }],
        }
        $http.post("urlS", this, {
            code: "01040016",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    let item = res.result
                    this.history.push({
                        pathname: "/0002/0015",
                        state: { ...item }
                    })
                }else{
                    Toast.info(res.errorMessage)
                }
            }
        });
    }
    //取消预约
    handleClancelAppiontRecord = (item) => {
        alert('取消预约', '确定取消???', [
            { text: '取消', onPress: () => console.log('cancel') },
            {
                text: '确定', onPress: () => {
                    let arr = []
                    arr.push(item.bookScheduleID)
                    let data = {
                        params: [{
                            bookScheduleIDArr: arr
                        }],
                    }
                    $http.post("urlS", this, {
                        code: "01040005",
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                this.handleGetItem()
                            }else{
                                Toast.info(res.errorMessage)
                            }
                        }
                    });
                }
            },
        ])
    }
    //进入预约治疗
    handleAppointment = () => {
        let item = store.getState().homeRedux && store.getState().homeRedux.treatmentItemsData
        this.history.push({
            pathname: "/0002/0014",
            state: { ...item }
        })
        store.dispatch({
            type: "treatmentItems",
            value: { ...item }
        });
    }
    //查看申请
    handleCheckApply = (item, flag) => {
        let obj = item
        if (!flag) {
            obj["disabled"] = true
            obj["edit"] = 0
            this.history.push({
                pathname: "/0002/0011",
                state: { ...obj }
            })
        } else {
            obj["disabled"] = false
            obj["edit"] = 1
            this.history.push({
                pathname: "/0002/0011",
                state: { ...obj }
            })
        }

    }
    //取消订单 回到首页
    toHome = () => {
        this.history.push({
            pathname: "0010",
            state: {}
        })
        let item = {}
        store.dispatch({
            type: "treatmentItems",
            value: { ...item }
        });
    }
    //点击付款
    handleToPay = (price,payNote) => {
        this.childPay.showModal(price,payNote)
        // let item = {
        //     count:111
        // }
        // this.setState({
        //     info:item
        // })
        // store.dispatch({
        //     type: "treatmentItems",
        //     value: { ...item }
        // });
    }
    onRef = (ref) => {
        this.childPay = ref
    }
     //支付成功
     payToNext=()=>{
        this.handleGetItem()
    }
    render() {
        const { projectArr } = this.state
        let ListItemOp = []
        projectArr && projectArr.map((item, i) => {
            return ListItemOp.push(
                <ListItem key={i}>
                    <ListItem className="treatmentOrder-top">
                        <div className="treatmentOrder-p">
                            <div>
                                <span className="line"></span>
                                <span>{item.bookDateTime}</span>
                                {/* <span style={{ marginRight: "6px" }}>2020-12-12</span>
                                <span>16:00</span> */}
                            </div>
                            <div className="status-dom">
                                <span>{(item.ordExecBill || "--") + "/"}</span>

                                <span>{item.cureBookStatus || "--"}</span>
                            </div>
                        </div>
                    </ListItem>
                    <div className="appoint-lists" style={{ padding: "6px 0 0 12px" }}>
                        <div>
                            <span className={(item.cureDoctorSex === '1' ? 'card-splice-boy' : 'card-splice-girl') + " AvtorImg"}></span>
                            <span className="info">
                                <span className="name">
                                    <span>{item.cureDoctorDesc}</span>
                                    <span>{item.appMedDesc}</span>
                                </span>
                                <span className="type">{item.cureLoc}</span>
                            </span>
                        </div>
                        <div>
                            <p style={{ color: "#22a9a9" }} onClick={this.handleAppiont}>排队序号<i style={{ margin: "0 4px" }}>{item.resSeqNo}</i></p>
                        </div>
                    </div>
                    <div className="treatmentOrder-bottom">
                        <div style={{ display: (item.cureBookStatusCode !== "AR" ? "flex" : "none") }}>
                            <Button style={{ display: (item.cureBookStatusCode === "F" ? "block" : "none") }} className="treatOrder" onClick={() => this.handleTreatmentRecord(item)}>治疗记录</Button>
                            <Button style={{ display: (item.cureBookStatusCode === "B" && (item.ordExecBillCode === "TB" || item.ordExecBillCode === "") ? "block" : "none") }} className="applyEdit" onClick={() => this.handleClancelAppiontRecord(item)}>取消预约</Button>
                            <Button onClick={()=>this.handleToPay((store.getState().homeRedux && store.getState().homeRedux.treatmentItemsData.patPrice),item.payNote)} style={{ display: (item.ordExecBillCode === "TB" ? "block" : "none") }} className="patOrder">付款</Button>
                        </div>
                        <div style={{ float: "left", paddingLeft: "16px", display: (item.cureBookStatusCode === "AR" && item.waitNo !== "" ? "block" : "none") }}>
                            <p style={{ float: "left", color: "#888" }}>
                                提示：您前面还有<span style={{ color: "blue", fontSize: "14px", fontWeight: "600" }}>{item.waitNo}</span>位患者等待
                            </p>
                        </div>
                    </div>
                </ListItem>
            )
        })
        return (
            <div className="appoint-dom appointRecord-dom treatmentOrder-dom" >
                <AppointmentItem info={this.state.info} handleCheckApply={this.handleCheckApply} toHome={this.toHome} hide={"hide"}></AppointmentItem>
                <WhiteSpace />
                <div className="patientRecord-dom">
                    <List>
                        {
                            projectArr && projectArr.length > 0 ? ListItemOp : <div style={{ textAlign: "center", height: "48px", lineHeight: "48px" }}>
                                暂无预约,点击去<span style={{ color: "#0079fe", margin: "0 2px" }} onClick={() => this.handleAppointment({})}>预约</span>
                            </div>
                        }
                    </List>
                </div>
                <PayTips onRef={this.onRef} payToNext={this.payToNext}></PayTips>
            </div>
        )
    }
};
export default AppointmentRecord;