<template>
  <a-modal
    title='操作'
    style='top: 20px;'
    :width='1000'
    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-row>
        <a-col :span='10'>
          <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='所属建筑'>
            <a-input placeholder='所属建筑' v-decorator="['build']" disabled='disabled' defaultValue='创智大厦' />
          </a-form-item>
        </a-col>
        <a-col :span='10'>
          <a-form-item label='展厅名称' :labelCol='labelCol' :wrapperCol='wrapperCol'>
            <a-input placeholder='展厅名称'
                     v-decorator="['meetingName',{rules: [{ required: true, message: '输入会议名称' }]}]" />
          </a-form-item>
        </a-col>
      </a-row>

      <a-row>
        <a-col :span='10'>
          <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='所属楼层'>
            <a-select v-decorator="['buildId',{rules: [{ required: true, message: '请选择所属楼层' }]}]" placeholder='所属楼层'
                      @change='getRoomList'>
              <a-select-option v-for='item in floorList' :key='item.id' :value='item.id'>
                {{ item.floorName }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span='10'>
          <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='所属房间'>
            <a-select v-decorator="['roomId',{rules: [{ required: true, message: '选择所属房间' }]}]" placeholder='所属房间'
                      @change='getArea'>
              <a-select-option v-for='item in roomList' :key='item.id' :value='item.id'>
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>

        <a-col :span='10'>
          <a-form-item label='会议面积' :labelCol='labelCol' :wrapperCol='wrapperCol'>
            <a-input placeholder='会议面积' v-decorator="['area',{rules: [{ required: true, message: '请输入会议面积' }]}]"
                     disabled='disabled' />
          </a-form-item>
        </a-col>
        <a-col :span='10'>
          <a-form-item label='容纳人数' :labelCol='labelCol' :wrapperCol='wrapperCol'>
            <a-input placeholder='容纳人数'
                     v-decorator="['capacityNum',{rules: [{ required: true, message: '输入容纳人数' }]}]" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>

        <a-col :span='10'>
          <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='会议地址 '>
            <a-input placeholder='会议地址' v-decorator="['address',{rules: [{ required: true, message: '输入会议地址' }]}]" />
          </a-form-item>
        </a-col>
        <a-col :span='10'>
          <a-form-item label='是否启用' :labelCol='labelCol' :wrapperCol='wrapperCol'>
            <a-select v-decorator="['isShow',{rules: [{ required: true, message: '请选择是否启用'}]}]" placeholder='是否启用'>

              <a-select-option value='0'>
                是
              </a-select-option>
              <a-select-option value='1'>
                否
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>

        <a-col :span='10'>
          <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='负责人'>
            <a-input placeholder='负责人' v-decorator="['headName',{rules: [{ required: true, message: '输入负责人' }]}]" />
          </a-form-item>
        </a-col>
        <a-col :span='10'>
          <a-form-item label='负责人联系方式' :labelCol='labelCol' :wrapperCol='wrapperCol'>
            <a-input placeholder='负责人联系方式'
                     v-decorator="['headPhone',{rules: [{ required: true, message: '输入负责人联系方式'}]}]" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span='10'>
          <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='启用时间'>
            <a-date-picker
              format='YYYY-MM-DD HH:mm:ss'
              valueFormat='YYYY-MM-DD HH:mm:ss'
              v-decorator="['startTime',{rules: [{ required: true, message: '输入启用时间'}]}]"
              :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
              type='date'
              placeholder='选择时间'
              style='width: 100%;'

            />
          </a-form-item>
        </a-col>
        <a-col :span='10'>
          <a-form-item label='结束时间' :labelCol='labelCol' :wrapperCol='wrapperCol'>
            <a-date-picker
              format='YYYY-MM-DD HH:mm:ss'
              valueFormat='YYYY-MM-DD HH:mm:ss'
              v-decorator="['endDate',{rules: [{ required: true, message: '输入结束时间'}]}]"
              :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
              type='date'
              placeholder='选择时间结束时间'
              style='width: 100%;'

            />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span='10'>
          <a-form-item :labelCol='labelCol' :wrapperCol='wrapperCol' label='室内图片url'>
            <!--        <a-input placeholder="室内图片url" v-decorator="['indoorPicUrl']" />-->
            <a-upload
              v-decorator="['indoorPicUrl']"
              list-type='picture-card'
              class='avatar-uploader'
              :show-upload-list='false'
              :action='uploadUrl'
              :headers='headers'
              :before-upload='beforeUpload'
              @change='handleChange'
            >
              <img v-if='imageUrl' :src='imageUrl' style='width: 102px; height: 102px' />
              <div v-else>
                <a-icon :type="loading ? 'loading' : 'plus'" />
                <div class='ant-upload-text'> 上传</div>
              </div>
            </a-upload>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>
<script>
import { saveShowroom,get } from '@/api/admin/meeting/showroom'
import {
  getFloorList,
  getRoomListByFloorId,
  selectRoomById,
} from '@/api/admin/meeting/roomContent'
import pick from 'lodash.pick'
import moment from 'moment'
import storage from 'store'

export default {
  name: 'ShowroomModal',
  props: {},
  components: {},
  mounted() {
    this.getFloorList()
  },
  data() {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      mdl: {},
      floorList: [],

      roomList: [],
      form: this.$form.createForm(this)
    }
  },
  beforeCreate() {
  },
  created() {
  },
  methods: {
    moment,
    getArea(value) {
      let param = {
        'id': value
      }
      selectRoomById(param).then((res) => {
        this.form.setFieldsValue({
          area: res.data.area
        })
      })
    },
    getFloorList() {
      getFloorList().then((res) => {
        this.floorList = res.data
      })
    },
    getRoomList(value) {
      let param = {
        'buildingDetailId': value
      }
      getRoomListByFloorId(param).then((res) => {
        this.roomList = 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', 'showroomCode', 'meetingName', 'capacityNum', 'expandNum', 'indoorPicUrl', 'address', 'startTime', 'endDate', 'isShow', 'headName', 'headPhone', 'content', 'createBy', 'createTime', 'updateBy', 'updateTime', 'version', 'deleteFlag', 'roomId', 'area'))
      })
      get(record.id).then(res =>{
        let param = {
          'buildingDetailId': res.buildId
        }
        getRoomListByFloorId(param).then((res) => {
          this.roomList = res.data
        })
        this.form.setFieldsValue({
          roomId:res.roomId,
          buildId:res.buildingName.toString(),
          isShow:res.isShow.toString()
        })

      })
    },
    handleSubmit(e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveShowroom(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>
