<template>
    <!-- pages/entryWrite/page/Entry/prescription/addExamine/completeExamine/completeExamine.wxml -->
    <view class="complete-content">
        <view class="complete-wrap">
            <view v-if="listArr.length <= 0" class="nodata-show">
                <view style="text-align: center">
                    <text>当前未选择项目</text>
                    <view class="btn-reset" @tap="resetSelect">重新选择</view>
                    <view class="btn-back" @tap="resetBack">返回医嘱列表</view>
                </view>
            </view>
            <view class="item-content" v-for="(item, aIndex) in listArr" :key="aIndex">
                <view class="item-title">
                    <!-- <text>{{item.ordDesc}}</text> -->
                    <text class="title-left">{{ item.ordDesc }}</text>
                    <text v-if="handle === 'add'" class="title-right" @tap="onDeleteItem" :data-index="aIndex">删除</text>
                </view>

                <view>
                    <picker @change="bindSpecChange" :data-aindex="aIndex" :value="item.specIndex" :range="item.specList" range-key="descripts">
                        <mp-cell link hover value="标本" :footer="item.specList[item.specIndex].descripts"></mp-cell>
                    </picker>

                    <view class="picker-border">
                        <picker @change="bindAdmReasonChange" :data-aindex="aIndex" :range="item.admReasonList" :value="item.admReasonIndex" range-key="descripts">
                            <mp-cell link hover value="*结算类型" :footer="item.admReasonList[item.admReasonIndex].descripts"></mp-cell>
                        </picker>
                    </view>
                    <view class="picker-border">
                        <picker @change="bindrecLocChange" :data-aindex="aIndex" :range="item.recLocList" :value="item.recLocIndex" range-key="recLocDesc">
                            <mp-cell link hover value="*执行科室" :footer="item.recLocList[item.recLocIndex].recLocDesc"></mp-cell>
                        </picker>
                    </view>
                </view>
            </view>
        </view>
        <view class="save-btn" @tap="saveList">保存</view>
    </view>
</template>

<script>
import Cache from '../../../../../../../utils/cache';
// pages/entryWrite/page/Entry/prescription/addExamine/completeExamine/completeExamine.js
import { $http } from '../../../../../../../config/https';
export default {
    components: {},
    data() {
        return {
            sequenceList: [],
            listArr: [],

            //组合的所有数据
            handle: 'add',

            //默认状态为添加 用来控制返回
            allListArr: [],

            //用来编辑保存
            allListArrIndex: 0,

            //当前编辑的index
            tempArr: [],

            //用来存储未保存的数据
            motto: null,

            defaultShow: true,
            aIndex: 0
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.handle === 'add') {
            let tempArr = JSON.parse(options.yList);
            this.setData({
                tempArr
            });

            // 新增
            let orders = JSON.parse(options.medIds);
            this.getDoctorQuery(orders).then(async () => {
                console.log(this.sequenceList);
                let newArr = this.sequenceList.map((data, index) => {
                    return {
                        ordSeqNo: index,
                        medID: data.defaultData.medID,
                        ordDesc: data.defaultData.ordDesc,
                        specList: data.dataList.specList,
                        //标本
                        specIndex: 0,
                        //标本下拉index
                        admReasonList: data.dataList.admReasonList,
                        //结算类型
                        admReasonIndex: 0,
                        //0是自费1是普通医保
                        recLocList: data.dataList.recLocList,
                        //执行科室
                        recLocIndex: 0,
                        //执行科室的下拉index
                        ordersObJ: data.defaultData //保存医嘱需要
                    };
                });

                this.setData({
                    listArr: newArr,
                    handle: 'add'
                });
                console.log('newArr==', newArr);
            });
        } else {
            let listArr = JSON.parse(options.listArr);
            console.log('listArr===', listArr);
            this.setData(
                {
                    allListArr: listArr,
                    allListArrIndex: options.index
                },
                () => {
                    this.setData({
                        listArr: [listArr[options.index]],
                        handle: 'edit'
                    });
                }
            );
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        // 1.获取医嘱项信息
        getDoctorQuery: function (orders) {
            return new Promise((resolve, reject) => {
                let patientData = Cache.get('patientData');
                let { admID, admNo } = patientData;
                let data = {
                    params: [
                        {
                            admID: admID,
                            orders: orders
                        }
                    ]
                };
                $http.post('urlDeault', this, {
                    code: '03050020',
                    data: data,
                    success: function (res) {
                        if (res.errorCode == '0') {
                            this.setData(
                                {
                                    sequenceList: res.data
                                },
                                () => {
                                    resolve();
                                }
                            );
                        } else {
                            reject();
                        }
                    },
                    fail: function (err) {
                        reject();
                    }
                });
            });
        },

        bindSpecChange: function (e) {
            let { aindex } = e.currentTarget.dataset;
            this.listArr[aindex].specIndex = e.detail.value;
            this.setData({
                listArr: this.listArr
            });
        },

        saveList: function () {
            let patientData = Cache.get('patientData');
            let userData = Cache.get('userData');
            let { userID, locID, hospID, userName } = userData;
            let { admID, admNo } = patientData;
            let paramsArr = [];
            if (this.handle === 'add') {
                paramsArr = this.tempArr.concat(this.listArr);
            } else {
                //编辑对应的检验
                console.log(this.allListArr, this.allListArrIndex, this.listArr);
                this.allListArr[this.allListArrIndex] = this.listArr[0];
                paramsArr = this.allListArr;
            }
            let data = {
                params: [
                    {
                        medType: 'JY',
                        admID: admID || '',
                        hospID: hospID,
                        userID: userID,
                        locID: locID || '',
                        //开单科室
                        docID: userID,
                        paramsArr: paramsArr
                    }
                ]
            };
            console.log('data=', data);
            $http.post('urlDeault', this, {
                code: '03050176',
                data: data,
                success: function (res) {
                    if (res.errorCode == '0') {
                        uni.navigateBack({
                            delta: this.handle === 'add' ? 2 : 1
                        });
                    }
                },
                fail: function (err) {}
            });
        },

        // 结算类型
        bindAdmReasonChange: function (e) {
            console.log('e==', e);
            let { aindex } = e.currentTarget.dataset;
            this.listArr[aindex].admReasonIndex = e.detail.value;
            this.setData({
                listArr: this.listArr
            });
        },

        // 执行科室
        bindrecLocChange: function (e) {
            console.log('e==', e);
            let { aindex } = e.currentTarget.dataset;
            this.listArr[aindex].recLocIndex = e.detail.value;
            this.setData({
                listArr: this.listArr
            });
        },

        // 删除列表项
        onDeleteItem: function (e) {
            let { index } = e.currentTarget.dataset;
            let { listArr } = this;
            listArr.splice(index, 1);
            this.setData({
                listArr,
                defaultShow: false
            });
        },

        resetSelect: function (e) {
            uni.navigateBack({
                delta: 1
            });
        },

        resetBack: function (e) {
            uni.navigateBack({
                delta: 2
            });
        }
    }
};
</script>
<style>
/* pages/entryWrite/page/Entry/prescription/addExamine/completeExamine/completeExamine.wxss */
page {
    height: 100%;
    width: 100%;
}
.complete-content {
    /* height: 100%;
  width: 100%;
  background-color: #FFFFFF;
  overflow: auto; */
    display: flex;
    flex-direction: column;
    height: 100%;
}
.complete-wrap {
    height: 100%;
    width: 100%;
    background-color: #ffffff;
    overflow: auto;
}

.item-title {
    display: flex;
    flex-direction: row;
    padding-left: 10rpx;
    padding: 20rpx 20rpx;
    font-size: 32rpx;
    font-weight: bold;
    background-color: #f5f4f4;
    border-bottom: 1rpx solid #f5f4f4;
}

.picker-border {
    border-bottom: 1rpx solid var(--weui-FG-3);
}
.btn-flex {
    width: 250rpx;
    display: flex;
    flex-direction: row;
    justify-content: right;
    margin-left: auto;
}
.selectT {
    background-color: #ffffff;
    width: 200rpx;
    height: 60rpx;
    line-height: 60rpx;
    border: 1rpx solid #f5f4f4;
    text-align: center;
    border-radius: 20rpx;
}
.selectL {
    padding: 0px 5rpx;
    margin-right: 10rpx;
}
.part-flex {
    display: flex;
    flex-direction: column;
    height: 100%;
}
.part-a {
    flex: 1;
    padding: 20rpx;
}
.part-b {
    display: flex;
    flex-direction: row;
}
.part-bbtn {
    flex: 1;
    text-align: center;
}
.part-aTitle {
    font-size: 32rpx;
    color: black;
}
.part-child {
    display: flex;
}
.part-aChild {
    flex: 0 0 33.333%;
}
.part-bChild {
    flex: 0 0 25%;
}

.part-margin {
    border: 1rpx solid #f5f4f4;
    text-align: center;
    margin: 10rpx;
}

.child-active {
    color: #1890ff;
    border: 1rpx solid #1890ff;
}

.save-btn {
    height: 100rpx;
    line-height: 100rpx;
    color: #ffffff;
    background-color: #1890ff;
    text-align: center;
    font-size: 32rpx;
}

.title-left {
    flex: 1;
}

.title-right {
    color: #fa541c;
}

.nodata-show {
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
.btn-reset {
    color: #ffffff;
    background-color: #1890ff;
    padding: 20rpx;
    width: 200rpx;
    border-radius: 60rpx;
    margin-top: 80rpx;
}
.btn-back {
    background-color: #ffffff;
    padding: 20rpx;
    width: 200rpx;
    border: 1rpx solid #f5f4f4;
    border-radius: 60rpx;
    margin-top: 30rpx;
}
</style>
