/*
 * Creator:     ML
 * CreatDate:   2020-03-20
 * descripts：  患者中心 治疗订单列表
 * */
import React from 'react';
import { Toast, Button, Modal, List } from 'antd-mobile';
import { $http } from "containers/config/https";
// import { Util } from 'tools';
import store from 'store'
import './style/index.less';
import PayTips from './componts/payTips'
const ListItem = List.Item;
const alert = Modal.alert;
class TreatmentOrderList extends React.Component {
    constructor({ history }) {
        super();
        let userData = localStorage.getItem("userData") ? JSON.parse(localStorage.getItem("userData")) : {};
        this.state = {
            userData,
        };
        this.history = history;
    }

    componentDidMount = () => {
        this.handleGetItem()
    }

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

    }

    //查询治疗订单
    handleGetItem = (val) => {
        const { userData } = this.state
        let data = {
            params: [{
                hospCode: userData && userData.hospCode ? userData.hospCode : "",
                patID: userData && userData.HISPatID ? userData.HISPatID : "",
            }],
        }
        $http.post("urlS", this, {
            code: "01040011",
            data: data,
            success: function (res) {
                if (+res.errorCode === 0) {
                    this.setState({
                        projectArr: res.result.data
                    })
                } else {
                    Toast.info(res.errorMessage)
                }
            }
        });
    }
    //查看申请
    handleCheckApply = (item, flag) => {
        let obj = item
        if (!flag) {
            obj["disabled"] = true
            obj["edit"] = 0
            this.history.push({
                pathname: "/0002/0011",
                state: { ...obj }
            })
            store.dispatch({
                type: "treatmentItems",
                value: { ...obj }
            });
        } else {
            obj["disabled"] = false
            obj["edit"] = 1
            this.history.push({
                pathname: "/0002/0011",
                state: { ...obj }
            })
            store.dispatch({
                type: "treatmentItems",
                value: { ...obj }
            });
        }

    }
    //进入预约治疗
    handleAppointment = (item) => {
        // console.log("item",item)
        this.history.push({
            pathname: "/0002/0014",
            state: { ...item }
        })
        store.dispatch({
            type: "treatmentItems",
            value: { ...item }
        });
    }
    //进入预约记录
    handleAppiontRecord = (item) => {
        this.history.push({
            pathname: "/0002/0016",
            state: {}
        })
        store.dispatch({
            type: "treatmentItems",
            value: item
        });
    }
    //进入治疗记录
    handleTreatmentRecord = (item) => {
        this.history.push({
            pathname: "/0002/0018",
            state: { ...item }
        })
        store.dispatch({
            type: "treatmentItems",
            value: item
        });
    }
    //取消治疗订单
    handleCancel = (item) => {
        // const {userData}=this.state
        alert('取消订单', '确定取消???', [
            { text: '取消', onPress: () => console.log('cancel') },
            {
                text: '确定', onPress: () => {
                    const { userData } = this.state
                    let data = {
                        params: [{
                            userID: userData && userData.patID ? userData.patID : "",
                            cureAppID: item.ID
                        }],
                    }
                    $http.post("urlS", this, {
                        code: "01040014",
                        data: data,
                        success: function (res) {
                            if (+res.errorCode === 0) {
                                this.handleGetItem()
                            } else {
                                Toast.info(res.errorMessage)
                            }
                        }
                    });
                }
            },
        ])

    }
    onRef = (ref) => {
        this.childPay = ref
    }
    //点击付款
    handleToPay = (price, payNote) => {
        this.childPay.showModal(price, payNote)
    }
    //支付成功
    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.applyTime}
                                </span>
                            </div>
                            <div className="status-dom">
                                <span>{item.reportFlag === "Y" ? "已支付" : "未支付"} /</span>
                                <span>{item.cureStatus || "--"}</span>
                            </div>
                        </div>
                    </ListItem>
                    <div className="project-lists treatmentOrder-center" >
                        <p className="small">
                            <span>{item.descripts}</span>
                            <span> <span >{item.totalPrice}元</span></span>
                        </p>
                        <p className="small">
                            <span>{item.locDesc || "--"}</span>
                            <span>{item.patPrice}元</span>
                        </p>
                        <p className="number">
                            <span>x{item.count}</span>
                        </p>
                    </div>
                    <div className="treatmentOrder-bottom">
                        <div>
                            <Button
                                className="applyEdit"
                                onClick={() => this.handleCheckApply(item)}>查看申请</Button>
                            {/* <Button 
                            style={{display:(item.reportFlag == "N"&&item.statusCode == "3"?"block":"none")}} 
                            className="applyEdit" 
                            onClick={() => this.handleCheckApply(item, "1")}>修改申请</Button> */}
                            <Button
                                style={{ display: (item.reportFlag === "N" && item.statusCode === "3" ? "block" : "none") }}
                                className="applyEdit" onClick={() => this.handleCancel(item)}>取消</Button>
                            <Button
                                style={{ display: (item.reportFlag === "N" ? "block" : "none") }}
                                onClick={() => this.handleToPay(item.totalPrice, item.payNote)}
                                className="patOrder">付款</Button>
                            <Button style={{ display: (item.appointment === "T" ? "block" : "none") }} className="treatment" onClick={() => this.handleAppointment(item)}>预约治疗</Button>
                            <Button style={{ display: (item.appointmentRecord === "T" ? "block" : "none") }} className="patOrder" onClick={() => this.handleAppiontRecord(item)}>预约记录</Button>
                            <Button style={{ display: (item.cureRecord === "T" ? "block" : "none") }} className="treatOrder" onClick={() => this.handleTreatmentRecord(item)}>治疗记录</Button>
                        </div>
                    </div>
                </ListItem>
            )
        })
        return (
            <div className="treatmentOrder-dom patientCenter-dom">
                <List>
                    {
                        projectArr && projectArr.length > 0 ? ListItemOp : <div style={{ textAlign: "center", height: "48px", lineHeight: "48px" }}>
                            暂无治疗订单,点击<span style={{ color: "#0079fe", margin: "0 2px" }} onClick={() => window.history.go(-1)}>回到首页</span>
                        </div>
                    }
                </List>
                <PayTips onRef={this.onRef} payToNext={this.payToNext}></PayTips>
            </div>
        )
    }
};
export default TreatmentOrderList;