<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="盲票组名称" prop="boxTitle">
        <el-input
          v-model="queryParams.boxTitle"
          placeholder="请输入盲票组名称"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盲票类型" prop="boxType">
        <el-select
          v-model="queryParams.boxType"
          placeholder="请选择盲票类型"
          style="width: 100%"
          size="small"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option label="线上盲票" value="online"></el-option>
          <el-option label="线下盲票" value="offline"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入用户手机号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="下单时间">
        <el-date-picker
          v-model="tradeTimeArr"
          type="datetimerange"
          size="small"
          value-format="timestamp"
          range-separator="至"
          start-placeholder="时间选择"
          end-placeholder="时间选择"
          @change="tardeTime"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="门店" prop="parentId">
        <el-select
          v-model="queryParams.channelId"
          placeholder="输入门店名称或手机号码"
          style="width: 100%;"
          filterable
          clearable
          :filter-method="dataFilter"
          @change="handleQuery"
        >
          <el-option
            v-for="(item, index) in channelList"
            :key="item.channelId"
            :label="item.name"
            :value="item.channelId">
            <div>
              <span style="float: left;">{{item.name}} </span>
              <span style="float: right;">{{item.mobile}}</span>
            </div>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="付款方式" prop="payType">
        <el-select
          v-model="queryParams.payType"
          placeholder="请选择付款方式"
          style="width: 100%"
          size="small"
          filterable
          clearable
          @change="handleQuery"
        >
          <el-option label="全部" value=""></el-option>
          <el-option label="微信" value="2"></el-option>
          <el-option label="支付宝" value="1"></el-option>
        </el-select>
      </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-button
          type="infor"
          plain
          icon="el-icon-download"
          size="small"
          @click="handleExportDraw"
          v-hasPermi="['order:userTicket:export']"
          >导出订单</el-button>
        <el-button
          type="infor"
          plain
          icon="el-icon-download"
          size="small"
          @click="handleExportDrawExport"
          v-hasPermi="['order:userTicket:ticketExport']"
        >导出订单盲票</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <!-- 订单状态 -->
    <el-tabs type="card" v-model="state" @tab-click="handleClick">
      <el-tab-pane label="全部订单" name="null"></el-tab-pane>
      <el-tab-pane label="待付款" name="0"></el-tab-pane>
      <el-tab-pane label="已完成" name="1"></el-tab-pane>
      <el-tab-pane label="已取消" name="-1"></el-tab-pane>
    </el-tabs>

    <!-- 列表 -->
    <el-table ref="table" v-loading="loading" :data="list">
      <el-table-column
        label="订单号"
        fixed="left"
        align="center"
        prop="orderId"
        min-width="110"
      />
      <el-table-column label="下单时间" min-width="100">
        <template slot-scope="{ row }">
          <div>{{ parseTime(row.createdTime) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="盲票组名称" prop="title" min-width="95" />
      <el-table-column label="盲票类型" prop="boxType" min-width="95" />
      <el-table-column label="盲票售价(元)" prop="parentName" min-width="95">
        <template slot-scope="{ row }">
          <div>{{ $numberFormat(row.salePrice) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="数量" prop="ticketNum" min-width="60" />
      <el-table-column label="订单状态" prop="status" min-width="95" />
      <el-table-column label="实付金额(元)" min-width="95">
        <template slot-scope="{ row }">
          <div>{{ $numberFormat(row.payAmt) }}</div>
        </template>
      </el-table-column>
      <el-table-column label="付款方式" min-width="95">
        <template slot-scope="{ row }">
          <div v-if="row.payType == '2'">微信</div>
          <div v-else-if="row.payType == '1' || row.payType == '10'">支付宝</div>
          <div v-else>--</div>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称/手机号" min-width="120">
        <template slot-scope="{ row }">
          <div>{{ row.nickName }}/{{ row.phone }}</div>
        </template>
      </el-table-column>
      <el-table-column label="门店" prop="channelName" min-width="95" >
        <template slot-scope="{ row }">
          <div v-if="row.channelName">{{ row.channelName }}</div>
          <div v-else>--</div>
        </template>
      </el-table-column>>
      <el-table-column label="操作" fixed="right" align="center" width="80">
        <template slot-scope="{ row }">
          <div>
            <el-button
              v-hasPermi="['order:channel:query']"
              type="text"
              @click="$router.push({ name: 'UserTicketDetail', query: { id: row.orderId } })"
              >查看</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pageParams.pageNum"
      :limit.sync="pageParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>
<script>
import { ticketOrderList, ticketOrderExport, listSite, deliverOrderDrawExport } from "@/api/business/order";

export default {
  name: "UserTicket",
  data() {
    return {
      loading: false,
      showSearch: true,
      // 筛选
      queryParams: {
        boxTitle: "",
        boxType: "",
        startTime: "",
        endTime: "",
        phone: "",
        status: "",
        channelId: "",
        payType: "",
      },
      // 上级渠道列表
      channelList:[],
      channelCopyList:[],
      // 分页
      pageParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      list: [],
      // 下单时间
      tradeTimeArr: [],
      // 订单列表状态
      state: "null",
    };
  },
  created() {
    this.getList()
    this.getChannelList()
  },
  methods: {
    // 订单列表
    getList() {
      this.loading = true;
      ticketOrderList(
        "pageNum=" +
          this.pageParams.pageNum +
          "&pageSize=" +
          this.pageParams.pageSize +
          "&",
        this.queryParams
      )
        .then((res) => {
          this.loading = false;
          if (res.code == 0) {
            this.total = res.total;
            this.list = res.rows;
            this.list.forEach((item) => {
              item.boxType = item.boxType && JSON.parse(item.boxType).desc;
              item.status = item.status && JSON.parse(item.status).desc;
            });
          }
        })
        .catch(() => {
          this.loading = false;
        });
    },

    // 订单切换
    handleClick(e) {
      if (e.name == "null") {
        this.queryParams.status = "";
      } else {
        this.queryParams.status = Number(e.name);
      }
      this.getList();
    },

    //搜索
    handleQuery() {
      this.pageParams.pageNum = 1;
      this.getList();
    },

    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.tradeTimeArr = [];
      this.pageParams.pageNum = 1;
      this.queryParams.startTime = "";
      this.queryParams.endTime = "";
      this.queryParams.status = "";
      this.queryParams.channelId = "";
      this.queryParams.payType = "";
      this.state = "null"
      this.getList();
    },

    // 获取上级渠道下拉列表
    getChannelList(){
      listSite({}).then(response => {
        this.channelList = response.rows || [];
        this.channelCopyList = response.rows || [];
      });
    },
    dataFilter(val) {
      if (val) { //val存在
        this.channelList = this.channelCopyList.filter((item) => {
          if (!!~item.mobile.indexOf(val) || !!~item.mobile.toUpperCase().indexOf(val.toUpperCase())
            || !!~item.name.indexOf(val) || !!~item.name.indexOf(val)) {
            return true
          }
        })
      } else { //val为空时，还原数组
        this.channelList = this.channelCopyList;
      }
    },

    // 选择下单时间
    tardeTime(e) {
      if (e) {
        this.queryParams.startTime = e[0];
        this.queryParams.endTime = e[1];
        this.handleQuery();
      } else {
        this.queryParams.startTime = "";
        this.queryParams.endTime = "";
        this.handleQuery();
      }
    },

    // 查看详情
    getDetail(row) {
      this.$router.push({ name: "ChannelDetail", query: { id: row.orderId } });
    },

    // 点击发货
    toGoods(row) {
      this.goodsShow = true;
      this.goodsInfo = row;
      this.getBoxList(row);
    },

    // 关闭发货弹框
    close() {
      this.getList();
    },

    // 导出订单
    handleExportDraw() {
      this.$confirm("是否确认导出订单?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.vloading = this.$loading({
            lock: true,
            text: "正在导出订单.....",
            background: "rgba(0, 0, 0, 0.7)",
          });
          return ticketOrderExport(this.queryParams);
        })
        .then((response) => {
          this.vloading.close();
          this.download(response.msg);
        })
        .catch(() => {
          this.vloading.close();
        });
    },
    handleExportDrawExport(){
      this.$confirm("是否确认导出订单盲票?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.vloading = this.$loading({
            lock: true,
            text: "正在导出订单.....",
            background: "rgba(0, 0, 0, 0.7)",
          });
          return deliverOrderDrawExport(this.queryParams);
        })
        .then((response) => {
          this.vloading.close();
          this.download(response.msg);
        })
        .catch(() => {
          this.vloading.close();
        });
    }
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-tabs--border-card > .el-tabs__content {
  padding: 0;
}
</style>
