<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <!-- <el-form-item label="所属商家" prop="businessId">
        <el-select v-model="queryParams.businessId" filterable placeholder="请选择所属商家" size="small" clearable style="width: 100%">
          <el-option v-for="(v,i) in shopList" :key="i" :label="v.businessName" :value="v.businessId" />
        </el-select>
      </el-form-item> -->
      <el-form-item label="订单号" prop="orderNumber">
        <el-input v-model="queryParams.orderNumber" placeholder="请输入订单号" maxlength="30" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="下单手机号" prop="userPhone">
        <el-input v-model="queryParams.userPhone" placeholder="请输入下单手机号" maxlength="11" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商品名称" prop="businessProductName">
        <el-input v-model="queryParams.businessProductName" placeholder="请输入商品名称" maxlength="30" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="下单时间" prop="">
        <el-date-picker v-model="daterangeCreateTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item label="券码核销码" prop="couponCode">
        <el-input v-model="queryParams.couponCode" placeholder="请输入券码核销码" maxlength="60" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-radio-group size="mini" v-model="queryParams.queryStatus" style="margin-bottom: 30px;" @input="handleQuery">
          <el-radio-button v-for="(v,i) in queryStatusList" :key="i" :label="v.value">{{v.label}}</el-radio-button>
        </el-radio-group>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain size="mini" @click="handleExport">导出订单</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="index" width="55" label="序号" align="center">
        <template slot-scope="scope">
          <div>
            {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
          </div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="商家ID" align="center" prop="businessId" /> -->
      <el-table-column label="订单编号" align="center" prop="orderNumber" />
      <el-table-column label="商品名称" align="center" prop="businessProductName" />
      <el-table-column label="销售价格" align="center" prop="sellingPrice" />
      <el-table-column label="购买数" align="center" prop="buyQuantity" />
      <el-table-column label="可用数" align="center" prop="waitSum">
        <template slot-scope="{row}">
          {{row.waitSum||"0"}}
        </template>
      </el-table-column>
      <el-table-column label="实付金额" align="center" prop="payAmount">
        <template slot-scope="{row}">
          {{row.payAmount||"--"}}
        </template>
      </el-table-column>
      <el-table-column label="下单手机号" align="center" prop="userPhone" />
      <el-table-column label="所属商家" align="center" prop="businessName" />
      <el-table-column label="订单状态" align="center" prop="orderStatus">
        <template slot-scope="{row}">
          <!-- <el-tag v-for="(v,i) in statusList" :key="i" :type="v.tag" v-if="row.orderStatus==v.value">{{v.label}}</el-tag> -->
          <el-tag v-if="row.orderStatus==10">待使用</el-tag>
          <el-tag v-if="row.orderStatus==20&&row.refundStatus!=2&&row.refundStatus!=1" type="success">已完成</el-tag>
          <el-tag v-if="row.orderStatus==30&&row.refundStatus==2" type="info">已退款</el-tag>
          <el-tag v-if="row.orderStatus==20&&row.refundStatus==1" type="info">已完成（有退款）</el-tag>
          <el-tag v-if="row.orderStatus==0" type="danger">待支付</el-tag>
          <el-tag v-if="row.orderStatus==30&&row.refundStatus!=2" type="info">已取消</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="订单属性" align="center" prop="orderType">
        <template slot-scope="{row}">
          {{row.orderType==0?"自购":row.orderType==1?"奖励":"--"}}
        </template>
      </el-table-column>
      <el-table-column label="下单时间" align="center" prop="orderTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.orderTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="goDetails(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { businessProductOrderPageBusiness } from '@/api/shopOrder.js'
import { businessList } from "@/api/shop"
export default {
  data () {
    return {
      //按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 测试单表表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        businessId: undefined,
        orderNumber: undefined,
        userPhone: undefined,
        businessProductName: undefined,
        // orderStatus: null,
        orderType: null,
        queryStatus: undefined
      },
      daterangeCreateTime: [],
      // statusList: [
      //   { label: '待使用', value: 10, tag: "" },
      //   { label: '已完成', value: 20, tag: "success" },
      //   { label: '已退款', value: 3, tag: "info" },
      //   { label: '待支付', value: 0, tag: "danger" },
      //   { label: '已取消', value: 30, tag: "info" },
      // ],
      queryStatusList: [
        { label: '全部', value: undefined },
        { label: '待使用', value: 10 },
        { label: '已完成', value: 20 },
        { label: '已退款', value: 40 },
        { label: '待支付', value: 0 },
        { label: '已取消', value: 30 },
      ],
      tabPosition: 0,
      shopList: [],
    }
  },
  created () {
    this.getList();
    this.getShopList()
  },
  methods: {
    goDetails (row) {
      this.$router.push({
        path: '/shopOrder/details',
        query: { orderId: row.orderId }
      })
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    //  导出订单
    handleExport () {
      this.download('/system/businessProductOrder/export-business', {
        ...this.queryParams
      }, `门店商品订单_${new Date().getTime()}.xlsx`)
    },
    dealStatus (val) {
      return this.statusList.find(item => item.value == val).label
    },
    getShopList () {
      businessList().then(res => {
        this.shopList = res.data
      })
    },
    getList () {
      this.loading = true;
      this.queryParams["orderTimeStart"] = undefined;
      this.queryParams["orderTimeEnd"] = undefined;
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams["orderTimeStart"] = this.daterangeCreateTime[0];
        this.queryParams["orderTimeEnd"] = this.daterangeCreateTime[1];
      }
      businessProductOrderPageBusiness(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset () {
      this.form = {
        name: undefined,
        shelvedStatus: undefined,
        orderIndex: 1
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
  }
}
</script>

<style lang='scss' scoped>
</style>