const app = getApp()
const api = require("../../../utils/request.js");
Page({
  data: {
    formData: [
      {
        type: 'input',
        id:'name',
        lable:'考试名称',
        isRequired: true,//是否必填
        maxLength: 20,//最大长度
        defaultValue:'',//初始值
        rules:[//规则验证数组
          {
            regular: '^\\S*$',//正则字符串
            tips: '不能有空格'//错误提示
          },
        ]
      },
      {
        type: 'input',
        id:'place',
        lable:'考试地点',
        isRequired: true,//是否必填
        maxLength: 50,//最大长度
        defaultValue:'',//初始值
        rules:[//规则验证数组
          {
            regular: '^\\S*$',//正则字符串
            tips: '不能有空格'//错误提示
          },
        ]
      },
      {
        type: 'input',
        id: 'chance',
        lable: '考试机会',
        defaultValue: 3,
        inputType: 'digit', //对应input组件type值(text,number)
        placeholder: '请填写数字',
        isRequired: true,//是否必填
        //disabled:true,
        rules: [
          
        ]
      },
      {
        type: 'picker',
        id: 'workscope',
        lable: '工作类别',
        defaultIdx:0,//默认选择索引
        // disabled:true,
        isRequired:true,
        range:[
          {
            id: 0,
            name: '正常'
          },
          {
            id: 1,
            name: '异常'
          },
        
        ]
      },
      {
        type: 'date',
        id: 'daterange',
        lable: '开关时间',
        isRequired: true,
        /* 显示完整时间包含时分秒；当使用endDate的时候关闭,不要同时打开, 否则日期将会换行；
           与config中的colum属性共同设置
        */
        completeTime:true, //显示完整时间, 包含时分秒
        config: {
          endDate: true,
          dateLimit: true,
          // initStartTime: "2020-01-01 12:32:44",
          // initEndTime: "2020-12-01 12:32:44",
          column: "minute",//day、hour、minute、secend
          limitStartTime: "2000-01-01 00:00:59",
          limitEndTime: "2100-01-01 00:00:59"
        }
      },
      {
        type: 'input',
        id:'proctor_name',
        lable:'监考人姓名',
        isRequired: true,//是否必填
        maxLength: 50,//最大长度
        defaultValue:'',//初始值
        rules:[//规则验证数组
          {
            regular: '^\\S*$',//正则字符串
            tips: '不能有空格'//错误提示
          },
        ]
      },
      {
        type: 'input',
        id:'proctor_phone',
        lable:'监考人联系方式',
        isRequired: true,//是否必填
        maxLength: 50,//最大长度
        defaultValue:'',//初始值
        rules:[//规则验证数组
          {
            regular: '^\\S*$',//正则字符串
            tips: '不能有空格'//错误提示
          },
        ]
      },
      {
        type: 'input',
        id:'train_name',
        lable:'培训名称',
        isRequired: false,//是否必填
        maxLength: 50,//最大长度
        defaultValue:null,//初始值
        rules:[//规则验证数组
          {
            regular: '^\\S*$',//正则字符串
            tips: '不能有空格'//错误提示
          },
        ]
      },
      {
        type: 'date',
        id: 'daterange2',
        lable: '培训时间',
        isRequired: false,
        /* 显示完整时间包含时分秒；当使用endDate的时候关闭,不要同时打开, 否则日期将会换行；
           与config中的colum属性共同设置
        */
        completeTime:false, //显示完整时间, 包含时分秒
        config: {
          endDate: true,
          dateLimit: true,
          // initStartTime: "2020-01-01 12:32:44",
          // initEndTime: "2020-12-01 12:32:44",
          column: "day",//day、hour、minute、secend
          limitStartTime: "2000-01-01 00:00:59",
          limitEndTime: "2100-01-01 00:00:59"
        }
      },
    ],
    toSubmit: Math.random()
  },
  onFormSubmit(e){
    console.log('表单提交: ', e);
    let x = {};
    x.opentime = e.detail.daterange.startDate;
    x.closetime = e.detail.daterange.endDate;
    x.train_name = e.detail.train_name.value;
    if (e.detail.daterange2.startDate){
      x.train_start_date = e.detail.daterange2.startDate
    }
    if (e.detail.daterange2.endDate){
      x.train_end_date = e.detail.daterange2.endDate
    }
    x.name = e.detail.name.value
    x.place = e.detail.place.value
    x.chance = e.detail.chance.value
    x.workscope = e.detail.workscope.original.range[e.detail.workscope.idx].id
    x.proctor_name = e.detail.proctor_name.value
    x.proctor_phone = e.detail.proctor_phone.value
    wx.showModal({
      content: '确认发布考试吗',
      success (res) {
        if (res.confirm) {
          api.requesta('/examtest/exam/', 'POST', x).then(res=>{
            let rdata = res.data
            wx.showModal({
              title: '发布成功',
              content: '考试号为' + rdata.code,
              showCancel: false,
              success (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: '/pages/admin/exam/detail?id='+rdata.id,
                  })
                }
              }
            })
          })
        } 
      }
    })
  },
  onFormChange(e){
    //console.log('表单变化: ',e);
  },
  //变更数值, 触发表单提交事件
  toSubmitChange(){
    this.setData({
      toSubmit: Math.random()
    })
  },
  onLoad: function () {
    api.requesta('/examtest/workscope/?can_exam=true', 'GET').then(res=>{
      this.data.formData[3].range=res.data
      this.setData({
        formData: this.data.formData
      })
    })
  },
})
