<template>
  <div>

    <div>
      <div ref="lxHeader">
        <lx-header icon="md-apps" :text="$route.meta.title" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content">
            <div class="searchwrap" style="display: flex;align-items: center;">
              <div>
                <el-date-picker @change="changeDate" v-model="select.date" type="date" placeholder="预约日期"
                  value-format="yyyy-MM-dd" format="yyyy-MM-dd">
                </el-date-picker>
              </div>
              <div>
                <el-input v-model="select.user_name" placeholder="请输入预约人"></el-input>
              </div>
              <div>
                <el-select v-model="select.site" placeholder="请选择预约场地" clearable>
                  <el-option v-for="item in site_options" :key="item.id" :label="item.name" :value="item.id">
                  </el-option>
                </el-select>
              </div>
              <div>
                <el-button type="primary" size="small" @click="getList">查询</el-button>
              </div>
              <div>
                <el-button type="primary" size="small" @click="editBook('add')">手动预约</el-button>
              </div>
              <div>
                <el-button type="primary" size="small" @click="editBook('more')">批量预约</el-button>
              </div>
            </div>
          </div>

        </lx-header>
      </div>
    </div>

    <div>
      <xy-table :list="list" :total="total" @pageIndexChange="pageIndexChange" @pageSizeChange="pageSizeChange"
        :table-item="table_item">
        <template v-slot:status>
          <el-table-column align='center' label="状态" width="120" header-align="center">
            <template slot-scope="scope">
              <div v-for="item in book_status">
                <el-tag :type="item.type" v-if="scope.row.status===item.id">{{item.value}}</el-tag>
              </div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:timerange>
          <el-table-column align='center' label="预约时段" width="120" header-align="center">
            <template slot-scope="scope">
              {{formateHH(scope.row.start_time)}}-{{formateHH(scope.row.end_time)}}
            </template>
          </el-table-column>
        </template>

        <template v-slot:btns>
          <el-table-column align='center' label="操作" width="220" header-align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="small" v-if="scope.row.status>0"
               @click="checkBook('show',scope.row.id)">查看</el-button>
              <el-button type="primary" size="small" v-if="scope.row.status===0"
                @click="checkBook('check',scope.row.id)">审核</el-button>
              <el-button type="primary" size="small" v-if="scope.row.status===0"
                @click="editBook('editor',scope.row.id)">编辑</el-button>
              <el-popconfirm style="margin:0 10px" @confirm="deleteList(scope.row.id)" title="确定删除吗？">
                <el-button type="danger" size="small" slot="reference">删除</el-button>
              </el-popconfirm>

            </template>
          </el-table-column>
        </template>
      </xy-table>
    </div>

    <check-book ref="checkBook" @refresh="getList"></check-book>
    <addBook ref="addBook" @refresh="getList"></addBook>
  </div>
</template>

<script>
  import myMixins from "@/mixin/selectMixin.js";
  import checkBook from './components/checkBook.vue';
  import addBook from './components/addBook.vue';
  import {
    index
  } from "@/api/book/index.js"
  import {
    index as getAppointment
  } from "@/api/book/appointment.js"
  export default {
    mixins: [myMixins],
    components: {
      checkBook,
      addBook
    },
    data() {
      return {
        select: {
          user_name: '',
          date: '',
          site: '',
          page: 1,
          page_size: 10,
        },
        site_options: [],
        list: [],
        total: 0,
        table_item: [{
          prop: 'date',
          label: '预约日期',
          align: 'left',
          width: 120,
        }, {
          prop: 'timerange',
          label: '预约时段',
          align: 'center',
          width: 120,
        }, {
          prop: 'content',
          label: '预约事项',
          align: 'center',
          width: 120,
        }, {
          prop: 'appointment_config.name',
          label: '预约场地',
          align: 'center',
          width: 120,
        }, {
          prop: 'plate',
          label: '车牌',
          align: 'center',
          // width: 180,
        }, {
          prop: 'accompany_total',
          label: '同行人数',
          align: 'center',
          width: 120,
        }, {
          prop: 'user.username',
          label: '预约人',
          align: 'center',
          width: 120,
        }, {
          prop: 'status',
          label: '状态',
          align: 'center',
          width: 120,
        }]

      }
    },
    created() {
      this.getList()
      this.getSites()
    },
    methods: {
      pageIndexChange(e) {
        this.select.page = e
        this.getList()
      },
      pageSizeChange(e) {
        this.select.page_size = e
        this.select.page = 1
        this.getList()
      },
      async getList() {
        const res = await index({
          page: this.select.page,
          page_size: this.select.page_size,
          user_name: this.select.user_name,
          show_relation: ['user', 'appointmentConfig'],
          filter: [{
            key: 'date',
            op: 'eq',
            value: this.select.date ? this.select.date : ''
          }, {
            key: 'site',
            op: 'eq',
            value: this.select.site
          }]
        })
        this.list = res.data
        this.total = res.total
      },
      async getSites() {
        const res = await getAppointment({
          page: 1,
          page_size: 999,
          filter: [{
            key: 'status',
            op: 'eq',
            value: 1
          }]
        })
        this.site_options = res.data
      },
      changeDate(e) {
        console.log(e)
        if (e) {
          this.select.date = e
        } else {
          this.select.date = ''
        }
        console.log("this.select.date", this.select.date)
      },
      formateHH(val) {
        if (val) {
          return this.$moment(val).format("HH:mm")
        } else {
          return ''
        }
      },
      checkBook(type, id) {
        this.$refs.checkBook.id = id
        this.$refs.checkBook.type = type
        this.$refs.checkBook.isShow = true
      },
      editBook(type, id) {
        if (id) {
          this.$refs.addBook.id = id
        }
        this.$refs.addBook.type = type
        this.$refs.addBook.site_options = this.site_options
        this.$refs.addBook.isShow = true
      },
    }
  }
</script>

<style lang="scss" scoped>
  .searchwrap {
    display: flex;
    align-items: center;

    &>div {
      display: flex;
      align-items: center;
      margin-right: 10px;

      span {
        min-width: 70px;
      }
    }
  }
</style>
