<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input
        v-model="listQuery.name" clearable size="mini" class="filter-item"
        style="width: 200px;" placeholder="请输入桌号名称"
      />
      <el-select
        v-model="listQuery.isStop" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请选择是否停用"
      >
        <el-option label="否" :value="0" />
        <el-option label="是" :value="1" />
      </el-select>
      <el-button
        size="mini" class="filter-item" type="primary" icon="el-icon-search"
        style="margin-left:10px;"
        @click="handleSearch"
      >
        查找
      </el-button>
      <br />
      <el-button
        size="mini" type="primary" icon="el-icon-plus"
        @click="$refs.EditModal && $refs.EditModal.handleOpen({ id: '' })"
      >
        添加
      </el-button>
    </div>

    <!-- 查询结果 -->
    <div v-tableHeight>
      <el-table
        v-loading="listLoading" height="100%" element-loading-text="正在查询中。。。" :data="list"
        v-bind="{ stripe: true, size: 'small', border: true, fit: true, highlightCurrentRow: true }"
      >
        <el-table-column align="center" width="100" label="桌号ID" prop="id" fixed="left" />
        <el-table-column align="center" width="150" label="桌号名称" prop="name" fixed="left" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="商家ID" prop="aid" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="区域ID" prop="areaId" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="最大承载人数" prop="num" show-overflow-tooltip />
        <el-table-column align="center" label="餐桌状态" prop="status">
          <template slot-scope="{ row }">
            <el-tag v-if="row.status === 1" effect="plain" type="info">就餐中</el-tag>
            <el-tag v-else-if="row.status === 2" effect="plain" type="success">空闲</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="100" label="排序编号" prop="sort" show-overflow-tooltip />
        <el-table-column align="center" label="是否禁用" prop="isStop">
          <template slot-scope="{ row }">
            <el-tag v-if="row.isStop === 0" effect="plain" type="info">否</el-tag>
            <el-tag v-else-if="row.isStop === 1" effect="plain" type="success">是</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="是否删除" prop="isDelete">
          <template slot-scope="{ row }">
            <el-tag v-if="row.isDelete === 0" effect="plain" type="info">否</el-tag>
            <el-tag v-else-if="row.isDelete === 1" effect="plain" type="success">是</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" width="150" label="创建时间" prop="createTime" />
        <el-table-column align="center" label="操作" width="300" fixed="right" class-name="small-padding fixed-width">
          <template slot-scope="{ row }">
            <el-button type="warning" size="mini" @click="handleDetail(row)">
              详情
            </el-button>
            <el-button size="mini" @click="handleEdit(row)">
              编辑
            </el-button>
            <el-button
              type="info" size="mini"
              @click="$refs.MealCodeCreate && $refs.MealCodeCreate.handleOpen(row)"
            >
              创建桌码
            </el-button>
            <el-button
              type="primary" size="mini"
              @click="$refs.MealOrderCreate && $refs.MealOrderCreate.handleOpen(row)"
            >
              创建订单
            </el-button>
            <el-button
              type="warning" size="mini"
              @click="handleCheckout(row)"
            >
              结账
            </el-button>
            <el-button type="danger" size="mini" @click="handleDelete(row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <div>
      <el-pagination
        :current-page="listQuery.page" :page-sizes="[10, 20, 50, 100]" :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="(val) => ((listQuery.pageSize = val) && getList())"
        @current-change="(val) => ((listQuery.page = val) && getList())"
      />
    </div>

    <!-- 创建桌号二维码 -->
    <MealCodeCreate ref="MealCodeCreate" @success="getList" />
    <!-- 创建点餐订单 -->
    <MealOrderCreate ref="MealOrderCreate" @success="getList" />
    <!-- 新增编辑 -->
    <EditModal ref="EditModal" @success="getList" />
    <!-- 查看详情 -->
    <DetailModal ref="DetailModal" />
  </div>
</template>

<script>
import MealCodeCreate from './components/MealCodeCreate'
import MealOrderCreate from './components/MealOrderCreate'
import EditModal from './components/EditModal'
import DetailModal from './components/DetailModal'
import { getPageAllShopAuthDept, deletePostShopAuthDept, checkoutShopLaOrders } from '@/api/mealManagement/TableNumberSetting'

export default {
  name: 'TableNumberSetting',
  components: {
    MealCodeCreate,
    MealOrderCreate,
    EditModal,
    DetailModal
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        pageSize: 20,
        name: '',
        isStop: ''
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await getPageAllShopAuthDept(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDetail(row) {
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    },
    handleEdit(row) {
      this.$refs.EditModal && this.$refs.EditModal.handleOpen(row)
    },
    handleDelete(row) {
      this.$confirm('确定删除此项？')
        .then(async () => {
          await deletePostShopAuthDept({ id: row.id })
          this.$message({ message: '删除成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    },
    handleCheckout(row) {
      this.$confirm('确定结账此项？')
        .then(async () => {
          await checkoutShopLaOrders({ did: row.id })
          this.$message({ message: '结账成功！', type: 'success' })
          this.handleSearch()
        })
        .catch(() => {})
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
	padding: 20px;
	display: flex;
	flex-direction: column;

	.filter-container {
		.filter-item {
			display: inline-block;
			vertical-align: middle;
			margin-bottom: 10px;
		}
	}

	.small-padding {
		.cell {
			padding-left: 5px;
			padding-right: 5px;
		}
	}

	.fixed-width {
		.el-button--mini {
			padding: 7px 10px;
		}
	}
}
</style>
