<template>
    <view>
        <!--/*
***HotApp云笔记，基于HotApp小程序统计云后台
***免费云后台申请地址 https://weixin.hotapp.cn/cloud
***API 文档地址：https://weixin.hotapp.cn/api
***小程序技术讨论QQ群：173063969
*/-->
        <!-- index.wxml -->
        <form @submit="onSubmit" @reset="">
            <view class="container">
                <view class="title"><input name="title" placeholder-class="placeholder" placeholder="在此输入标题（可选）" :value="item.value.title" /></view>
                <view class="row">
                    <textarea class="text" placeholder-class="placeholder" name="content" :focus="focus" :value="item.value.content" placeholder="点击添加文本" />
                </view>
                <view class="bottom">
                    <button formType="submit" class="btn success">保存</button>
                </view>
            </view>
        </form>
    </view>
</template>

<script>
/*
 ***HotApp云笔记，基于HotApp小程序统计云后台
 ***免费云后台申请地址 https://weixin.hotapp.cn/cloud
 ***API 文档地址：https://weixin.hotapp.cn/api
 ***小程序技术讨论QQ群：173063969
 */
var app = getApp();
export default {
    data() {
        return {
            item: {
                key: '',
                value: {
                    title: '',
                    content: ''
                },
                create_time: '',
                update_time: '',
                state: 1
            },
            isNew: false,
            focus: true
        };
    }
    /**
     * 页面渲染事件
     */,
    onShow: function () {
        var item = this.item;
        item.key = app.globalData.hotapp.genPrimaryKey('item');
        this.setData({
            item: item
        });
    },
    methods: {
        /**
         * 保存数据事件
         */
        onSubmit: function (event) {
            console.log(event);
            var item = this.item;
            item.value.title = event.detail.value.title;
            item.value.content = event.detail.value.content;
            this.setData({
                item: item
            });
            this.saveData();
        },

        /**
         * 请求服务器保存数据
         */
        saveData: function () {
            var item = this.item;
            var now = Date.parse(new Date()) / 1000;
            item.update_time = now;
            item.create_time = now;
            this.setData({
                item: item
            });
            app.globalData.store(this.item, function (res) {
                if (res) {
                    uni.showToast({
                        title: '保存成功'
                    });
                    //返回首页
                    uni.navigateBack();
                } else {
                    uni.showToast({
                        title: '保存失败'
                    });
                }
            });
        }
    }
};
</script>
<style>
@import './index.css';
</style>
