<template>
  <div class="padding-20">
    <div class="search-box">
      <el-form ref="form"
               :inline="true"
               :model="searchForm"
               clearable
               class="mt-10">
        <el-form-item label="座位名称:">
          <el-input v-model="searchForm.place_name" placeholder="请输入座位名称" clearable></el-input>
        </el-form-item>
        <el-form-item class="ml-10">
          <el-button icon="el-icon-search" type="primary" @click="searchSubmit">查询</el-button>
        </el-form-item>
        <el-form-item class="ml-10">
          <el-button icon="el-icon-plus" type="primary" v-permission="'business_place_list_add'" @click="add">添加座位
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData"
              stripe
              v-loading="tableLoading"
              fit
              class="marginT-10 order-table"
              border
              :max-height="vheight">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="座位名称" prop="place_name" width="240"></el-table-column>
      <el-table-column label="座位说明" prop="place_desc" width="240"></el-table-column>
      <el-table-column label="座位类型">
        <template slot-scope="scope">
          <p>{{ ['正常', '临时'][scope.row.place_type] }}</p>
        </template>
      </el-table-column>

      <el-table-column label="预定方式">
        <template slot-scope="scope">
          <p>{{ ['整桌预定', '座位预定'][scope.row.place_pre_type] }}</p>
        </template>
      </el-table-column>


      <el-table-column label="座位数量" prop="place_num"></el-table-column>
      <el-table-column label="单桌人数" prop="place_user_max"></el-table-column>
      <el-table-column label="单价(元)">
        <template slot-scope="scope">
          <p>{{ scope.row.place_price | fen2Yuan }}</p>
        </template>
      </el-table-column>
      <el-table-column label="最晚到店时间" prop="place_latest_time"></el-table-column>
      <el-table-column label="最晚保留时间(分钟)" prop="place_max_time" width="160"></el-table-column>
      <el-table-column label="添加时间" prop="created_at" width="160"></el-table-column>
      <el-table-column label="操作" width="220" fixed="right">
        <template slot-scope="scope">
          <el-button type="text"
                     v-permission="'business_place_list_edit'"
                     @click="edit(scope.row)">编辑
          </el-button>
          <el-button type="text"
                     :disabled="booLock"
                     v-permission="'business_place_list_download'"
                     @click="download(scope.row)">下载点单二维码
          </el-button>
          <el-button type="text"
                     v-permission="'business_place_list_del'"
                     @click="del(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="marginT-20"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :hide-on-single-page="true"
      :current-page="page"
      :page-size="page_size"
      :page-sizes="[10, 20, 100, 200, 300, 400]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"/>
    <detail v-if="detailsDialog.show"
            v-model="detailsDialog.show"
            :exData="detailsDialog.exData"
            @success="init"></detail>
  </div>
</template>

<script>
import page from '@/mixin/page'
import detail from './details'
import axios from 'axios'

export default {
  mixins: [page],
  components: {
    detail,
  },
  data () {
    return {
      detailsDialog: {
        show: false,
        exData: {}
      },
      searchForm: {},
      tableData: [],
      tableUrl: '/v1/bar/place/template/list',
      booLock: false
    }
  },
  methods: {
    add () {
      this.detailsDialog.exData = {}
      this.detailsDialog.show = true
    },
    edit (row) {
      this.detailsDialog.exData = row
      this.detailsDialog.show = true
    },
    del (row) {
      this.$confirm('确定要删除吗', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const data = await this.$fetch('/v1/bar/place/template/delete', { id: row.id }, 'get')
        if (data.code === 200) {
          this.$message.success('删除成功')
          this.init()
        }
      }).catch(() => {})
    },
    download (row) {
      this.booLock = true
      axios({
        method: 'GET',
        url: '/v1/bar/place/template/download',
        params: {
          id: row.id
        },
        responseType: 'blob',
        headers: { 'Content-Type': 'application/json; application/octet-stream' }
      }).then(response => {
        this.booLock = false
        if (response.data.type === 'application/octet-stream') {
          /* 兼容ie内核，360浏览器的兼容模式 */
          if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            const blob = new Blob([response.data], { type: 'application/zip' })
            window.navigator.msSaveOrOpenBlob(blob)
          } else {
            /* 火狐谷歌的文件下载方式 */
            const blob = new Blob([response.data], { type: 'application/zip' })
            const url = window.URL.createObjectURL(blob)
            const aTag = document.createElement('a')
            aTag.href = url
            aTag.download = ''
            aTag.click()
            URL.revokeObjectURL(url) // 释放内存
          }
        } else {
          this.$message.error('下载出错，请联系管理员')
        }
      }).catch(error => {
        this.booLock = false
        this.$message.error(JSON.stringify(error))
      })
    }
  },
  mounted () {
    this.init()
  },
}
</script>

<style lang="scss" scoped>
.el-dropdown {
  margin: 0 10px;

  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
}
</style>
