<template>
    <view>
        <nav-bar :navbarData="navbarData"></nav-bar>
        <view class="page-replyTemplateHand" :style="'margin-top: ' + (height * 2 + 10) + 'rpx; height: calc(100vh - ' + (height * 2 + 10) + 'rpx)'">
            <view class="searchList">
                <view class="section">
                    <textarea class="textarea" placeholder="请输入内容" auto-focus focus maxlength="10000" :value="content" @input="checkChange" />
                </view>
            </view>
            <view class="multimeBtn-contenttemp">
                <block v-if="type == 'add'">
                    <button class="multimeBtn-rightSave" @tap="add" :disabled="saveflag">{{ saveflag && saveflag ? '保存中' : '保存' }}</button>
                </block>
                <block v-if="type == 'edit'">
                    <button class="multimeBtn-leftdel" @tap="tapRemove" :disabled="deleteflag">{{ deleteflag ? '删除中...' : '删除' }}</button>
                    <button class="multimeBtn-rightSave" @tap="add" :disabled="saveflag">{{ saveflag && saveflag ? '修改中' : '保存' }}</button>
                </block>
            </view>
        </view>
    </view>
</template>

<script>
import Cache from '../../../../utils/cache';
import navBar from '@/pages/components/navbar/index';
import { $http } from '../../../../config/https';
const app = getApp();
export default {
    components: {
        navBar
    },
    data() {
        return {
            content: '',
            id: '',
            idx: '',

            navbarData: {
                isGoBack: true,
                background: '',
                title: '添加模板',
                //导航栏 中间的标题
                bottom: false
            },

            height: app.globalData.height,
            saveflag: false,
            deleteflag: false,
            type: '',
            select: 0
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let { value, id, idx } = options;
        this.setData(
            {
                content: value,
                id: id,
                idx: idx,
                type: options.type
            },
            () => {
                if (value && value !== '') {
                    let { navbarData } = this;
                    navbarData.title = '编辑模板';
                    this.setData({
                        navbarData
                    });
                }
            }
        );
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {},
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {},
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {},
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {},
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {},
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {},
    methods: {
        //输入文本框的值
        checkChange: function (e) {
            this.setData({
                content: e.detail.value
            });
        },

        // 点击删除
        tapRemove: function (event) {
            let { idx } = this;
            let that = this;
            console.log('idx', idx);
            uni.showModal({
                title: '提示',
                content: '是否确认删除',
                success: function (res) {
                    // console.log('success', res);
                    if (res.confirm) {
                        that.deleteDiag(idx);
                    } else if (res.cancel) {
                    }
                },
                fail: function (res) {
                    // console.log('fail', res);
                }
            });
        },

        //删除
        deleteDiag: function (index) {
            let that = this;
            if (that.deleteflag == true) {
                throw SyntaxError();
            }
            that.setData({
                deleteflag: true
            }); //请求前禁用标记改为true,防止重复请求
            let userData = Cache.get('userData');
            let data = {
                params: [
                    {
                        ID: [this.id],
                        userID: userData.userID //就诊ID session
                    }
                ]
            };

            $http.post('urlDeault', this, {
                code: '03100026',
                data: data,
                success: function (res) {
                    let errorCode = res.errorCode;
                    if (errorCode === '0') {
                        // this.listData() // 获取 下拉框数据
                        // console.log('deleteDiag', res);
                        uni.navigateBack({});
                    }
                    setTimeout(() => {
                        that.setData({
                            deleteflag: false
                        }); //请求成功禁用标记改回false
                    }, 1000);
                },
                fail: function () {
                    setTimeout(() => {
                        that.setData({
                            deleteflag: false
                        }); //请求虽然失败,禁用标记也改回false
                    }, 1000);
                }
            });
        },

        // add 保存
        add: function () {
            let that = this;
            // console.log(that.data.saveflag+"aaaa");
            if (that.saveflag == true) {
                throw SyntaxError();
            }
            // console.log('bbb');
            that.setData({
                saveflag: true
            }); //请求前禁用标记改为true,防止重复请求
            let userData = Cache.get('userData');
            let { id } = this;
            let { userID, locID, hospID } = userData;
            let { content } = this;
            let params = {
                params: [
                    {
                        userID: userID || '',
                        // 用户ID session 中获取
                        locID: locID || '',
                        // 科室ID session 中获取
                        hospID: hospID || '',
                        templet: content,
                        ID: id ? id : '' //编辑
                    }
                ]
            };

            $http.post('urlDeault', this, {
                code: '03100025',
                data: params,
                success: function (res) {
                    let errorCode = res.errorCode;
                    if (errorCode == '0') {
                        // console.log('res',res)
                        let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
                        let prevPage = pages[pages.length - 2];
                        prevPage.setData(
                            {
                                // 将我们想要传递的参数在这里直接setData。上个页面就会执行这里的操作。
                                select: 0
                            },
                            () => {
                                uni.navigateBack({});
                            }
                        );
                    }
                    setTimeout(() => {
                        that.setData({
                            saveflag: false
                        }); //请求成功禁用标记改回false
                    }, 1000);
                },
                fail: function () {
                    setTimeout(() => {
                        that.setData({
                            saveflag: false
                        }); //请求虽然失败,禁用标记也改回false
                    }, 1000);
                }
            });
        }
    }
};
</script>
<style>
.page-replyTemplateHand {
    display: flex;
    flex-direction: column;
    flex-wrap: nowrap;
    justify-content: space-between;
    align-items: center;
}

.searchSave {
    flex: 0 0 100rpx;
    width: 100%;
    /* border:1px solid red; */
}

.searchSave-btn {
    background: #fff;
    padding: 10rpx;
    width: 100%;
    font-size: 30rpx;
    position: fixed;
    bottom: 0;
}

.searchList {
    box-sizing: border-box;
    flex: 1 1 1%;
    width: 100%;
    /* border:1px  solid green; */
    background: #eee;
    padding: 50rpx 30rpx;
    /* padding-bottom: 120rpx; */
}

.searchList .textarea {
    width: 100%;
    min-height: 1000rpx;
}

.multimeBtn-contenttemp {
    height: 100rpx;
    font-size: 30rpx;
    color: #fff;
    padding: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    background-color: #1890ff;
    padding-bottom: env(safe-area-inset-bottom);
}

.multimeBtn-contenttemp:active {
    background-color: #127adb;
}

.multimeBtn-leftdel {
    flex: 1;
    align-items: center;
    justify-content: center;
    display: flex;
    border-radius: 0 !important;
}

.multimeBtn-rightSave {
    flex: 1;
    align-items: center;
    justify-content: center;
    display: flex;
    background-color: #1890ff;
    color: #ffffff;
    border-radius: 0 !important;
    border: 0;
    box-shadow: 0 0 0 transparent;
}

.multimeBtn-rightSave:active {
    background-color: #127adb !important;
}

.multimeBtn-rightSave::after,
.multimeBtn-rightSave::before {
    box-shadow: 0 0 0 transparent;
    border: 0 !important;
}
</style>
