<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-select v-decorator="['tenantId', {rules: [{ required: true, message: '请选择所属地区' }]}]"
                  @change='selectTenant'
                  :disabled='tenantEnable'>
          <a-select-option v-for='item in tenantList' :key='item.id'>{{ item.name }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='所属园区'>
        <a-select v-decorator="['parkId', {rules: [{ required: true, message: '请选择所属园区' }]}]"
                  @change='selectPark'
                  :disabled='parkEnable'>
          <a-select-option v-for='item in parkList' :key='item.id'>{{ item.name }}</a-select-option>
        </a-select>
      </a-form-item>

      <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='所属楼宇'>
        <a-select v-decorator="['buildingId', {rules: [{ required: true, message: '请选择所属楼宇' }]}]"
                  @change='selectBuilding'>
          <a-select-option v-for='item in buildingList' :key='item.id'>{{ item.buildingName }}</a-select-option>
        </a-select>
      </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="['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-input placeholder='备注' v-decorator="['remake']" />
      </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'
import { getInfo } from '@/api/login'
import { getTenantList } from '@/api/tenant'
import { getParkList } from '@/api/admin/park'
import { getBuildingList } from '@/api/admin/building'
import { getBuildingDetailList } from '@/api/admin/buildingDetail'

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),
      tenantList: [], // 地区
      parkList: [], // 园区
      buildingList: [], // 楼宇
      roomContentList: [], // 会议室
      tenantEnable: false,
      parkEnable: false
    }
  },
  beforeCreate() {
  },
  created() {
    // 查询地区
    this.getTenantData()
  },
  methods: {
    // 获取用户详细信息
    getUserDetail() {
      getInfo().then(res => {
        console.log('getUserDetail', res)
        this.userDetail = res
        // 园区：5
        if (this.userDetail.roleIds && this.userDetail.roleIds.length > 0 && this.userDetail.roleIds[0] == 5) {
          //
          // 判断：是新增就查询园区
          let { id } = this.mdl
          if (!id) {
            // 选择地区 -> 查询园区
            this.selectTenant(this.userDetail.tenantId)
            // 选择园区 -> 查询楼宇
            this.selectPark(this.userDetail.parkId)
          }
          //
          // 不可编辑
          this.tenantEnable = true
          this.parkEnable = true
          // 关联地区和园区
          this.mdl.parkId = this.userDetail.parkId
          this.mdl.tenantId = this.userDetail.tenantId
          this.form.setFieldsValue({
            parkId: this.mdl.parkId,
            tenantId: this.mdl.tenantId
          })
        }
      })
    },
    // 查询地区
    getTenantData() {
      getTenantList().then(res => {
        this.tenantList = res.rows
      })
    },
    // 选择地区 -> 查询园区
    selectTenant(id) {
      console.log('selectTenant', id)
      // 清空数据
      this.mdl.parkId = null // 园区
      this.mdl.buildingId = null // 楼宇
      this.mdl.roomContentId = null // 会议室

      this.form.setFieldsValue({
        parkId: null, // 园区
        buildingId: null, // 楼宇
        roomContentId: null // 会议室
      })
      // 查询园区
      getParkList({
        tenantId: id
      }).then(res => {
        this.parkList = res.rows
      })
    },
    // 选择园区 -> 查询楼宇
    selectPark(id) {
      // 清空数据
      this.mdl.buildingId = null // 楼宇
      this.mdl.roomContentId = null // 会议室

      this.form.setFieldsValue({
        buildingId: null, // 楼宇
        roomContentId: null // 会议室
      })
      // 查询楼宇
      getBuildingList({
        parkId: id
      }).then(res => {
        this.buildingList = res.rows
      })
    },
    // 选择楼宇 -> 查询会议室
    selectBuilding(id) {
      // 清空数据
      this.mdl.roomContentId = null // 会议室

      this.form.setFieldsValue({
        roomContentId: null // 会议室
      })
      roomContentList({
        buildingId: id
      }).then(res => {
        console.log('roomContentList', res)
        this.roomContentList = res.data
      })
    },
    moment,
    onRangeChangeSign() {

    },
    add() {
      this.form.resetFields()
      this.edit({ id: 0 })
    },
    edit(record) {
      this.mdl = Object.assign(this.mdl, record)
      let copyMdl = JSON.parse(JSON.stringify(this.mdl))
      //
      if (record.id != 0) { // 编辑
        // 选择地区 -> 查询园区
        if (copyMdl.tenantId) {
          this.mdl = JSON.parse(JSON.stringify(copyMdl))
          this.selectTenant(copyMdl.tenantId)
        }
        // 选择园区 -> 查询楼宇
        if (copyMdl.parkId) {
          this.mdl = JSON.parse(JSON.stringify(copyMdl))
          this.selectPark(copyMdl.parkId)
        }
        // 选择楼宇 -> 查询会议室
        if (copyMdl.buildingId) {
          this.mdl = JSON.parse(JSON.stringify(copyMdl))
          this.selectBuilding(copyMdl.buildingId)
        }
        // 防止替换
        this.mdl = JSON.parse(JSON.stringify(copyMdl))

      } else { // 新增

      }
      // 获取用户详细信息
      this.getUserDetail()

      this.visible = true
      this.$nextTick(() => {
        this.form.setFieldsValue(pick(this.mdl, 'id', 'tenantId', 'parkId', 'buildingId', 'roomContentId', 'userId', 'ticketId', 'customerId',
          'title', 'stauts', 'isAfterSale', 'oderNumber', 'orderMoney', 'cancelTime', 'cancelResaon',
          'visitType', 'explainNeedType', 'meetingNeedType', 'meetingId', 'photographType',
          'startTime', 'endTime', 'endDate', 'deleteFlag', 'createBy', 'createTime', 'updateBy', 'updateTime', 'remake'))
      })


    },
    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>
