<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="['type', {rules: [{ required: true, message: '请选择分类' }]}]">
          <a-select-option v-for="item in typeData" :key="item.value">{{ item.value }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="设备名称">
        <a-input placeholder="设备名称" v-decorator="['equipmentName' , {rules: [{ required: true, message: '请输入设备名称' }]}]" />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="设备编号">
        <a-input placeholder="设备编号" v-decorator="['equipmentNum' , {rules: [{ required: true, message: '请输入设备编号' }]}]"  />
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="会议室">
        <a-select :label-in-value="true"
                  v-decorator="['contentId', {rules: [{ required: true, message: '请选择会议室' }]}]"
                  @change="selectRoom">
          <a-select-option v-for="item in roomList" :key="item.id" :value="item.id">{{ item.meetingName }}</a-select-option>
        </a-select>
      </a-form-item>

    </a-form>
  </a-modal>
</template>
<script>
import { saveWisdomDevice } from '@/api/admin/wisdomDevice'
import { getWisdomRoomList } from '@/api/admin/wisdomRoom'

import pick from 'lodash.pick'

export default {
  name: 'WisdomDeviceModal',
  props: {},
  components: {},
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      roomList: [],
      typeData: [
        {
          key: '1',
          value: '灯条'
        },
        {
          key: '2',
          value: '雾化玻璃'
        },
        {
          key: '3',
          value: '窗帘'
        },
        {
          key: '4',
          value: '插座'
        },
        {
          key: '5',
          value: '调光灯'
        }
      ],
      confirmLoading: false,
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
    this.selectRoomList()
  },
  methods: {
    selectRoomList () {
      getWisdomRoomList().then(res => {
        console.log(res)
        this.roomList = res.rows
      })
    },
    selectRoom (item) {
      console.log(item)
    },
    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', 'type', 'equipmentName', 'status', 'equipmentNum', 'pic', 'deleteFlag', 'createBy', 'createTime', 'updateBy', 'updateTime', 'ip', 'tenantId', 'parkId', 'contentId', 'contentName'))
      })
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveWisdomDevice(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>
