<template>
  <div>
    <el-table
      :data="chartList"
      border
      stripe>
      <el-table-column
        prop="name"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="action"
        label="操作">
        <template slot-scope="scope">
          <el-button @click="handelSelectChart(scope.row)">
            编辑座位
          </el-button>
          <el-button @click="$refs.chartInfo.show(scope.row)">
            座位图信息修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 座位图信息编辑 -->
    <seatChartInfoModal
      ref="chartInfo"
      @update="getSeatChartList"></seatChartInfoModal>

    <SeatEditor ref="seatEditor"></SeatEditor>
  </div>
</template>

<script>
import { getSeatConfigList, getSeatArea, addSeat, getSeatList, deleteSeat, updateSeat } from '@/api/seat'

import seatChartInfoModal from './seatChartInfoModal'
import SeatEditor from './SeatEditor/index.vue'
import groupby from 'lodash/groupBy'

export default {
  components: {
    SeatEditor,
    seatChartInfoModal
  },
  data () {
    return {
      chart: null,
      chartList: [],
      areaList: [],
      selectedChart: 0,
      selectedArea: 0,
      selectedBlankSeats: [],
      existSeats: []
    }
  },
  mounted () {
    this.getSeatChartList()
  },
  methods: {
    handelSelectChart (chart) {
      this.$refs.seatEditor.show(chart)
    },
    getSeatChartList () {
      getSeatConfigList({}).then(res => {
        this.chartList = res.list
      })
    },
    async getAreaList () {
      this.colorMap = {}

      await getSeatArea({ keyWords1: this.selectedChart }).then(res => {
        this.areaList = res.list
        this.selectedArea = (this.areaList[0] && this.areaList[0].id) || 0

        this.areaList.forEach((item, index) => {
          this.colorMap[item.id] = this.colorPalette[index]
        })
      })

      this.getSeatList()
    },

    getSeatList () {
      getSeatList({ keyWords1: this.selectedChart, pageSize: -1, pageNum: 0 }).then(res => {
        this.existSeats = res.list.map(item => {
          item.fill = this.colorMap[item.area_id] || 'gray'
          return item
        })
      })
    },

    handleAddToArea () {
      if (!this.selectedArea) return this.$message.error('请选择区域')

      const selectedSeats = this.$refs.seatChart.getSelectedSeat()

      addSeat({
        config_id: this.selectedChart,
        area_id: this.selectedArea,
        seatList: selectedSeats
      }).then(res => {
        this.$message.success('添加成功')
        this.getSeatList()
      })
    },
    handleDeleteSeat () {
      let selectedSeats = this.$refs.seatChart.getSelectedSeat()

      selectedSeats = selectedSeats.filter(item => {
        return item.area_id
      })

      if (!selectedSeats.length) return this.$message.error('请选择要删除的座位')

      this.$confirm('确认删除吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteSeat({
          idList: selectedSeats.map(i => i.id)
        }).then(res => {
          this.$message.success('删除成功')
          this.getSeatList()
        })
      }).catch(() => {})
    },
    // 批量重命名
    handleRenameAll () {
      let seats = JSON.parse(JSON.stringify(this.existSeats))
      // 按行分组
      let groupSeats = groupby(seats, i => i.y)

      // console.log(groupSeats)

      // 所有行号
      let rowKeys = Object.keys(groupSeats)

      // 排序
      rowKeys.sort((a, b) => {
        return Number(a) - Number(b)
      })

      rowKeys.forEach((rowkey, rowIndex) => {
        let row = groupSeats[rowkey]

        // 按x排序
        row.sort((a, b) => {
          return a.x - b.x
        })

        // console.log(rowkey, row, rowIndex)

        row.forEach((item, index) => {
          item.name = (index + 1 + '')
          item.row_tag = (rowIndex + 1 + '')
        })
      })

      updateSeat({
        seats
      }).then(res => {
        this.$message.success('操作成功')

        this.getSeatList()
      })
    }
  },
  watch: {
    selectedArea (val) {
      if (val) {

      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
