<template>
  <view class="page">
    <view v-if="ready">
      <l-select
        @input="setValue('StuLeaveManagement.CheckStatus', $event)"
        :value="getValue('StuLeaveManagement.CheckStatus')"
        :range="dataSource.StuLeaveManagement.CheckStatus"
        title="审核状态"
		:disabled="!edit"
		v-if="!edit"
      />
      <l-textarea
        @input="setValue('StuLeaveManagement.CheckRemark', $event)"
        :value="getValue('StuLeaveManagement.CheckRemark')"
        title="审核备注"
		v-if="!edit"
		:readonly="!edit"
      />
	  <l-select
	    @input="setValue('StuLeaveManagement.CheckUserNo', $event)"
	    :value="getValue('StuLeaveManagement.CheckUserNo')"
		:range="dataSource.StuLeaveManagement.CheckUserNo"
	    title="审核人"
		v-if="!edit"
		:disabled="!edit"
	  />
	  <l-select
	    @input="setValue('StuLeaveManagement.LeaveType', $event)"
	    :value="getValue('StuLeaveManagement.LeaveType')"
	    :range="dataSource.StuLeaveManagement.LeaveType"
	    title="请假类型"
		required
		:disabled="!edit"
	  />
	  <l-date-picker
	    @input="setValue('StuLeaveManagement.StartTime', $event)"
	    :value="getValue('StuLeaveManagement.StartTime')"
	    :disabled="!edit"
		required
	    title="开始时间"
	  /> 
	  <l-date-picker
	    @input="setValue('StuLeaveManagement.EndTime', $event)"
	    :value="getValue('StuLeaveManagement.EndTime')"
	    :disabled="!edit"
		required
	    title="结束时间"
	  /> 
     <!-- <l-datetime-picker
        @input="setValue('StuLeaveManagement.StartTime', $event)"
        :value="getValue('StuLeaveManagement.StartTime')"
        title="开始时间"
		required
		:disabled="!edit"
      />
      <l-datetime-picker
        @input="setValue('StuLeaveManagement.EndTime', $event)"
        :value="getValue('StuLeaveManagement.EndTime')"
        title="结束时间"
		required
		:disabled="!edit"
      /> -->
	  <l-input
	    @input="setValue('StuLeaveManagement.LeaveDay', $event)"
	    :value="getValue( String('StuLeaveManagement.LeaveDay'))"
		type="number"
	    title="请假天数"
		:disabled="!edit"
	  />
	  <l-textarea
	    @input="setValue('StuLeaveManagement.LeaveReason', $event)"
	    :value="getValue('StuLeaveManagement.LeaveReason')"
	    title="请假事由"
		:readonly="!edit"
	  />
	  <l-input
	    @input="setValue('StuLeaveManagement.CreateUserNo', $event)"
	    :value="getValue('StuLeaveManagement.CreateUserNo')"
	    :readonly="!edit"
	    title="学号"
		disabled
		v-if="!edit"
	  />
	  <l-input
	    @input="setValue('StuLeaveManagement.CreateUserName', $event)"
	    :value="getValue('StuLeaveManagement.CreateUserName')"
	    title="姓名"
		disabled
		v-if="!edit"
	  />
	  <l-input
	    @input="setValue('StuLeaveManagement.CreateTime', $event)"
	    :value="getValue('StuLeaveManagement.CreateTime')"
	    title="申请时间"
		disabled
		v-if="!edit"
	  />
	  <l-select
	    @input="setValue('StuLeaveManagement.ClassNo', $event)"
	    :value="getValue('StuLeaveManagement.ClassNo')"
		:range="dataSource.StuLeaveManagement.ClassNo"
	    title="班级"
		disabled
		v-if="!edit"
	  />
	  <l-select
	    @input="setValue('StuLeaveManagement.DeptNo', $event)"
	    :value="getValue('StuLeaveManagement.DeptNo')"
		:range="dataSource.StuLeaveManagement.DeptNo"
	    title="系部"
		disabled
		v-if="!edit"
	  />
	  <l-select
	    @input="setValue('StuLeaveManagement.MajorNo', $event)"
	    :value="getValue('StuLeaveManagement.MajorNo')"
		:range="dataSource.StuLeaveManagement.MajorNo"
	    title="专业"
		disabled
		v-if="!edit"
	  />
	  <l-select
	    @input="setValue('StuLeaveManagement.ClassDiredctorNo', $event)"
	    :value="getValue('StuLeaveManagement.ClassDiredctorNo')"
		:range="dataSource.StuLeaveManagement.ClassDiredctorNo"
	    title="班主任"
		disabled
		v-if="!edit"
	  />
	  <l-select
	    @input="setValue('StuLeaveManagement.ClassTutorNo', $event)"
	    :value="getValue('StuLeaveManagement.ClassTutorNo')"
		:range="dataSource.StuLeaveManagement.ClassTutorNo"
	    title="辅导员"
		disabled
		v-if="!edit"
	  />
    </view>

    <view v-if="ready&&(origin.StuLeaveManagement.CheckStatus=='0'||edit)" class="bg-white margin-tb padding" style="padding-top: 0; overflow: hidden;">
      <l-button v-if="edit" @click="action('save')" size="lg" color="green" class="block margin-top" block>
        保存草稿
      </l-button>
      <l-button v-if="!edit && mode !== 'create'" @click="action('edit')" size="lg" line="orange" class="block margin-top" block>
        编辑本页
      </l-button>
      <l-button v-if="edit && mode !== 'create'" @click="action('reset')" size="lg" line="red" class="block margin-top" block>
        取消编辑
      </l-button>
      <l-button v-if="!edit && mode !== 'create'" @click="action('delete')" size="lg" line="red" class="block margin-top" block>
        删除
      </l-button>
    </view>
  </view>
</template>


<script>
/*
 * 版 本 Learun-ADMS V7.0.3 力软敏捷开发框架(http://www.learun.cn)
 * Copyright (c) 2013-2021 上海力软信息技术有限公司
 * 创建人：超级管理员
 * 日  期：2021-02-21 10:07
 * 描  述：会议管理
 */

/**
 * 本段代码由移动端代码生成器输出，移动端须 2.2.0 版本及以上可以使用
 * 请在移动端 /pages.json 中的 pages 字段中添加一条记录：
 * { "path": "pages/PersonnelManagement/StuLeaveManagement/single", "style": { "navigationBarTitleText": "表单详情页" } }
 * 
 * （navigationBarTitleText 字段为本页面的标题文本，可以修改）
 * （必须自行操作该步骤，力软代码生成器不会自动帮您修改 /pages.json 文件）
 */

import get from 'lodash/get'
import set from 'lodash/set'
import moment from 'moment'
import customPageMixins from '@/common/custompage.js'

export default {
  mixins: [customPageMixins],

  data() {
    return {
      // 页面相关参数
      id: null,
      mode: null,
      edit: null,
      ready: false,

      // 表单数据
      current: {},
      origin: {},

      // 表单项数据结构
      scheme: {
        StuLeaveManagement: {
          CheckStatus: { type: 'select', title: '审核状态', dataSource: '0' },
          CheckRemark: { type: 'texteditor', title: '审核备注', },
		  CreateUserNo: { type: 'text', title: '学号' },
		  LeaveType: { type: 'select', title: '请假类型', dataSource: '0',verify:"NotNull" },
		   StartTime: { type: 'datetime', title: '开始时间',verify:"NotNull", dateformat: '0' }, 
		   EndTime:{type: 'datetime', title: '结束时间', verify:"NotNull",dateformat: '0'},
          // StartTime: { type: 'datetime', title: '开始时间', dateformat: '1',verify:"NotNull"  },
          // EndTime: { type: 'datetime', title: '结束时间',dateformat: '1', verify:"NotNull"  },
		  
		  LeaveDay: { type: 'number', title: '请假天数',verify:"NumOrNull" },
		  LeaveReason: { type: 'texteditor', title: '请假事由' },
		  CheckUserNo: { type: 'select', title: '审核人',dataSource: '1' },
		  CreateUserName: { type: 'text', title: '姓名' },
		  CreateTime: { type: 'text', title: '申请时间' },
		  ClassNo: {  type: 'select', title: '班级', dataSource: '1', dataSourceId: 'bjsj,classname,classno' },
		  DeptNo: { type: 'select', title: '系部', dataSource: '1', },
		  MajorNo: { type: 'select', title: '专业', dataSource: '1', },
		  ClassDiredctorNo: { type: 'select', title: '班主任',dataSource: '1', },
		  ClassTutorNo: { type: 'select', title: '辅导员',dataSource: '1', },
		  
        },

      },

      // 数据源
      dataSource: {
		  StuLeaveManagement:{
			  ClassNo:[],
			  DeptNo:[],
			  MajorNo:[],
			  ClassTutorNo:[],
			  ClassDiredctorNo:[],
			  CheckUserNo:[],
			  LeaveType: Object.values(this.GET_GLOBAL('dataDictionary').LeaveType).map(t => ({ value: t.value, text: t.text })),
			  CheckStatus: Object.values(this.GET_GLOBAL('dataDictionary').LeaveCheck).map(t => ({ value: t.value, text: t.text }))
		  }
      }
    }
  },

  async onLoad({ type, id }) {
    await this.init(type, id)
  },

  methods: {
    // 页面初始化
    async init(type, id) {
      this.LOADING('加载数据中...')

      this.id = id
      this.mode = type
      this.edit = ['create', 'edit'].includes(this.mode)

      // 拉取表单数据，同时拉取所有来自数据源的选单数据
      await Promise.all([
		  // this.FETCH_DATASOURCE('ConferenceRoom').then(result => {
		  //   this.dataSource.StuLeaveManagement.CheckStatus = result.data.map(t => ({ text: t.name, value: t.id }))
		  // }),
		  this.FETCH_DATASOURCE('bjsj').then(result => {
			  console.log(result)
		    this.dataSource.StuLeaveManagement.ClassNo = result.data.sort((a,b)=>{return b.classno-a.classno}).map(t => ({ text: t.classname, value: t.classno })) 
		  }),
		  this.FETCH_DATASOURCE('EmpInfo').then(result => {
		  	this.dataSource.StuLeaveManagement.ClassDiredctorNo = result.data.sort((a,b)=>{return b.classno-a.classno}).map(t => ({ text: t.empname, value: t.empno }));
		  }),
		  this.FETCH_DATASOURCE('EmpInfo').then(result => {
		  	this.dataSource.StuLeaveManagement.ClassTutorNo = result.data.sort((a,b)=>{return b.classno-a.classno}).map(t => ({ text: t.empname, value: t.empno }));
		  }),
		  this.FETCH_DATASOURCE('EmpInfo').then(result => {
		  	this.dataSource.StuLeaveManagement.CheckUserNo = result.data.sort((a,b)=>{return b.classno-a.classno}).map(t => ({ text: t.empname, value: t.empno }));
		  }),
		  this.FETCH_DATASOURCE('CdDeptInfo').then(result => {
		  	this.dataSource.StuLeaveManagement.DeptNo = result.data.map(t => ({ text: t.deptname, value: t.deptno }));
		  }),
		  this.FETCH_DATASOURCE('CdMajorInfo').then(result => {
			  console.log()
		  	this.dataSource.StuLeaveManagement.MajorNo = result.data.map(t => ({ text: t.majorname, value: t.majorno }));
		  }),
      ])
      await this.fetchForm()

      this.ready = true
      this.HIDE_LOADING()
    },

    // 加载表单数据
    async fetchForm() {
      if (this.mode === 'create') {
        this.origin = await this.getDefaultForm()
      } else {
        const result = await this.HTTP_GET('/Learun/adms/EducationalAdministration/StuLeaveManagement/form', this.id)
        this.origin = await this.formatFormData(result)
		// console.log(this.origin,this.origin.prototype)
      }
      this.current = this.COPY(this.origin)
    },

    // 点击 「编辑」、「重置」、「保存」、「删除」 按钮
    async action(type) {
      switch (type) {
        case 'edit':
          this.edit = true
          break

        case 'reset':
          // this.current = this.COPY(this.origin)
          // this.edit = false
		  this.EMIT('EducationalAdministrationStuLeaveManagement-list-change')
		  // this.NAV_BACK()
		  uni.navigateBack({})
          break

        case 'save':
		  // console.log(this.current)
          const verifyResult = this.verifyForm()
          if (verifyResult.length > 0) {
            this.CONFIRM('表单验证失败', verifyResult.join('\n'))
            return
          }

          if (!(await this.CONFIRM('提交确认', '确定要提交本页表单内容吗？', true))) {
            return
          }

          this.LOADING('正在提交...')
          const postData = await this.getPostData(this.id)

          this.HTTP_POST('/Learun/adms/EducationalAdministration/StuLeaveManagement/save', postData, '表单提交保存失败').then(success => {
            this.HIDE_LOADING()
            if (!success) {
              return
            }

            this.EMIT('EducationalAdministrationStuLeaveManagement-list-change')
            // this.NAV_BACK()
			uni.navigateBack({})
            this.TOAST('提交保存成功')
          })
          break

        case 'delete':
          if (!(await this.CONFIRM('删除项目', '确定要删除本项吗？', true))) {
            return
          }

          this.LOADING('提交删除中...')
          this.HTTP_POST('/Learun/adms/EducationalAdministration/StuLeaveManagement/delete', this.id, '删除失败').then(success => {
            this.HIDE_LOADING()
            if (!success) {
              return
            }

            this.EMIT('EducationalAdministrationStuLeaveManagement-list-change')
            this.NAV_BACK()
            this.this.TOAST('删除成功', 'success')
          })
          break

        default: break
      }
    },

    // 获取表单值
    getValue(path) {
      return get(this.current, path)
    },

    // 设置表单值
    setValue(path, val) {
      set(this.current, path, val)
    }
  }
}
</script>


