<template>
	<view>
		<cu-custom bgColor="bg-gradual-blueness" :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">工作安排</block>
		</cu-custom>
		<view>
			<!-- 插入模式 -->
			<uni-calendar class="uni-calendar--hook" :selected="info.selected" :showMonth="false" @change="change" @monthSwitch="monthSwitch" />
		</view>
		<scroll-view scroll-y>
			<view class="cu-bar bg-white solid-bottom margin-top-sm">
				<view class="action">
					本日工作提醒
				</view>
			</view>
			<view class="padding-sm bg-white">
				<view class="cale-item">
					<view class="">
						<text class="cuIcon-title text-blue mr10"></text>
						<text class="text-grey">需进行{{yjcNum}}项隐患排查</text>
					</view>
					<view class="text-green">
						已完成
					</view>
				</view>
				<view class="cale-item">
					<view class="">
						<text class="cuIcon-title text-blue mr10"></text>
						<text class="text-grey">需进行{{wjcNum}}项隐患排查</text>
					</view>
					<view class="text-red">
						未完成
					</view>
				</view>
			</view>
			<view class="cu-bar bg-white solid-bottom margin-top-sm">
				<view class="action">
					本日日程安排
				</view>
			</view>
			<view class="padding-sm bg-white">
				<view class="cale-item">
					<view class="">
						<text class="cuIcon-title text-blue mr10"></text>
						<text class="text-grey" v-if="todayschedule!=null">{{todayschedule.desc}}</text>
						<text class="text-grey" v-if="todayschedule==null">暂无数据</text>
					</view>
				</view>
			</view>
			<view class="margin-top-sm"></view>
		</scroll-view>

    <uni-popup :show="showDailog" type="center" :custom="true" :mask-click="false">
      <view class="uni-tip">
        <view class="uni-tip-title">标题:</view>
        <view class="uni-tip-content">
          <input v-model="schedule.title"  placeholder="请输入"></input>
        </view>

        <view class="uni-tip-title">内容:</view>
        <view class="uni-tip-content">
          <textarea class="uni-tip-content-textarea" focus="true" placeholder="请输入" maxlength="-1" v-model="schedule.desc" />
        </view>
        <!-- 按钮部分 -->
        <view class="uni-tip-group-button">
          <button :loading="buttonloading" type="primary" @click="confirm">确定</button>
          <button type="warn" @click="cancel">取消</button>
        </view>
      </view>
    </uni-popup>
	</view>
</template>

<script>
	import {
		basePath,corpinfoId,loginSession,loginUser
	} from '@/common/tool.js';
  import uniPopup from '@/components/dialog/uni-dialog.vue'
	export default {
    components: {
      uniPopup
    },
		data() {
			return {
				info: {
					lunar: true,
					range: true,
					insert: false,
					selected: [],


				},
				buttonloading: false,
				rcap:'',
				CDATA:'',
				username:'',
				schedule:[],
        		todayschedule: [],
				wjcNum:0,
				yjcNum:0,

        showDailog: false, // 是否显示弹窗
        msg : 'add',
			}
		},
		onLoad(e){
			this.getData();
      		this.getTodayData();
			this.getbrgzData();
			this.username=loginUser.USERNAME;
		},
		methods: {
			async change(e) {
				console.log('change 返回:', e)
				// 模拟动态打卡
				// if (this.info.selected.length > 5) return
				// this.info.selected.push({
				// 	date: e.fulldate,
				// 	info: '打卡'
				// })
				this.CDATA= e.fulldate;
				await this.getData();
        this.showDailog = true
			},
      getData() {
        return new Promise((resolve, reject) => {
          var _this = this;
          this.schedule= {}
          uni.showLoading({
            title: '请稍候'
          })
          uni.request({
            url: basePath + '/app/schedule/getData',
            method: 'POST',
            dataType: 'json',
            header:{
                'Content-type':'application/x-www-form-urlencoded'
            },
            data: {
              CDATA: this.CDATA,
              USERNAME:loginUser.USERNAME,
              CORPINFO_ID:loginUser.CORPINFO_ID,
              USER_ID:loginUser.USER_ID,
            },
            success: (res) => {
              if(res.data.result=="success"){
                if(res.data.varList.length>0){
                  _this.schedule=res.data.varList[0] || {};
                  this.msg = 'edit'
                }else{
                    _this.schedule= {"date":"","id":Math.random(),"title":"","desc":""}
                  _this.msg = 'add'
                }
                uni.hideLoading();
                resolve()
              }else {
                reject()
                uni.showToast({
                  title: "获取数据失败",
                  duration: 2000
                });
              }
            }
          });
        });
			},
      getTodayData(){
        var _this = this;
        this.todayschedule= {}
        uni.showLoading({
          title: '请稍候'
        })
        uni.request({
          url: basePath + '/app/schedule/getData',
          method: 'POST',
          dataType: 'json',
          header:{
            'Content-type':'application/x-www-form-urlencoded'
          },
          data: {
            CDATA: this.CDATA,
            USERNAME:loginUser.USERNAME,
            CORPINFO_ID:loginUser.CORPINFO_ID,
            USER_ID:loginUser.USER_ID,
          },
          success: (res) => {
            if(res.data.result=="success"){
              if(res.data.varList.length>0){
                _this.todayschedule=res.data.varList[0] || {};
                this.msg = 'edit'
              }else{
                _this.todayschedule= {}
                _this.msg = 'add'
                _this.todayschedule.id = Math.random()
              }
              uni.hideLoading();
              resolve()
            }else {
              reject()
              uni.showToast({
                title: "获取数据失败",
                duration: 2000
              });
            }
          }
        });
      },
      cancel() {
        this.showDailog = false
      },
      getbrgzData() {
				var _this = this;
				uni.showLoading({
					title: '请稍候'
				})
				uni.request({
					url: basePath + '/app/listmanager/checkListIndex',
					method: 'POST',
					dataType: 'json',
					header:{
							'Content-type':'application/x-www-form-urlencoded'
					},
					data: {
						tm:new Date().getTime(),
						DEPARTMENT_ID:loginUser.DEPARTMENT_ID,
						CORPINFO_ID:loginUser.CORPINFO_ID,
						USER_ID:loginUser.USER_ID,
					},
					success: (res) => {
						console.log('confirm 返回:', res)
						if(res.data.result=="success"){
							_this.wjcNum=res.data.wjcNum;
							_this.yjcNum=res.data.yjcNum;
							uni.hideLoading();
						}else {
							uni.showToast({
								title: "获取数据失败",
								duration: 2000
							});
						}
					}
				});
			},
			confirm(e) {
			  var _this = this
				this.buttonloading =true
        uni.showLoading({
          title: '请稍候'
        })
        uni.request({
          url: basePath + '/app/schedule/'+this.msg,
          method: 'POST',
          dataType: 'json',
          header:{
            'Content-type':'application/x-www-form-urlencoded'
          },
          data: {
            CDATA: this.CDATA,
            USERNAME:loginUser.USERNAME,
            CORPINFO_ID:loginUser.CORPINFO_ID,
            USER_ID:loginUser.USER_ID,
            SCHEDULE_ID: this.schedule.id,
            TITLE: this.schedule.title || '',
            FHDESC: this.schedule.desc  || '',
          },
          success: (res) => {
            if(res.data.result=="success"){
              uni.showToast({
                icon:'none',
                title: '保存成功',
                duration: 2000
              });
              this.buttonloading = false
              _this.showDailog = false
              _this.getData()
            }else {
              _this.showDailog = false
              uni.showToast({
                title: "获取数据失败",
                duration: 2000
              });
            }
          }
        });
			},
			monthSwitch(e) {
				console.log('monthSwitchs 返回:', e)
			},
			//跳转事件
			goToEdit(e) {
				uni.navigateTo({
					url: '/pages/application/basic-info-manage/basic-information/basic-information-edit'
				});
			},
		}
	}
</script>

<style>
.cu-bar{
	min-height: 86upx;
}
.cale-item{
	display: flex;
	align-items: center;
	justify-content: space-between;
	text-align: left;
	line-height: 2;
}
.cale-item .zt{

}

.uni-tip {
  padding: 15px;
  width: 300px;
  background: #fff;
  box-sizing: border-box;
  border-radius: 10px;
}

.uni-tip-title {
  text-align: center;
  font-weight: bold;
  font-size: 16px;
  color: #333;
  text-align: left;
}

.uni-tip-content {
  padding: 15px;
  font-size: 14px;
  color: #666;
  /* background: #C8C7CC; */
  border:2upx solid #ccc;
  border-radius: 10upx;
}

.uni-tip-content-textarea{
  height: 300upx;
  width: 100%;
  text-align: left;
}
.uni-tip-group-button {
  margin-top: 10px;
  display: flex;
}
.uni-tip-group-button>button{
  font-size:24upx;
  height: 40upx;
  line-height: 40upx;

}
</style>