<template>
  <a-card :bordered='false'>
    <div class='table-page-search-wrapper'>
      <a-form layout='inline'>
        <a-row :gutter='48'>
          <a-col :md='5' :sm='15'>
            <a-form-item label='会议室名称'>
              <a-input placeholder='请输入名称' v-model='queryParam.meetingName' />
            </a-form-item>
          </a-col>
          <a-col :md='5' :sm='15'>
            <a-form-item label='所在楼层'>
              <a-select v-model='queryParam.typeName'>
                <a-select-option v-for='item in typeList' :value='item.value'>
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='5' :sm='15'>
            <a-form-item label='排列形状'>
              <a-select v-model='queryParam.shape' placeholder='请选择排列形状'>
                <a-select-option v-for='item in shapeList' :value='item.value'>
                  {{ item.text }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='5' :sm='15'>
            <a-form-item label='容纳人数'>
              <a-select v-model='queryParam.capacityNum' placeholder='请选择范围'>
                <a-select-option value=''>
                  全部
                </a-select-option>
                <a-select-option value='1'>
                  0-10
                </a-select-option>
                <a-select-option value='2'>
                  11-20
                </a-select-option>
                <a-select-option value='3'>
                  21-30
                </a-select-option>
                <a-select-option value='4'>
                  31-40
                </a-select-option>
                <a-select-option value='5'>
                  41-50
                </a-select-option>
                <a-select-option value='6'>
                  50-100
                </a-select-option>
                <a-select-option value='7'>
                  100以上
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md='8' :sm='24'>
            <span class='table-page-search-submitButtons'>
              <a-button type='primary' @click='$refs.table.refresh(true)'>查询</a-button>
              <a-button style='margin-left: 8px' @click='reset'>重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <div class='table-operator'>
      <a-button v-if='addEnable' type='primary' icon='plus' @click='$refs.modal.add()'>新建</a-button>
      <a-dropdown v-if='removeEnable && selectedRowKeys.length > 0'>
        <a-button type='danger' icon='delete' @click='delByIds(selectedRowKeys)'>删除</a-button>
      </a-dropdown>
    </div>
    <s-table
      size='default'
      ref='table'
      rowKey='id'
      showPagination='true'
      :rowSelection='{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }'
      :columns='columns'
      :data='loadData'
    >
     <span slot='type' slot-scope='text'>
        {{ text | typeFilter }}
      </span>
      <span slot='item' slot-scope='text,record'>
        <a @click='handleItemTable(record)'>{{ record.itemCount }}</a>
      </span>
      <span slot='serve' slot-scope='text,record'>
        <a @click='handleServeTable(record)'>{{ record.serveCount }}</a>
      </span>
      <span slot='action' slot-scope='text, record'>
        <a v-if='editEnabel' @click='handleEdit(record.id)'>编辑</a>
        <a-divider type='vertical' />
        <a v-if='removeEnable' @click='delByIds([record.id])'>删除</a>
      </span>
    </s-table>

    <roomContent-modal ref='modal' @ok='handleOk' />
    <a-modal v-model='visible' title='会议设备' width='600px' @ok='itemHandleOk()'>
      <s-table
        size='default'
        ref='itemTable'
        rowKey='id'
        :rowSelection='{ selectedRowKeys: selectedItemRowKeys, onChange: onItemSelectChange }'
        :columns='itemColumns'
        :data='itemLoadData'
      >
      </s-table>
    </a-modal>
    <a-modal v-model='visible1' title='会议服务' width='600px' @ok='serveHandleOk()'>
      <s-table
        size='default'
        ref='serveTable'
        rowKey='id'
        :rowSelection='{ selectedRowKeys: selectedServeRowKeys, onChange: onServeSelectChange }'
        :columns='serveColumns'
        :data='serveLoadData'
      >
      </s-table>
    </a-modal>
  </a-card>


</template>

<script>
import { STable } from '@/components'

import {
  getRoomContentList, delRoomContent, selectSearchInfoByType,
  selectSearchInfoByShape, selectSearchInfoByCapacity, addRoomServe,
  selectRoomServeByRoom, selectRoomItemByRoom, getMeetingDict
} from '@/api/admin/meeting/roomContent'
import { getRoomServeList } from '@/api/admin/meeting/roomServe'
import { getRoomItemList } from '@/api/admin/meeting/roomItem'
import RoomContentModal from './modules/RoomContentModal.vue'
import { checkPermission } from '@/utils/permissions'
import { getDictArray } from '@/utils/dict'
import { addRoomItem } from '../../../api/admin/meeting/roomContent'
import { delRepairDevice } from '@/api/admin/repair/repairDevice'

const typeMap = {}
const serveTypeMap = {}
export default {
  name: 'TableList',
  components: {
    STable,
    RoomContentModal
  },
  data() {
    return {
      parkId: 25,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      visible: false,
      visible1: false,
      typeMap,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        meetingName: '',
        typeName: '',
        shape: '',
        capacityNum: ''
      },
      itemQueryParam: {},
      serveQueryParam: {},
      capacityList: [],
      shapeList: [],
      typeList: [],
      serveTypeMap,
      itemColumns: [
        {
          title: '物品名称',
          dataIndex: 'name'
        }
      ],
      serveColumns: [
        {
          title: '服务名称',
          dataIndex: 'serveName'
        }
      ],
      // 表头
      columns: [
        {
          title: '会议室名称',
          dataIndex: 'name'
        },
        {
          title: '所属楼层',
          dataIndex: 'floor',
        },
        {
          title: '所属房间',
          dataIndex: 'roomNum'
        },
        {
          title: '容纳人数',
          dataIndex: 'capacityNum'
        },
        {
          title: '排列形状',
          dataIndex: 'typeName'
        },
        {
          title: '会议室设备',
          dataIndex: 'device',
        },
        // {
        //   title: '关联服务',
        //   dataIndex: 'serveCount',
        //   scopedSlots: { customRender: 'serve' }
        // },
        {
          title: '操作',
          width: '200px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const param = {
          name: this.queryParam.meetingName, // 名称
          floor: this.queryParam.typeName, // 楼层名称
          typeName: this.queryParam.shape, // 形状
          capacityNum: this.queryParam.capacityNum // 容纳人数
        }
        // if (param.typeName === '') {
        //   delete param.typeName
        // }
        // if (param.shape === '') {
        //   delete param.shape
        // }
        // if (param.capacityNum === '') {
        //   delete param.capacityNum
        // }
        return getRoomContentList(Object.assign(parameter, param))
      },
      itemLoadData: (parameter) => {
        let param = {
          parkId: this.parkId
        }
        return getRoomItemList(Object.assign(parameter, param))
      },
      serveLoadData: (parameter) => {
        let param = {
          parkId: this.parkId
        }
        return getRoomServeList(Object.assign(parameter, param))
      },
      selectedRowKeys: [],
      selectedRows: [],

      selectedItemRowKeys: [],
      selectedItemRows: [],

      selectedServeRowKeys: [],
      selectedServeRows: [],
      meetingId: '',
      addEnable: checkPermission('meeting:roomContent:add'),
      editEnabel: checkPermission('meeting:roomContent:edit'),
      removeEnable: checkPermission('meeting:roomContent:remove'),
      // 删除
      deleteVisible: false,
      deleteLoading: false,
      deleteIds: null
    }
  },
  filters: {
    typeFilter(Type) {
      return typeMap[Type].text
    },
    serveTypeFilter(serveType) {
      return serveTypeMap[serveType].text
    }
  },
  async created() {
    const TypeData = await getDictArray('meeting_type')
    TypeData.map(d => {
      typeMap[d.dictValue] = { text: d.dictLabel }
    })
    // console.log(typeMap)
    const serveTypeData = await getDictArray('serve_type')
    serveTypeData.map(d => {
      serveTypeMap[d.dictValue] = { text: d.dictLabel }
    })
  },
  mounted() {
    this.getDict()
    // this.selectSearchInfoByType()
    // this.selectSearchInfoByShape()
    // this.selectSearchInfoByCapacity()
  },
  methods: {
    // 获取查询参数
    getDict() {
      getMeetingDict().then((res) => {
        // 楼层
        let _typeList = []
        let dataObj = res.data
        for (let key in dataObj.floors) {
          let eachObj = dataObj.floors[key]
          const keys = Object.keys(eachObj)
          _typeList.push({
            text: keys[0],
            value: keys[0]
          })
        }
        _typeList = [{
          text: '全部',
          value: ''
        }, ..._typeList]
        this.typeList = _typeList

        // 形状
        let _shapeList = []
        for (let key in dataObj.types) {
          let eachObj = dataObj.types[key]
          const keys = Object.keys(eachObj)
          _shapeList.push({
            text: keys[0],
            value: keys[0]
          })
        }
        _shapeList = [{
          text: '全部',
          value: ''
        }, ..._shapeList]
        this.shapeList = _shapeList
      })
    },
    reset() {
      this.queryParam = {
        meetingName: '',
        typeName: '',
        shape: '',
        capacityNum: ''
      }
      this.$refs.table.refresh(true)
    },
    itemHandleOk() {
      console.log(this.meetingId)
      let params = {
        itemIds: this.selectedItemRowKeys,
        roomContentId: this.meetingId
      }
      addRoomItem(params).then((res) => {
        if (res.code == 0) {
          this.$message.success('关联成功')
          this.visible = false
          this.selectedItemRowKeys = []
          this.selectedItemRows = []
          this.handleOk()
        }
      })
    },

    serveHandleOk() {
      // 新增 物品跟会议室关联
      let params = {
        serveIds: this.selectedServeRowKeys,
        romeContentId: this.meetingId
      }
      addRoomServe(params).then((res) => {
        if (res.code == 0) {
          this.$message.success('关联成功')
          this.visible1 = false
          this.selectedServeRowKeys = []
          this.selectedServeRows = []
          this.handleOk()
        }
      })


    },
    onItemSelectChange(selectedRowKeys, selectedRows) {
      this.selectedItemRowKeys = selectedRowKeys
      this.selectedItemRows = selectedRows

    },
    onServeSelectChange(selectedRowKeys, selectedRows) {
      this.selectedServeRowKeys = selectedRowKeys
      this.selectedServeRows = selectedRows

    },
    handleItemTable(record) {
      let params = {
        roomContentId: record.id
      }
      // this.parkId = record.parkId

      selectRoomItemByRoom(params).then((res) => {
        this.selectedItemRowKeys = res.data.map(d => d.itemId)
        this.selectedItemRows = res.data
      })
      this.visible = true
      this.meetingId = record.id

    },
    handleServeTable(record) {
      let params = {
        romeContentId: record.id
      }
      // this.parkId = record.parkId
      selectRoomServeByRoom(params).then((res) => {
        this.selectedServeRowKeys = res.data.map(d => d.serveId)
        this.selectedServeRows = res.data
      })
      this.visible1 = true
      this.meetingId = record.id

    },

    // 获取查询条件
    // selectSearchInfoByType() {
    //   selectSearchInfoByType().then((res) => {
    //     this.typeList = res.roomContents
    //   })
    // },
    // selectSearchInfoByShape() {
    //   selectSearchInfoByShape().then((res) => {
    //     this.shapeList = res.roomContents
    //   })
    // },
    // selectSearchInfoByCapacity() {
    //   selectSearchInfoByCapacity().then((res) => {
    //     this.capacityList = res.roomContents
    //   })
    // },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleAdd() {
      this.$refs.modal.add()
    },
    handleEdit(id) {
      this.$refs.modal.edit(id)
    },
    handleOk() {
      this.$refs.table.refresh(true)
    },
    delByIds(ids) {
      const _this = this
      if (ids === '') {
        this.$message.error('请选择后删除！')
        return
      }
      this.$confirm({
        title: '警告',
        content: '删除后不可恢复，是否确认删除?',
        okText: '是',
        okType: 'warning',
        cancelText: '否',
        onOk() {
          delRoomContent({ ids: ids.join(',') }).then((res) => {
            if (res.code === 0) {
              _this.$message.success('删除成功')
              _this.handleOk()
            } else {
              _this.$message.error(res.msg)
            }
            _this.selectedRowKeys = []
            // 删除结束
            _this.deleteVisible = false
            _this.deleteLoading = false
          })
        },
        onCancel() {
        }
      })
    },

  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
