<!--  -->
<template>
  <div class="app-container">
    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-radio-group v-model="listQuery.orderType" class="filter-item" size="mini" @input="handleSetOrderType">
        <el-radio-button :label="1">商城</el-radio-button>
        <el-radio-button :label="2">商圈</el-radio-button>
      </el-radio-group>
      <el-radio-group
        v-if="listQuery.orderType === 1" v-model="listQuery.state" class="filter-item"
        style="margin-left: 10px;" size="mini" @input="handleSearch"
      >
        <el-radio-button label="">全部</el-radio-button>
        <el-radio-button :label="1">待付款</el-radio-button>
        <el-radio-button :label="2">待发货</el-radio-button>
        <el-radio-button :label="3">已发货</el-radio-button>
        <el-radio-button :label="4">已完成</el-radio-button>
        <el-radio-button :label="5">已关闭</el-radio-button>
      </el-radio-group>
      <el-radio-group
        v-else-if="listQuery.orderType === 2" v-model="listQuery.state" class="filter-item"
        style="margin-left: 10px;" size="mini" @input="handleSearch"
      >
        <el-radio-button label="">全部</el-radio-button>
        <el-radio-button :label="5">已取消</el-radio-button>
        <el-radio-button :label="8">待核销</el-radio-button>
        <el-radio-button :label="9">待核销已付款</el-radio-button>
        <el-radio-button :label="10">已核销</el-radio-button>
      </el-radio-group>
      <el-input
        v-model="listQuery.search" clearable size="mini" class="filter-item"
        style="display: inline-table;width: 400px;margin-left: 10px;"
        placeholder="请输入内容"
      >
        <template #prepend>
          <el-select v-model="listQuery.searchType" size="mini" style="width:150px" placeholder="请选择">
            <el-option label="订单id" value="1" />
            <el-option label="买家账户" value="2" />
            <el-option label="收件人姓名" value="3" />
            <el-option label="收件人手机号" value="4" />
            <!-- <el-option label="商品ID" value="5" /> -->
            <el-option label="下单号码" value="6" />
            <el-option label="订单编号" value="7" />
            <el-option label="支付单号" value="8" />
          </el-select>
        </template>
      </el-input>
      <el-input
        v-model="listQuery.shopName" maxlength="20" clearable size="mini"
        class="filter-item" style="width: 200px;margin-left: 10px;" placeholder="请输入商户名称"
      />
      <!-- <el-select
        v-model="listQuery.afterState" clearable size="mini" class="filter-item"
        style="width: 200px;margin-left: 10px;" placeholder="请选择售后状态"
        >
        <el-option label="全部" :value="null" />
        <el-option label="无售后" value="0" />
        <el-option label="售后中" value="1" />
        <el-option label="售后成功" value="2" />
        <el-option label="售后关闭" value="3" />
        </el-select> -->
      <el-date-picker
        v-model="listQuery.dates" type="daterange" range-separator="至"
        start-placeholder="开始时间" end-placeholder="结束时间" size="mini"
        class="filter-item" style="margin-left: 10px;line-height: 1;"
      />
      <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="exportExcelVisible = true">
        导出订单
      </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 }"
        @selection-change="(e) => selectOrderIds = e"
      >
        <el-table-column align="center" width="55" type="selection">
        </el-table-column>
        <el-table-column align="center" width="120" label="订单ID" prop="orderId" fixed="left">
          <template slot-scope="{ row }">
            {{ row.orderId }}
          </template>
        </el-table-column>
        <el-table-column align="center" width="220" label="订单编号" prop="orderFormid" fixed="left" />
        <el-table-column align="center" label="下单时间" prop="createTime" show-overflow-tooltip />
        <el-table-column align="center" width="180" label="下单号码" prop="customerPhone" />
        <el-table-column align="center" label="下单账户" prop="customerName" show-overflow-tooltip />
        <el-table-column align="center" width="220" label="商家名称" prop="shopName" show-overflow-tooltip />
        <el-table-column align="center" width="220" label="支付金额（元）" prop="price" />
        <el-table-column align="center" width="120" label="商家赠送代金券" prop="shopVoucher" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="平台赠送代金券" prop="platformVoucher" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="总赠送代金券" prop="presenterVoucher" show-overflow-tooltip />
        <el-table-column align="center" width="120" label="代金券抵扣金额" prop="voucherPrice" show-overflow-tooltip />
        <el-table-column align="center" min-width="120" label="订单状态" prop="state" show-overflow-tooltip>
          <template slot-scope="{ row }">
            <span v-if="row.state == 1">待付款</span>
            <span v-else-if="row.state == 2">待发货</span>
            <span v-else-if="row.state == 3">待收货</span>
            <span v-else-if="row.state == 4">已完成</span>
            <span v-else-if="row.state == 5">交易关闭（已取消）</span>
            <span v-else-if="row.state == 6">待成团</span>
            <span v-else-if="row.state == 7">待售后</span>
            <span v-else-if="row.state == 8">待核销（未付款）</span>
            <span v-else-if="row.state == 9">待核销（已付款）</span>
            <span v-else-if="row.state == 10">已核销</span>
            <span v-else>--</span>
          </template>
        </el-table-column>

        <el-table-column align="center" width="100" label="商品数量" prop="number" />
        <el-table-column align="center" label="订单类型" prop="scanType" show-overflow-tooltip>
          <template slot-scope="{ row }">
            <el-tag v-if="row.scanType === 1" effect="plain">常规订单</el-tag>
            <el-tag v-else-if="row.scanType === 2" effect="plain">收款码订单</el-tag>
            <span v-else>--</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="收件人" prop="receiveName" show-overflow-tooltip />
        <el-table-column align="center" label="手机号" prop="receivePhone" show-overflow-tooltip />
        <el-table-column align="center" label="操作" width="180" 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
              v-if="row.state === 1 || row.state === 8" type="danger" size="mini"
              @click="$refs.PaymentStatusUpdate && $refs.PaymentStatusUpdate.handleOpen(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>

    <el-dialog title="订单详情" :visible.sync="detailVisible" width="74%" center :close-on-click-modal="false">
      <OrderDetail :detail-row="form" @cancel="detailVisible = false" />
    </el-dialog>
    <!-- 同步支付状态 -->
    <PaymentStatusUpdate ref="PaymentStatusUpdate" @success="getList" />

    <el-dialog title="选择导出模版" :visible.sync="exportExcelVisible" center :close-on-click-modal="false">
      <el-radio-group v-model="exportTemplate">
        <el-radio :label="1">平台订单</el-radio>
        <el-radio :label="2">胖子发货订单</el-radio>
      </el-radio-group>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="exportExcelVisible = false">取 消</el-button>
          <el-button type="primary" :loading="exportExcelLoading" @click="handleDataExport">
            {{ exportExcelLoading
              ? '导出中...' : '确认导出' }}
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import OrderDetail from './components/pendDetails'
import PaymentStatusUpdate from './components/PaymentStatusUpdate'
import { orderGetAll, orderExport } from '@/api/order'
export default {
  name: 'Pending',
  components: {
    OrderDetail,
    PaymentStatusUpdate
  },
  data() {
    return {
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        searchType: '1',
        search: '', // 搜索字段
        state: '',
        // afterState: '', // 售后状态 0-无售后 1-售后中 2-售后成功 3-售后关闭
        dates: [], // 下单时间数组
        page: 1,
        shopName: '',
        pageSize: 20,
        templateType: 1,
        orderType: 1
      },
      detailVisible: false,
      exportExcelVisible: false,
      form: {},
      exportTemplate: 1, // excel导出模版
      exportExcelLoading: false,
      selectOrderIds: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      try {
        const res = await orderGetAll(this.listQuery)
        this.list = res.data.list
        this.total = res.data.total
      } finally {
        this.listLoading = false
      }
    },
    handleSetOrderType(e) {
      this.listQuery.orderType = e
      this.listQuery.state = ''
      this.handleSearch()
    },
    handleSearch() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDetail(row) {
      this.detailVisible = true
      this.form = row
      this.$refs.DetailModal && this.$refs.DetailModal.handleOpen(row)
    },

    // 导出订单
    async handleDataExport() {
      this.$message({
        message: '数据导出中，请勿重复操作！',
        type: 'success'
      })
      try {
        this.exportExcelLoading = true
        const exportExcelParams = { ...this.listQuery, templateType: this.exportTemplate }
        !!this.selectOrderIds.length && (exportExcelParams.orderIds = this.selectOrderIds.map((item) => item.orderId))
        const res = await orderExport(exportExcelParams)
        if (!res) {
          return
        }
        const blob = new Blob([ res ], { type: 'application/vnd.ms-excel' })
        const fileName = '订单数据明细表.xls'
        if ('download' in document.createElement('a')) {
          // 非IE下载
          const elink = document.createElement('a')
          elink.download = fileName
          elink.style.display = 'none'
          elink.href = URL.createObjectURL(blob)
          document.body.appendChild(elink)
          elink.click()
          URL.revokeObjectURL(elink.href) // 释放URL 对象
          document.body.removeChild(elink)
        } else {
          // IE10+下载
          navigator.msSaveBlob(blob, fileName)
        }
        this.$message({
          message: '导出成功',
          type: 'success'
        })
        this.selectOrderIds = []
        this.$refs.multipleTable.clearSelection()
      } catch (error) {
        this.$message({
          message: '导出失败',
          type: 'error'
        })
      } finally {
        this.exportExcelLoading = false
        this.exportExcelVisible = false
      }
    }
  }
}
</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>
