<template>
  <a-modal
    title='操作'
    style='top: 20px;'
    :width='800'
    v-model='visible'
    :confirmLoading='confirmLoading'
    @ok='handleSubmit'
  >
    <a-form :form='form'>
      <a-form-item style='display:none'>
        <a-input v-decorator="['id']" />
      </a-form-item>
      <a-form-item style='display:none'>
        <a-input v-decorator="['version']" />
      </a-form-item>
      <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='主题'>
        <a-input placeholder='主题' v-decorator="['title', {rules: [{required: true, message: '请输入主题'}]}]" />
      </a-form-item>
      <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='开始时间'>
        <!--        <a-input placeholder='开始时间' v-decorator="['startTime']" />-->
        <a-date-picker
          :ranges="{ Today: [moment(), moment()], 'This Month': [moment(), moment().endOf('month')] }"
          show-time
          format='YYYY-MM-DD HH:mm'
          valueFormat='YYYY-MM-DD HH:mm:ss'
          v-decorator="['startTime', {rules: [{required: true, message: '请选择开始时间'}]}]"
          placeholder='开始时间' />
      </a-form-item>
      <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='结束时间'>
        <a-date-picker
          :ranges="{ Today: [moment(), moment()], 'This Month': [moment(), moment().endOf('month')] }"
          show-time
          format='YYYY-MM-DD HH:mm'
          valueFormat='YYYY-MM-DD HH:mm:ss'
          v-decorator="['endDate', {rules: [{required: true, message: '请选择结束时间'}]}]"
          placeholder='结束时间' />
      </a-form-item>
      <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='会议室'>
        <a-select v-decorator="['roomContentId',{rules: [{ required: true, message: '请选择会议室' }]}]"
                  placeholder='请选择会议室'>
          <a-select-option v-for='item in roomContentList' :key='item.id' :value='item.id'>
            {{ item.meetingName }}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='备注'>
        <a-input placeholder='备注' v-decorator="['orderMoney']" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { saveReservation, get, roomContentList } from '@/api/admin/meeting/reservation'
import pick from 'lodash.pick'
import moment from 'moment'
import { selectRoomById } from '@/api/admin/meeting/roomContent'

export default {
  name: 'ReservationModal',
  props: {},
  components: {},
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this),
      roomContentList: []
    }
  },
  beforeCreate() {
  },
  created() {
    this.getRoomContentDataList()
  },
  methods: {
    moment,
    onRangeChangeSign() {

    },
    getRoomContentDataList() {
      roomContentList().then(res => {
        console.log('roomContentList', res)
        this.roomContentList = res.data
      })
    },
    add() {
      this.form.resetFields()
      this.edit({ id: 0 })
    },
    edit(record) {
      this.mdl = Object.assign(record)
      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'roomContentId', 'userId', 'ticketId', 'customerId', 'title', 'stauts', 'isAfterSale', 'oderNumber', 'orderMoney', 'cancelTime', 'cancelResaon', 'visitType', 'explainNeedType', 'meetingNeedType', 'meetingId', 'photographType', 'startTime', 'endTime', 'deleteFlag', 'createBy', 'createTime', 'updateBy', 'updateTime', 'remake'))
      })
      get(record.id).then(res => {
        this.form.setFieldsValue({
          endDate: res.endDate,
          duration: res.duration

        })
      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveReservation(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
