<template> 
  <view class="page"> 
    <view v-if="ready">
		<view class="paper-title">
			{{formData.VTitle}}
			<view class="paper-time">
				{{formData.VStartTime}}
				-
				{{formData.VStopTime}}
			</view>
		</view>
		<view v-for="(item,index) in formData.Eval_Questions" :key="item.QID">
			<view class="questionTitle">
				<text style="color: red;">{{item.QMust?"* ":""}}</text>
				{{item.QTitle}}
			</view>
			<!-- 单选 -->
			<l-select
			  v-if="item.QType == 1"
			  v-model="item.IID"
			  @change="(e)=>{checkBoxGroupChange(item.QID,e)}"
			  :range="item.Eval_QuestionItems"
			  title=""
			  placeholder="请选择"
			  :disabled="false"
			  textField="ITitle"
			  valueField="IID"
			/>
			<!-- 多选 -->
			<view v-if="item.QType == 2">
				<checkbox-group v-model="item.checked" @change="(e)=>{checkBoxGroupChange(item.QID,e.detail.value)}">
					<view v-for="item1 in item.Eval_QuestionItems" :key="item1.IID">
						<l-checkbox :title="item1.ITitle" v-model="item1.isChecked" :checkboxValue="item1.IID.toString()" />
						<l-textarea 
						  simpleMode
						  v-if="item1.QShowText&&
						  checkBoxs[item.QID]&&
						  checkBoxs[item.QID].includes(item1.IID)"
						  v-model="uTexts[item1.IID]"
						  style="margin-top: 6px;"
						  placeholder="请输入" 
						/>
					</view>
				</checkbox-group>
			</view>
			<!-- 填空 -->
			<l-textarea v-if="item.QType == 3" v-model="item.QContent" placeholder="请输入" />
			<!-- 校验提示 -->
			<view v-if="validate[item.QID]" style="color: red;font-size: 12px;margin-bottom: 4px;padding-left: 12px;">
				{{validate[item.QID]}}
			</view>
		</view>
		
		<!-- <l-input v-model="text" disabled title="你说呢你说呢？"></l-input>
		<radio-group>
		  <l-radio v-model="radioValue" title="单选框1" radioValue="1" />
		  <l-radio v-model="radioValue" title="单选框2" radioValue="2" />
		</radio-group> -->
    </view>
  
    <view v-if="ready" class="bg-white margin-tb padding" style="padding-top: 0; overflow: hidden;">
      <l-button v-if="edit" @click="action('save')" size="lg" style="background-color: #0C86D8;color: #fff;" class="block margin-top" block> 
        提交保存 
      </l-button>
    </view> 
  </view> 
</template> 
  
  
<script> 
/* 
 * 版 本 Learun-ADMS V7.0.3 力软敏捷开发框架(http://www.learun.cn) 
 * Copyright (c) 2013-2020 上海力软信息技术有限公司 
 * 创建人：超级管理员 
 * 日  期：2020-10-16 15:39 
 * 描  述：工作日志 
 */
  
/** 
 * 本段代码由移动端代码生成器输出，移动端须 2.2.0 版本及以上可以使用 
 * 请在移动端 /pages.json 中的 pages 字段中添加一条记录： 
 * { "path": "pages/EducationalAdministration/JournalSend/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 {
		aa:"",
		bbb:[],
      // 页面相关参数 
      id: null, 
      mode: null, 
      edit: null, 
      ready: false, 
      ready: false,
	  formData:{
		  Eval_Questions:[]
	  },
	  submitParams:null,
	  uTexts:{},
	  checkBoxs:{},
	  validate:{},
    }
  },
  async onLoad({ id,EmpNo,LessonNo }) {
	this.submitParams = {EmpNo,LessonNo}
    await this.init(id)
  }, 
  
  methods: { 
	  checkBoxGroupChange(id,value){
		this.checkBoxs[id] = value
	  },
    // 页面初始化 
    async init( id ) { 
      this.LOADING('加载数据中...')
      this.id = id
      await this.fetchForm() 
  
      this.ready = true
      this.HIDE_LOADING() 
    }, 
  
    // 加载表单数据 
    async fetchForm() {
      return this.HTTP_GET('learun/adms/eval/paper', {VID:this.id}, '加载数据时出错').then(success => {
		this.HIDE_LOADING() 
		if (!success) { 
		  return
		}
		if(success){
			this.edit = true
		}
		this.formData = success
      }) 
	  
    }, 
  
    // 点击 「编辑」、「重置」、「保存」、「删除」 按钮 
    async action(type) { 
      switch (type) { 
        case 'edit': 
          this.edit = true
          break
  
        case 'reset': 
          this.current = this.COPY(this.origin) 
          this.edit = false
          break
  
        case 'save': 
          if (!(await this.CONFIRM('提交确认', '确定要提交本页表单内容吗？', true))) { 
            return
          } 
  
          this.LOADING('正在提交...')
		  let postarray = []
		  this.formData.Eval_Questions.map(item=>{
			  let validate = {}
			  switch (item.QType){
				case 1:
				  if(item.QMust){
					validate[item.QID] = this.checkBoxs[item.QID]?'':'此项必填！'
				  }
				  this.validate = {...this.validate,...validate}
				  postarray.push({
					  QID:item.QID,
					  IID:this.checkBoxs[item.QID]?this.checkBoxs[item.QID].toString():'',
					  UText:item.UText||'',
				  })
				  break;
				case 2:
				  if(item.QMust){
					validate[item.QID] = (this.checkBoxs[item.QID]&&this.checkBoxs[item.QID].length)?'':"此项必填！"
				    this.validate = {...this.validate,...validate}
				  }
				  if(this.checkBoxs[item.QID]&&this.checkBoxs[item.QID].length){
					  this.checkBoxs[item.QID].map(t=>{
						  postarray.push({
						    QID:item.QID,
						    IID:t,
						    UText:this.uTexts[t]||'',
						  })
					  })
				  }
				  break;
				case 3:
					if(item.QMust){
				      validate[item.QID] = item.QContent?'':"此项必填"
					  this.validate = {...this.validate,...validate}
					}
				    if(this.checkBoxs[item.QID]&&this.checkBoxs[item.QID].length){
				  	  this.checkBoxs[item.QID].map(t=>{
				  		  postarray.push({
				  		    QID:item.QID,
				  		    IID:'',
				  		    QContent:item.QContent||'',
				  		  })
				  	  })
				    }
				    break;
			  }
		  })
		  
		  let validataArr = Object.values(this.validate)
		  if(validataArr.some(t=>t)){
			  this.TOAST("请完善表单信息！")
			  return
		  }
          const postData = {
			  VID:this.formData.VID,
			  ...this.submitParams,
			  postarray
		  }
		  console.log(postData)
          this.HTTP_POST('learun/adms/eval/savepaper', postData, '表单提交保存失败').then(success => { 
            this.HIDE_LOADING() 
            if (!success) { 
              return
            } 
            this.TOAST('提交保存成功')
			setTimeout(()=>{
				this.NAV_BACK()
			},200)
          })
          break
  
        case 'delete': 
          if (!(await this.CONFIRM('删除项目', '确定要删除本项吗？', true))) { 
            return
          } 
  
          this.LOADING('提交删除中...') 
          this.HTTP_POST('learun/adms/EducationalAdministration/Journal/delete', this.id, '删除失败').then(success => { 
            this.HIDE_LOADING() 
            if (!success) { 
              return
            } 
  
            this.EMIT('EducationalAdministrationJournalSend-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> 
<style scoped lang="scss">
	.paper-title{
		font-size: 20px;
		color: #000;
		text-align: center;
		margin: 16px 0;
	}
	.paper-time{
		color: #606266;
		font-size: 12px;
		text-align: center;
	}
	.questionTitle{
		line-height: 18px;
		background-color: #fff;
		color: #000;
		margin-top: 8px;
		margin-bottom: 1px;
		padding: 16px 12px;
	}
</style>