<template>
  <div class="mall-section-container">
    <div class="mall-content-section">
      <div class="mall-content-title" style="border:none">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item>积分商城</el-breadcrumb-item>
          <el-breadcrumb-item>订单</el-breadcrumb-item>
          <el-breadcrumb-item>礼品兑换</el-breadcrumb-item>
        </el-breadcrumb>
        <h3>
          <span>礼品兑换</span>
        </h3>
      </div>
      <el-tabs v-model="orderStatus" class="bread-content" @tab-click="handleClick">
        <div style="height:24px;background:#f0f2f5;"></div>
        <div>
          <div class="operate-top">
            <div class="fl">
              <div class="search-item">
                <el-date-picker class="date-picker" @change="changeTime" v-model="dateValue" type="datetimerange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
              </div>
              <div class="search-item">
                <el-select v-model="changeType" placeholder="配送方式" clearable @change="getList">
                  <el-option label="物流发货" :value="2"></el-option>
                  <el-option label="在线发货（虚拟商品）" :value="3"></el-option>
                </el-select>
              </div>
              <div class="search-item">
                <el-input v-model="searchValue" placeholder="请输入礼品名称/编码进行搜索" prefix-icon="el-icon-search" clearable @keyup.enter.native="handleInputSearch(searchValue)" @clear="handleInputSearch(searchValue)"> </el-input>
              </div>
            </div>
            <div class="fr">
              <el-button type="primary" @click="betachDeliver">批量发货</el-button>
              <el-button plain @click="exportExcel"><i class="iconfont icon-icon_yunxiazai" style="margin-right:5px"></i>导出订单</el-button>
            </div>
          </div>
        </div>
        <el-tab-pane label="全部" name="-1" class="goods-table-content"> </el-tab-pane>
        <el-tab-pane label="待付款" name="11" class="goods-table-content"> </el-tab-pane>
        <el-tab-pane :label="'待发货' + '(' + deliverNum + ')'" name="1" class="goods-table-content"> </el-tab-pane>
        <el-tab-pane label="已发货" name="3" class="goods-table-content"> </el-tab-pane>
        <el-tab-pane label="交易成功" name="4" class="goods-table-content"> </el-tab-pane>
        <el-tab-pane label="交易关闭" name="0" class="goods-table-content"> </el-tab-pane>
        <div class="goods-list-content">
          <el-table :data="tableData" ref="multipleTable" style="width: 100%" @sort-change="sortChange" v-loading="loading">
            <!-- <el-table-column type="selection" width="40"> </el-table-column> -->
            <el-table-column label="礼品信息" width="300px">
              <template slot-scope="scope">
                <div class="gift-info">
                  <img :src="scope.row.giftMainPic" />
                  <div class="info-name">
                    <p class="pro-ellipsis">{{ scope.row.giftName }}</p>
                    <p class="category-ellipsis">
                      <span v-for="(i, v) in scope.row.sku" :key="v" style="margin-right:3px">{{ i.proName }}:{{ i.valueName ? i.valueName : '--' }}</span>
                    </p>
                    <p class="category-ellipsis">规格编码:{{ scope.row.proCode }}</p>
                  </div>
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="下单时间" sortable="custom">
              <template slot-scope="scope">
                <p style="line-height:18px">{{ scope.row.createTime ? getTime(scope.row.createTime) : '--' }}</p>
                <p style="line-height:18px" class="gray-color">{{ scope.row.createTime ? getSeconds(scope.row.createTime) : '--' }}</p>
              </template>
            </el-table-column>
            <el-table-column prop="definedCode" label="订单编号">
              <template slot-scope="scope">
                <el-dropdown @visible-change="i => getOrderInfo(i, scope.row.integralMallProExchangeId)" trigger="click" class="drop-list">
                  <span class="el-dropdown-link"> {{ scope.row.definedCode }}<i class="iconfont icon-icon-"></i> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="orderInfo.definedCode">订单编号：{{ orderInfo.orderNumber }}</el-dropdown-item>
                    <el-dropdown-item command="orderInfo.definedCode">商户支付号：{{ orderInfo.merchantNumber }}</el-dropdown-item>
                    <el-dropdown-item command="orderInfo.definedCode">支付流水号：{{ orderInfo.payNumber }}</el-dropdown-item>
                    <el-dropdown-item command="orderInfo.definedCode">下单时间：{{ orderInfo.orderTime ? getTimeAll(orderInfo.orderTime) : '--' }}</el-dropdown-item>
                    <el-dropdown-item command="orderInfo.definedCode">付款时间：{{ orderInfo.payTime ? getTimeAll(orderInfo.payTime) : '--' }}</el-dropdown-item>
                    <el-dropdown-item command="orderInfo.definedCode">发货时间：{{ orderInfo.deliveryTime ? getTimeAll(orderInfo.deliveryTime) : '--' }}</el-dropdown-item>
                    <el-dropdown-item command="orderInfo.definedCode">交易完成时间：{{ orderInfo.finishTime ? getTimeAll(orderInfo.finishTime) : '--' }}</el-dropdown-item>
                    <el-dropdown-item command="orderInfo.definedCode">收货信息：{{ orderInfo.definedCode }} {{ orderInfo.consignee }} {{ orderInfo.consigneePhone }} {{ orderInfo.receivingAddress }}</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </template>
            </el-table-column>
            <el-table-column label="会员">
              <template slot-scope="scope">
                <memberInfo :memberObj="scope.row" :requestProject="requestProject" :placement="placement"></memberInfo>
                <!-- <p>{{ scope.row.memberName }}</p>
                <p>
                  <a :href="windowUrl + '/member/#/wechatmemberDetail?memberId=' + scope.row.memberId" target="_blank">{{ scope.row.cardNum }}</a>
                </p> -->
              </template>
            </el-table-column>
            <el-table-column prop="complete" label="交易状态">
              <template slot-scope="scope">
                <p style="line-height:18px">
                  <span v-if="scope.row.status === 11">待付款</span>
                  <span v-if="scope.row.status === 1">待发货</span>
                  <span v-if="scope.row.status === 3">已发货</span>
                  <span v-if="scope.row.status === 4">交易完成</span>
                  <span v-if="scope.row.status <= 0">交易关闭</span>
                </p>
                <p style="line-height:18px">
                  <el-button type="text" v-if="scope.row.status === 1 && scope.row.changeType === 2" @click="handleDeliver(scope.row)">物流发货</el-button>
                  <el-button type="text" v-if="scope.row.status === 1 && scope.row.changeType === 3" @click="handleDeliver(scope.row)">在线发货</el-button>
                  <el-button type="text" v-if="scope.row.status === 3" @click="getDeliverInfo(scope.row)">查看物流</el-button>

                  <el-button type="text" v-if="scope.row.status === 1 && scope.row.refundStatus !== 2" @click="closeOrder(scope.row)">关闭订单</el-button>
                  <!-- 待付款关闭订单 -->
                  <el-button type="text" v-if="scope.row.status === 11 && scope.row.refundStatus !== 2" @click="closeOrderNoPay(scope.row)">关闭订单</el-button>
                  <el-button type="text" v-if="scope.row.status <= 0" @click="getCloseInfo(scope.row)">查看详情</el-button>
                </p>
              </template>
            </el-table-column>
            <el-table-column prop="saleNum" label="实付金额">
              <template slot-scope="scope">
                <p style="line-height:18px">积分：{{ scope.row.unitCostIntegral }}</p>
                <p style="line-height:18px">现金：￥{{ scope.row.payCost }}</p>
              </template>
            </el-table-column>

            <template slot="empty">
              <div class="no-data-wrap">
                <div class="no-data-icon">
                  <img src="../../../static/img/no-data_icon.png" />
                </div>
                <p>暂无数据</p>
              </div>
            </template>
          </el-table>
        </div>
        <div class="pagination-conteiner" style="background:#fff;margin:-50px 24px 0 24px">
          <div class="pagination">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageNum" :page-sizes="pageSizes" :page-size="pageSize" layout="total, sizes, prev, pager, next" :total="total"> </el-pagination>
          </div>
        </div>
      </el-tabs>
    </div>
    <!-- 批量发货 -->
    <importdispatch :deliverModal="deliverModal"></importdispatch>
    <!-- 导出订单 -->
    <vue-gic-export-excel :dataArr="tableData" :dialogVisible.sync="dialogVisible" :type="2" :excelUrl="excelUrl" :params="params" :projectName="projectName"></vue-gic-export-excel>
    <!-- 发货 -->
    <deliverModal :deliverdModal="deliverdModal" @getList="getList"></deliverModal>
    <!-- 物流详情 -->
    <deliverInfoModal :deliverInfodModal="deliverInfodModal" v-show="deliverInfodModal.dialogVisible"></deliverInfoModal>
    <!-- 关闭订单 -->
    <closeOrder :refunOrderdModal="refunOrderdModal" @getList="getList"></closeOrder>
    <!-- 取消订单 -->
    <cancelOrder :closeInfoModal="closeInfoModal"></cancelOrder>
    <!-- 会员信息 -->
  </div>
</template>

<script>
import qs from 'qs';
import request from '../../service/request.js';
import getInputVal from '../../utils/common.js';
import importdispatch from './importInfo';
import deliverModal from './delive';
import deliverInfoModal from './deliverInfo';
import closeOrder from './closeOrder';
import cancelOrder from './getCloseDetail';
import memberInfo from '../../components/memberInfo';
export default {
  data() {
    return {
      getTime: getInputVal.getTime,
      getTimeAll: getInputVal.formatDate,
      getSeconds: getInputVal.getSeconds,
      activeName: '-1',
      currentPage: 1,
      pageSizes: [20, 30, 50],
      pageSize: 20,
      total: 0,
      tableData: [],
      deliverNum: 0,
      dateValue: [new Date() - 90 * 24 * 60 * 60 * 1000, new Date().valueOf()],
      searchValue: '',
      beginTime: '',
      endTime: '',
      changeType: '', //配送方式
      windowUrl: window.location.origin,
      orderStatus: '-1',
      deliverModal: {
        //批量发货
        dialogVisible: false
      },
      // 导出数据控件
      projectName: 'integral-mall', // 当前项目名
      dialogVisible: false,
      excelUrl: '/api-integral-mall/download-integral-online-excel', // 下载数据的地址
      params: {}, // 传递的参数
      orderInfo: {}, //订单信息
      deliverdModal: {
        //发货
        dialogVisible: false,
        changeType: '',
        integralMallProExchangeId: '',
        consignee: '', //收件人
        consigneePhone: '', //联系方式
        receivingAddress: '', //收货地址
        logisticsCompanyId: '',
        logisticsCode: '',
        otherLogisticsCompanyName: ''
      },
      deliverInfodModal: {
        //物流详情
        dialogVisible: false,
        changeType: '',
        integralMallProExchangeId: '',
        consignee: '', //收件人
        consigneePhone: '', //联系方式
        receivingAddress: '', //收货地址
        traces: [], //物流信息
        clerkName: '', //操作人员
        deliveryTime: '', //发货时间
        deliveryContent: '', //发货内容
        logisticsCompanyName: '',
        logisticsCompanyId: '',
        logisticsCompanyCode: '',
        deliverStatus: true //false是在线发货。true是快递发货
      },
      refunOrderdModal: {
        //关闭订单
        dialogVisible: false,
        integralMallProExchangeId: ''
      },
      closeInfoModal: {
        //取消订单的详情
        dialogVisible: false,
        integralMallProExchangeId: '',
        consignee: '', //收件人
        consigneePhone: '', //联系方式
        receivingAddress: '', //收货地址
        cancelTime: '',
        clerkName: '', //操作人员
        cancelReason: ''
      },
      integralMallProId: '', //礼品id
      placement: 'top-start',
      requestProject: 'integral-mall'
    };
  },
  created() {
    this.getNumber();
    this.integralMallProId = this.$route.query.integralMallProId;
    this.getList();
  },
  methods: {
    // tab切换
    handleClick(tab) {
      this.orderStatus = tab.name;
      this.getList();
    },
    // 搜索
    handleInputSearch() {
      this.currentPage = 1;
      this.getList();
    },
    // 分页
    handleSizeChange(size) {
      //设置每页条数
      this.pageSize = size;
      this.getList();
    },
    handleCurrentChange(page) {
      //设置跳到第几页
      this.currentPage = page;
      this.getList();
    },
    getOrderInfo(i, integralMallProExchangeId) {
      if (i) {
        let params = {
          integralMallProExchangeId: integralMallProExchangeId
        };
        request.get('/api-integral-mall/get-order-information', { params }).then(res => {
          if (res.data.errorCode === 0) {
            if (res.data.result) {
              this.orderInfo = res.data.result;
            } else {
              this.orderInfo = {};
            }
          } else {
            this.$message.error(res.data.message);
          }
        });
      }
    },
    // 排序
    sortChange(value) {
      this.sortColumn = value.prop;
      if (value.order == 'ascending') {
        this.sortType = 'asc';
      } else if (value.order == 'descending') {
        this.sortType = 'desc';
      }
      this.getList();
    },
    // 时间改变
    changeTime() {
      if (this.dateValue) {
        this.beginTime = this.getTimeAll(this.dateValue[0]);
        this.endTime = this.getTimeAll(this.dateValue[1]);
      } else {
        this.beginTime = '';
        this.endTime = '';
      }
      this.getList();
    },
    //获取列表
    getList() {
      this.beginTime = this.dateValue ? this.getTimeAll(this.dateValue[0]) : '';
      this.endTime = this.dateValue ? this.getTimeAll(this.dateValue[1]) : '';
      let params = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        changeType: this.changeType === '' ? '-1' : this.changeType,
        orderStatus: this.orderStatus, //-1:全部 1、待发货 11、待付款 3、已发货 0 、已取消 4、交易成功
        searchParams: this.searchValue,
        pageType: 2, //1、代表优惠券订单列表 2、礼品订单
        beginTime: this.beginTime,
        endTime: this.endTime,
        sortType: this.sortType,
        integralMallProId: this.integralMallProId ? this.integralMallProId : ''
      };

      this.loading = true;
      request.post('/api-integral-mall/page-undeliver', qs.stringify(params)).then(res => {
        if (res.data.errorCode === 0) {
          this.loading = false;
          if (res.data.result.rows) {
            this.tableData = res.data.result.rows;
            this.total = res.data.result.total;
            for (let item of this.tableData) {
              if (item.sku) {
                item.sku = JSON.parse(item.sku);
              }
            }
          } else {
            this.tableData = [];
            this.total = 0;
          }
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    getNumber() {
      request.get('/api-integral-mall/get-not-send-count').then(res => {
        if (res.data.errorCode === 0) {
          this.deliverNum = res.data.result;
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    //批量发货
    betachDeliver() {
      this.deliverModal.dialogVisible = true;
    },
    // 导出发货单
    exportExcel() {
      if (this.dateValue) {
        this.beginTime = this.getTimeAll(this.dateValue[0]);
        this.endTime = this.getTimeAll(this.dateValue[1]);
      } else {
        this.beginTime = '';
        this.beginTime = '';
      }
      if (!this.beginTime || !this.beginTime) {
        this.$message.error('时间不能为空');
        return;
      }
      this.params = {
        orderStatus: this.orderStatus,
        changeType: this.changeType,
        searchParams: this.searchValue,
        beginTime: this.beginTime,
        endTime: this.endTime,
        pageType: 2, //1、代表优惠券订单列表 2、礼品订单
        requestProject: 'integral-mall'
      };
      this.dialogVisible = true;
      // window.location = `${exportOnlineListExcel}?orderStatus=${this.listParams.orderStatus}&changeType=${this.listParams.changeType}&searchParams=${this.listParams.searchParams}&beginTime=${this.listParams.beginTime}&endTime=${this.listParams.endTime}&requestProject=marketing`;
    },
    // 发货
    handleDeliver(item) {
      // 获取快递发货的信息
      let params = {
        integralMallProExchangeId: item.integralMallProExchangeId
      };
      request.post('/api-integral-mall/list-logistics-traces', qs.stringify(params)).then(res => {
        if (res.data.errorCode === 0) {
          this.deliverdModal.integralMallProExchangeId = item.integralMallProExchangeId;
          this.deliverdModal.changeType = item.changeType;
          this.deliverdModal.consignee = res.data.result.changeLog.consignee; //收件人
          this.deliverdModal.consigneePhone = res.data.result.changeLog.consigneePhone; //联系方式
          this.deliverdModal.receivingAddress = res.data.result.changeLog.receivingAddress; //收货地址
          this.deliverdModal.deliverStatus = true;
          this.deliverdModal.dialogVisible = true;
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    //查看物流
    getDeliverInfo(item) {
      let params = {
        integralMallProExchangeId: item.integralMallProExchangeId
        // integralMallProExchangeId: 'ff80808161f71e780161f78895700005'
      };
      request.post('/api-integral-mall/list-logistics-traces', qs.stringify(params)).then(res => {
        if (res.data.errorCode === 0) {
          this.deliverInfodModal.integralMallProExchangeId = item.integralMallProExchangeId;
          this.deliverInfodModal.changeType = item.changeType;
          this.deliverInfodModal.consignee = res.data.result.changeLog.consignee; //收件人
          this.deliverInfodModal.consigneePhone = res.data.result.changeLog.consigneePhone; //联系方式
          this.deliverInfodModal.receivingAddress = res.data.result.changeLog.receivingAddress; //收货地址
          this.deliverInfodModal.logisticsCompanyName = res.data.result.changeLog.logisticsCompanyName; //快递公司
          this.deliverInfodModal.logisticsCompanyId = res.data.result.changeLog.logisticsCompanyId; //快递公司
          this.deliverInfodModal.logisticsCompanyCode = res.data.result.changeLog.logisticsCompanyCode; //快递code
          this.deliverInfodModal.courierNumber = res.data.result.changeLog.courierNumber; //快递单号
          this.deliverInfodModal.otherLogisticsCompanyName = '';
          this.deliverInfodModal.otherLogisticsCompanyName = ''; //快递单号
          if (res.data.result.traces) {
            this.deliverInfodModal.traces = res.data.result.traces; //物流信息
          } else {
            this.deliverInfodModal.traces = [];
          }
          this.deliverInfodModal.changeType = item.changeType;
          this.deliverInfodModal.deliveryTime = res.data.result.changeLog.deliveryTime; //发货时间
          this.deliverInfodModal.clerkName = res.data.result.changeLog.clerkName; //操作人员
          this.deliverInfodModal.deliveryContent = res.data.result.changeLog.deliveryContent; //发货内容
          if (item.changeType === 2) {
            //快递发货
            this.deliverInfodModal.deliverStatus = true;
          } else if (item.changeType === 3) {
            //在线发货
            this.deliverInfodModal.deliverStatus = false;
          }

          this.deliverInfodModal.dialogVisible = true;
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    // 关闭订单
    closeOrder(item) {
      this.refunOrderdModal.integralMallProExchangeId = item.integralMallProExchangeId;
      // this.refunOrderdModal.orderStatus = item.orderStatus;
      this.refunOrderdModal.dialogVisible = true;
    },
    // 待付款关闭订单
    closeOrderNoPay(item) {
      let params = {
        integralMallProExchangeId: item.integralMallProExchangeId,
        optType: 4
      };
      request.get('/api-integral-mall/order-opt', { params }).then(res => {
        if (res.data.errorCode === 0) {
          this.$message.success('关闭订单成功');
          this.getList();
        } else {
          this.$message.error(res.data.message);
        }
      });
    },
    // 获取关闭的详情
    getCloseInfo(item) {
      let params = {
        integralMallProExchangeId: item.integralMallProExchangeId
      };
      request.post('/api-integral-mall/list-logistics-traces', qs.stringify(params)).then(res => {
        if (res.data.errorCode === 0) {
          this.closeInfoModal.consignee = res.data.result.changeLog.consignee; //收件人
          this.closeInfoModal.consigneePhone = res.data.result.changeLog.consigneePhone; //联系方式
          this.closeInfoModal.receivingAddress = res.data.result.changeLog.receivingAddress; //收货地址

          this.closeInfoModal.cancelTime = res.data.result.changeLog.cancelTime; //取消时间时间
          this.closeInfoModal.clerkName = res.data.result.changeLog.clerkName; //操作人员
          this.closeInfoModal.cancelReason = res.data.result.changeLog.cancelReason; //取消原因
          this.closeInfoModal.changeType = item.changeType;
          this.closeInfoModal.dialogVisible = true;
        } else {
          this.$message.error(res.data.message);
        }
      });
    }
  },
  components: {
    importdispatch,
    deliverModal,
    deliverInfoModal,
    closeOrder,
    cancelOrder,
    memberInfo
  }
};
</script>
<style scoped>
.section {
  background: #fff;
  margin: 24px;
  padding: 24px;
}

.operate-top {
  background: #fff;
  height: 32px;
  background: #fff;
  margin: 0 25px;
  padding: 25px 25px 0 25px;
}
.goods-list-content {
  margin: 0 25px 25px 25px;
  padding: 22px 24px 24px 24px;
  background: #fff;
}
.gift-info {
  display: flex;
}
.gift-info img {
  width: 63px;
  height: 63px;
  border-radius: 4px;
  margin-right: 5px;
  border: 1px solid #dcdfe6;
}
.info-name {
  display: inline-block;
  vertical-align: top;
  width: 75%;
}
.pro-ellipsis {
  color: #303133;
  line-height: 18px;
  font-size: 14px;
}
.category-ellipsis {
  color: #909399;
  font-size: 13px;
}
.el-icon-edit {
  margin-left: 5px;
  cursor: pointer;
}
.pagination-conteiner {
  padding: 30px 16px 34px 0;
}
.drop-list .icon-icon- {
  color: #909399;
  margin-left: 5px;
  cursor: pointer;
}
.drop-list .icon-icon-:hover {
  color: #303133;
}
</style>
