<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="直播ID" prop="liveId">
        <el-input
          v-model="queryParams.liveId"
          placeholder="请输入直播ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺ID" prop="storeId">
        <el-input
          v-model="queryParams.storeId"
          placeholder="请输入店铺ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入收货人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="收货电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入收货人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单总价" prop="totalPrice">
        <el-input
          v-model="queryParams.totalPrice"
          placeholder="请输入订单总价"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付方式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择支付方式" clearable size="small">
          <el-option v-for="(item,index) in livePayTypeOptions" :key="item.dictValue+index" :label="item.dictLabel" :value="item.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable size="small">
          <el-option v-for="(item,index) in orderStatusOptions" :key="item.dictValue+index" :label="item.dictLabel" :value="item.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="退款状态" prop="refundStatus">
        <el-select v-model="queryParams.refundStatus" placeholder="请选择退款状态" clearable size="small">
            <el-option v-for="(item,index) in liveRefundStatusOptions" :key="item.dictValue+index" :label="item.dictLabel" :value="item.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="退款时间" prop="refundTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.refundTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择退款时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="退款金额" prop="refundMoney">
        <el-input
          v-model="queryParams.refundMoney"
          placeholder="请输入退款金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="快递单号" prop="deliverySn">
        <el-input
          v-model="queryParams.deliverySn"
          placeholder="请输入快递单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="核销码" prop="verifyCode">
        <el-input
          v-model="queryParams.verifyCode"
          placeholder="请输入核销码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结束时间" prop="finishTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.finishTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="收货电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入收货人电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="推广佣金" prop="tuiMoney">
        <el-input
          v-model="queryParams.tuiMoney"
          placeholder="请输入推广佣金"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="推广佣金状态 1已发放 0待发放" prop="tuiMoneyStatus">
        <el-select v-model="queryParams.tuiMoneyStatus" placeholder="请选择推广佣金状态 1已发放 0待发放" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="上级推荐人ID" prop="tuiUserId">
        <el-input
          v-model="queryParams.tuiUserId"
          placeholder="请输入上级推荐人ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
<!--      <el-form-item label="优惠金额" prop="userCouponId">
        <el-input
          v-model="queryParams.userCouponId"
          placeholder="请输入优惠金额"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="销售公司" prop="companyId">
        <el-input
          v-model="queryParams.companyId"
          placeholder="请输入公司id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="员工id" prop="companyUserId">
        <el-input
          v-model="queryParams.companyUserId"
          placeholder="请输入公司员工id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物流状态" prop="deliveryStatus">
        <el-select v-model="queryParams.deliveryStatus" placeholder="请选择物流状态" clearable size="small">
          <el-option v-for="(item,index) in deliveryStatusOptions" :key="item.dictValue+index" :label="item.dictLabel" :value="item.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item label="跟踪状态" prop="deliveryType">
        <el-select v-model="queryParams.deliveryType" placeholder="请选择物流跟踪状态" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="发货时间" prop="deliverySendTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.deliverySendTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择发货时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否售后" prop="isAfterSales">
        <el-input
          v-model="queryParams.isAfterSales"
          placeholder="请输入是否可售后"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道" prop="channel">
        <el-input
          v-model="queryParams.channel"
          placeholder="请输入渠道"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单来源" prop="source">
        <el-input
          v-model="queryParams.source"
          placeholder="请输入订单来源"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否改价" prop="isEditMoney">
        <el-input
          v-model="queryParams.isEditMoney"
          placeholder="请输入是否改价"
          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-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['live:liveOrder:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="liveOrderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单ID" align="center" prop="orderId" />
      <el-table-column label="直播ID" align="center" prop="liveId" />
      <el-table-column label="店铺ID" align="center" prop="storeId" />
      <el-table-column label="订单号" align="center" prop="orderCode" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="收货人" align="center" prop="userName" />
      <el-table-column label="收货人电话" align="center" prop="userPhone" />
      <el-table-column label="详细地址" align="center" prop="userAddress" />
      <el-table-column label="购物车id" align="center" prop="cartId" />
      <el-table-column label="订单商品总数" align="center" prop="totalNum" />
      <el-table-column label="订单总价" align="center" prop="totalPrice" />
      <el-table-column label="实际支付金额" align="center" prop="payPrice" />
      <el-table-column label="支付金额" align="center" prop="payMoney" />
      <el-table-column label="支付状态" align="center" prop="isPay" :formatter="liveIsPayFormatter"/>
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="支付方式" align="center" prop="payType" :formatter="livePayTypeFormatter"/>
      <el-table-column label="订单状态" align="center" prop="status" :formatter="orderStatusFormatter"/>
      <el-table-column label="退款状态" align="center" prop="refundStatus" :formatter="liveRefundStatusFormatter"/>
      <el-table-column label="退款图片" align="center" prop="refundImg" />
      <el-table-column label="退款用户说明" align="center" prop="refundExplain" />
      <el-table-column label="退款时间" align="center" prop="refundTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.refundTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="不退款的理由" align="center" prop="refundReason" />
      <el-table-column label="退款金额" align="center" prop="refundMoney" />
      <el-table-column label="快递公司编号" align="center" prop="deliveryCode" />
      <el-table-column label="快递名称" align="center" prop="deliveryName" />
      <el-table-column label="快递单号" align="center" prop="deliverySn" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="是否删除" align="center" prop="isDel" />
      <el-table-column label="成本价" align="center" prop="costPrice" />
      <el-table-column label="核销码" align="center" prop="verifyCode" />
      <el-table-column label="配送方式" align="center" prop="shippingType" :formatter="liveShippingTypeFormatter"/>
      <el-table-column label="支付渠道" align="center" prop="isChannel" />
      <el-table-column label="结束时间" align="center" prop="finishTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="deliveryTime" />
      <el-table-column label="推广佣金" align="center" prop="tuiMoney" />
      <el-table-column label="推广佣金状态" align="center" prop="tuiMoneyStatus" :formatter="liveTuiMoneyStatusFormatter"/>
      <el-table-column label="上级推荐人ID" align="center" prop="tuiUserId" />
      <el-table-column label="优惠金额" align="center" prop="discountMoney" />
      <el-table-column label="优惠金额" align="center" prop="userCouponId" />
      <el-table-column label="公司id" align="center" prop="companyId" />
      <el-table-column label="公司员工id" align="center" prop="companyUserId" />
      <el-table-column label="仓库代码" align="center" prop="storeHouseCode" />
      <el-table-column label="扩展订单ID" align="center" prop="extendOrderId" />
      <el-table-column label="支付运费" align="center" prop="payDelivery" />
      <el-table-column label="剩余金额" align="center" prop="payRemain" />
      <el-table-column label="物流状态" align="center" prop="deliveryStatus" :formatter="deliveryStatusFormat"/>
      <el-table-column label="物流结算状态" align="center" prop="deliveryPayStatus" />
      <el-table-column label="物流结算状态" align="center" prop="deliveryPayTime" />
      <el-table-column label="物流跟踪状态" align="center" prop="deliveryType" />
      <el-table-column label="物流结算金额" align="center" prop="deliveryPayMoney" />
      <el-table-column label="回单导入时间" align="center" prop="deliveryImportTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliveryImportTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发货时间" align="center" prop="deliverySendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliverySendTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否可售后" align="center" prop="isAfterSales" />
      <el-table-column label="部门id" align="center" prop="deptId" />
      <el-table-column label="渠道" align="center" prop="channel" />
      <el-table-column label="订单来源" align="center" prop="source" />
      <el-table-column label="开票金额" align="center" prop="billPrice" />
      <el-table-column label="邮费" align="center" prop="totalPostage" />
      <el-table-column label="支付邮费" align="center" prop="payPostage" />
      <el-table-column label="消费赚取积分" align="center" prop="gainIntegral" />
      <el-table-column label="使用积分" align="center" prop="useIntegral" />
      <el-table-column label="实际支付积分" align="center" prop="payIntegral" />
      <el-table-column label="给用户退了多少积分" align="center" prop="backIntegral" />
      <el-table-column label="是否改价" align="center" prop="isEditMoney" />
      <el-table-column label="取消理由" align="center" prop="cancelReason" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-location"
            @click="showExpress(scope.row)"
            v-hasPermi="['live:liveOrder:express']"
          >物流信息</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"
    />

    <el-dialog :title="expressDialog.title" :visible.sync="expressDialog.open" width="600px" append-to-body>
      <el-table style="margin-top: 20px;width: 100%"
                ref="orderHistoryTable"
                :data="traces" border>
        <el-table-column label="操作时间"  width="160" align="center">
          <template slot-scope="scope">
            {{scope.row.AcceptTime}}
          </template>
        </el-table-column>
        <el-table-column label="位置" align="center">
          <template slot-scope="scope">
            {{scope.row.Location}}
          </template>
        </el-table-column>
        <el-table-column label="描述" align="center">
          <template slot-scope="scope">
            {{scope.row.AcceptStation}}
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { listLiveOrder,exportLiveOrder } from "@/api/live/liveOrder";
import { getExpress} from '@/api/live/liveOrder'
import { parseTime } from '../../../utils/common'

export default {
  name: "LiveOrder",
  data() {
    return {
      //字典
      orderStatusOptions: [],
      livePayTypeOptions: [],
      liveRefundStatusOptions: [],
      deliveryStatusOptions:[],
      liveIsPayOptions:[],
      liveShippingTypeOptions:[],
      liveTuiMoneyStatusOptions:[],
      //用户
      userOptions: [],
      loadingUser:false,
      //用户查询参数
      userParams: {
        nickName:null
      },
      //物流
      express:null,
      traces:[],
      expressDialog:{
        title:"物流信息",
        open:false,
      },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      liveOrderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        liveId: null,
        storeId: null,
        orderCode: null,
        userId: null,
        userName: null,
        userPhone: null,
        userAddress: null,
        cartId: null,
        totalNum: null,
        totalPrice: null,
        payPrice: null,
        payMoney: null,
        isPay: null,
        payTime: null,
        payType: null,
        status: null,
        refundStatus: null,
        refundImg: null,
        refundExplain: null,
        refundTime: null,
        refundReason: null,
        refundMoney: null,
        deliveryCode: null,
        deliveryName: null,
        deliverySn: null,
        isDel: null,
        costPrice: null,
        verifyCode: null,
        shippingType: null,
        isChannel: null,
        finishTime: null,
        deliveryTime: null,
        tuiMoney: null,
        tuiMoneyStatus: null,
        tuiUserId: null,
        itemJson: null,
        discountMoney: null,
        userCouponId: null,
        companyId: null,
        companyUserId: null,
        storeHouseCode: null,
        extendOrderId: null,
        payDelivery: null,
        payRemain: null,
        deliveryStatus: null,
        deliveryPayStatus: null,
        deliveryPayTime: null,
        deliveryType: null,
        deliveryPayMoney: null,
        deliveryImportTime: null,
        deliverySendTime: null,
        isAfterSales: null,
        deptId: null,
        channel: null,
        source: null,
        billPrice: null,
        totalPostage: null,
        payPostage: null,
        gainIntegral: null,
        useIntegral: null,
        payIntegral: null,
        backIntegral: null,
        isEditMoney: null
      },
      // 表单校验
      rules: {
        orderCode: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户id不能为空", trigger: "change" }
        ],
        totalNum: [
          { required: true, message: "订单商品总数不能为空", trigger: "blur" }
        ],
        totalPrice: [
          { required: true, message: "订单总价不能为空", trigger: "blur" }
        ],
        payPrice: [
          { required: true, message: "实际支付金额不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "订单状态不能为空", trigger: "blur" }
        ],
        totalPostage: [
          { required: true, message: "邮费不能为空", trigger: "blur" }
        ],
        payPostage: [
          { required: true, message: "支付邮费不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_live_order_status").then(response => {
      this.orderStatusOptions = response.data;
    });
    this.getDicts("sys_live_pay_type").then(response => {
      this.livePayTypeOptions = response.data;
    });
    this.getDicts("sys_live_refund_status").then(response => {
      this.liveRefundStatusOptions = response.data;
    });
    this.getDicts("sys_store_order_delivery_status").then(response => {
      this.deliveryStatusOptions = response.data;
    });
    //isPay
    this.getDicts("sys_live_is_pay").then(response => {
      this.liveIsPayOptions = response.data;
    });
    //sys_live_shipping_type
    this.getDicts("sys_live_shipping_type").then(response => {
      this.liveShippingTypeOptions = response.data;
    });
    //sys_live_tui_money_status
    this.getDicts("sys_live_tui_money_status").then(response => {
      this.liveTuiMoneyStatusOptions = response.data;
    })
  },
  methods: {
    parseTime,
    /** 查询订单列表 */
    getList() {
      this.loading = true;
      listLiveOrder(this.queryParams).then(response => {
        this.liveOrderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    orderStatusFormatter(row, column) {
      return this.selectDictLabel(this.orderStatusOptions, row.status);
    },
    livePayTypeFormatter(row, column) {
      return this.selectDictLabel(this.livePayTypeOptions, row.payType);
    },
    liveRefundStatusFormatter(row, column) {
      return this.selectDictLabel(this.liveRefundStatusOptions, row.payType);
    },
    //liveTuiMoneyStatusOptions
    liveTuiMoneyStatusFormatter(row, column) {
      return this.selectDictLabel(this.liveTuiMoneyStatusOptions, row.status);
    },
    //liveShippingTypeOptions
    liveShippingTypeFormatter(row, column) {
      return this.selectDictLabel(this.liveShippingTypeOptions, row.shippingType);
    },
    //liveIsPayOptions
    liveIsPayFormatter(row, column) {
      return this.selectDictLabel(this.liveIsPayOptions, row.isPay);
    },
    //deliveryStatusOptions
    deliveryStatusFormat(row, column) {
      return this.selectDictLabel(this.deliveryStatusOptions, row.deliveryStatus);
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.orderId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportLiveOrder(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {
      });
    },
    showExpress(row){
      this.expressDialog.open=true;
      getExpress(row.orderId).then(response => {
        this.express = response.data;
        if(this.express!=null&&this.express.Traces!=null){
          this.traces=this.express.Traces
        }
      });
    }/* ,
    syncExpress(){
      var that=this;
      this.$confirm('确定同步物流状态吗', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        const data = that.item.orderId
        return syncExpress(data);
      }).then(() => {
        this.msgSuccess("操作成功");
        this.expressDialog.open=false
        getLiveOrder(this.item.orderId).then(response => {
          this.item=response.data
          that.getlogList(this.item.orderId);
          that.$parent.$parent.getList();
        });
      }).catch(function() {});
    } */
  }
};
</script>
