<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <div class="cell">
          <div class="cell-title">
            <div class="title-left">
              <div class="title">订单管理</div>
            </div>
          </div>
          <div class="cell-body">
            <div class="controls">
              <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="">
                  <el-input v-model="formInline.order_name_phone" prefix-icon="el-icon-search" clearable placeholder="请输入关键字"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" class="btn-search" @click="btn_search">查询</el-button>
                </el-form-item>
                <el-form-item label="支付时间：">
                  <el-date-picker v-model="search_datatime" type="datetimerange" align="right" start-placeholder="开始日期" end-placeholder="结束日期"
                    @change="search_datetime_change" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss" format="yyyy-MM-dd HH:mm:ss"
                    clearable>
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="">
                  <el-select v-model="formInline.status" placeholder="支付状态" @change="search_state_change">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="待支付" value="1"></el-option>
                    <el-option label="已支付" value="2"></el-option>
                    <el-option label="已入住" value="3"></el-option>
                    <el-option label="已取消" value="4"></el-option>
                    <el-option label="已退款" value="5"></el-option>
                    <el-option label="已退房" value="6"></el-option>
                  </el-select>
                </el-form-item>
              </el-form>
              <el-button type="primary" class="export" @click="handler_download_excel">导出订单</el-button>
            </div>
            <el-table :data="tableData" height="510" style="width: 100%" :cell-style="cell_style" v-loading="loading" :header-cell-style="header_cell_style">
              <el-table-column label="订单号" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.order_num }}</span>
                </template>
              </el-table-column>
              <el-table-column label="状态" align="center">
                <template slot-scope="scope">
                  <span v-if="scope.row.status === '1'" class="txt-cell-red">待支付</span>
                  <span v-else-if="scope.row.status === '2'" class="txt-cell-green">已支付</span>
                  <span v-else-if="scope.row.status === '3'" class="txt-cell-green">已入住</span>
                  <span v-else-if="scope.row.status === '4'" class="txt-cell-red">已取消</span>
                  <span v-else-if="scope.row.status === '5'" class="txt-cell-red">已退款</span>
                  <span v-else-if="scope.row.status === '6'" class="txt-cell-red">已退房</span>
                  <span v-else class="txt-cell-red">未知</span>
                </template>
              </el-table-column>
              <el-table-column label="预订人" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.user_name }}</span>
                </template>
              </el-table-column>
              <el-table-column label="手机号" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.phone }}</span>
                </template>
              </el-table-column>
              <el-table-column label="预定房型" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.h_type }}</span>
                </template>
              </el-table-column>
              <el-table-column label="房费（元）" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.price.toFixed(2) }}</span>
                </template>
              </el-table-column>
              <el-table-column label="支付时间" align="center">
                <template slot-scope="scope">
                  <span>{{ scope.row.create_time }}</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" width="300">
                <template slot-scope="scope">
                  <el-button size="mini" round type="primary" @click="collate_handler(scope.$index, scope.row)">核销</el-button>
                  <el-button size="mini" round type="danger" @click="refund_handler(scope.$index, scope.row)">退款</el-button>
                  <el-button size="mini" round type="warning" @click="checkout_handler(scope.$index, scope.row)">退房</el-button>
                </template>
              </el-table-column>
            </el-table>
            <div class="pagination-table">
              <el-pagination @current-change="currentPageChange" :current-page.sync="pagination.currentPage" :page-size="pagination.pageSize"
                layout="prev, pager, next, jumper" :total="pagination.total">
              </el-pagination>
            </div>
          </div>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import {
    getTableData,
    collate,
    refund,
    checkout,
    downloadExcel
  } from '@/api/order'
  export default {
    data() {
      return {
        // 表格单元格样式
        cell_style: {
          color: '#1A202B',
          'font-size': '14px',
          'font-family': 'Microsoft YaHei-3970(82674968)'
        },
        // 表格头部样式
        header_cell_style: {
          background: '#E6ECFE',
          color: '#1A202B',
          'font-size': '16px',
          'font-family': 'Microsoft YaHei-3970(82674968)'
        },
        search_datatime: '',
        // 表单数据
        formInline: {
          order_name_phone: '',
          start_time: '',
          end_time: '',
          status: ''
        },
        tableData: [],
        cond_data: {},
        // 分页参数
        pagination: {
          currentPage: 1,
          pageSize: 9,
          total: 0
        },
        loading: true
      }
    },
    created() {
      // 获取消费记录,表格数据
      this.get_table_data('list')
    },
    mounted() {
      document.getElementsByClassName("el-pagination__jump")[0].childNodes[0].nodeValue = "跳至";
    },
    methods: {
      /**
       * 核销
       */
      collate_handler(index, row) {
        // console.log(index, row, row.status);
        this.$confirm('确定核销？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          closeOnClickModal: false
        }).then(() => {
          let data = {
            id: row.id,
            status: 3,
            dom: row.dom
          };
          // console.log(data);
          collate(data).then((res) => {
            // console.log(res);
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: res.message
              });
              // 刷新列表
              this.get_table_data('pages');
            } else {
              this.$message.warning(res.message)
            }
          }).catch((err) => {
            // console.log(err);
            this.$message.error(err.message)
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消核销'
          });
        });
      },
      /**
       * 退款
       */
      refund_handler(index, row, param) {
        // console.log(index, row, row.status);
        this.$confirm('确定退款？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          closeOnClickModal: false
        }).then(() => {
          let data = {
            id: row.id,
            status: 5,
            dom: row.dom
          };
          // console.log(data);
          refund(data).then((res) => {
            // console.log(res);
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: res.message
              });
              // 刷新列表
              this.get_table_data('pages');
            } else {
              this.$message.warning(res.message)
            }
          }).catch((err) => {
            // console.log(err);
            this.$message.error(err.message)
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退款'
          });
        });
      },
      /**
       * 退房
       */
      checkout_handler(index, row) {
        // console.log(index, row, row.status);
        this.$confirm('确定退房？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          closeOnClickModal: false
        }).then(() => {
          let data = {
            id: row.id,
            status: 6,
            dom: row.dom
          };
          // console.log(data);
          checkout(data).then((res) => {
            // console.log(res);
            if (res.code === 200) {
              this.$message({
                type: 'success',
                message: res.message
              });
              // 刷新列表
              this.get_table_data('pages');
            } else {
              this.$message.warning(res.message)
            }
          }).catch((err) => {
            // console.log(err);
            this.$message.error(err.message)
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退房'
          });
        });
      },
      /**
       * 下载Excel表格
       */
      handler_download_excel() {
        this.get_condtion();
        // 开始发送请求，获取配置数据
        downloadExcel(this.cond_data).then((res) => {
          // console.log(res);
          if (typeof res.code == 'undefined' || res.code == '') {
            this.$message.error('返回数据格式问题，code未获取到！')
            return
          }

          if (res.code == 200) {
            this.$message.success('下载成功！')
            let xls = 'https://chtech.ncjti.edu.cn/hotelReservation' + res.downurl
            window.open(xls)
          } else {
            this.$message.error(res.message)
          }
        }).catch((err) => {
          // console.log(err);
          this.$message.error(err.message)
        })
      },
      /**
       * 选取的时间进行格式化，赋值给formInline
       */
      search_datetime_change() {
        // console.log(this.search_datatime);
        if (this.search_datatime == null) {
          this.formInline.start_time = ''
          this.formInline.end_time = ''
        } else {
          this.formInline.start_time = this.search_datatime[0]
          this.formInline.end_time = this.search_datatime[1]
        }
        // 刷新列表
        this.get_table_data('search')
      },
      /**
       * 搜索状态
       */
      search_state_change() {
        // 刷新列表
        this.get_table_data('search')
      },
      /**
       * 获取消费记录,表格数据
       */
      get_table_data(param) {
        this.loading = true
        this.get_condtion();

        let data = this.cond_data;
        if (param == 'search') {
          data.page = 1
          this.pagination.currentPage = 1
        }
        // console.log(data);
        getTableData(data).then((res) => {
          // console.log(res);
          if (typeof res.rows !== 'undefined' && res.rows !== '') {
            this.pagination.total = res.total
            this.tableData = []
            for (var i = 0; i < res.rows.length; i++) {
              this.tableData.push(res.rows[i])
            }
          } else {
            this.tableData = []
            this.$message.warning('没有符合条件的数据！')
          }
        }).catch((err) => {
          // console.log(err);
          this.$message.error(err.message)
        });
        this.loading = false;
      },
      /**
       * 条件
       */
      get_condtion() {
        let data = {
          page: this.pagination.currentPage,
          rows: this.pagination.pageSize
        }
        if (this.formInline.order_name_phone !== '') {
          data.order_name_phone = this.formInline.order_name_phone
        }
        if (this.formInline.start_time !== '') {
          data.start_time = this.formInline.start_time
        }
        if (this.formInline.end_time !== '') {
          data.end_time = this.formInline.end_time
        }
        if (this.formInline.status !== '') {
          data.status = this.formInline.status
        }

        this.cond_data = data;
      },
      /**
       * 搜索
       */
      btn_search() {
        this.get_table_data('search')
      },
      /**
       * currentPage 改变时会触发
       * @param {Object} val
       */
      currentPageChange(val) {
        this.pagination.currentPage = val
        this.get_table_data('pages')
      }
    }
  }
</script>

<style lang="scss" scoped>
  .app-container {
    background-color: #EFF2F7;
    padding: 10px;

    .el-row {

      .el-col {
        padding: 10px;

        .cell {
          padding: 30px;
          border-radius: 10px;
          background-color: #FFFFFF;
          // box-shadow: 5px 5px 15px #979797;
          box-shadow: 0px 3px 21px 0px rgba(60, 108, 254, 0.16);

          .cell-title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 30px;
            padding-bottom: 30px;
            border-bottom: 1px solid #CCCCCC;

            .title-left {
              display: flex;
              align-items: center;

              .title {
                font-size: 22px;
                font-family: Microsoft YaHei-3970(82674968);
                font-weight: bold;
                color: #1A202B;
              }

              .title-detail {
                margin-left: 50px;
                font-size: 18px;
                font-family: Microsoft YaHei-3970(82674968);
                color: #1A202B;
              }
            }

            .title-right {
              display: flex;
              justify-content: space-between;
              align-items: center;

              .el-button {
                width: 110px;
                height: 46px;
                background: #2B4CFE;
                font-size: 18px;
                color: #FFFFFF;
                font-family: Microsoft YaHei-3970(82674968);
                border-radius: 5px;
              }

              .el-button--warning {
                background: #F88A64;
              }
            }
          }

          .cell-body {

            .controls {
              display: flex;
              justify-content: space-between;
              align-items: center;

              .demo-form-inline {
                display: inline-flex;
                flex-wrap: wrap;
                align-items: center;
                justify-content: flex-start;

                .btn-search {
                  background: #3762FE;
                  border-color: #3762FE;
                  font-size: 14px;
                  color: #FFFFFF;
                  font-family: Microsoft YaHei-3970(82674968);
                  border-radius: 5px;
                }

                .el-date-editor--datetimerange {
                  padding: 3px 8px;
                  width: 370px;
                }

                .el-select {
                  width: 130px;
                }
              }

              .export {
                background: #3762FE;
                border-color: #3762FE;
                font-size: 14px;
                color: #FFFFFF;
                font-family: Microsoft YaHei-3970(82674968);
                border-radius: 5px;
                margin-bottom: 22px;
              }
            }

            .btn-cell-blue {
              color: #2B4CFE;
              border: none;
              font-size: 18px;
            }

            .btn-cell-black {
              color: #53575A;
              border: none;
              font-size: 18px;
            }

            .txt-cell-green {
              color: #67C23A;
            }

            .txt-cell-red {
              color: #F56C6C;
            }

            .pagination-table {
              display: flex;
              justify-content: flex-end;
              align-items: center;
              height: 30px;
              margin-top: 20px;
              padding-right: 100px;

              ::v-deep .el-pagination {
                display: flex;
                justify-content: flex-end;
                align-items: center;
              }

              ::v-deep .el-pagination ul {
                display: flex;

              }

              ::v-deep .el-pagination button,
              ::v-deep .el-pagination li {
                display: flex;
                justify-content: center;
                align-items: center;
                width: 50px;
                height: 36px;
                border: 1px solid #626262;
                border-radius: 3px;
                font-size: 14px;
                margin: 0 5px;
              }

              ::v-deep .el-pagination span {
                margin-left: 10px;
              }

              ::v-deep .el-pagination .el-pagination__jump {
                font-size: 16px;
              }

              ::v-deep .el-pagination .el-pagination__editor {
                width: 50px;
                height: 36px;
                margin: 0 5px;
              }

              ::v-deep .el-pagination .el-input__inner {
                width: 50px;
                height: 36px;
                border: 1px solid #626262;
                margin: -4px 0 0 0;
              }
            }
          }
        }
      }
    }

    ::v-deep .el-dialog {
      margin: 0 !important;
      width: 400px;
      height: 320px;
      background: #FFFFFF;
      box-shadow: 0px 0px 13px 0px rgba(0, 0, 0, 0.29);
      border-radius: 10px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);

      .el-dialog__header {
        display: flex;
        align-items: center;
        width: 100%;
        height: 58px;
        padding: 30px;
        background: #E6EBFE;
        border-radius: 10px 10px 0px 0px;
        font-weight: bold;
      }

      .el-dialog__body {
        padding-bottom: 0;

        .el-form-item {
          margin-bottom: 0;

          .el-form-item__label,
          .el-form-item__content {
            font-size: 16px;
            font-family: Microsoft YaHei-3970(82674968);
            color: #53575A;
          }

          .el-input__inner {
            width: 100px;
          }
        }
      }
    }
  }
</style>
