<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
<!--      <el-form-item label="添加时间" prop="addTime">
        <el-date-picker clearable
          v-model="queryParams.addTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择添加时间">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="收货人姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入 收货人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="orderNum">
        <el-input
          v-model="queryParams.orderNum"
          placeholder="请输入 订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label=" 父订单(支付订单)号" prop="payId">
        <el-input
          v-model="queryParams.payId"
          placeholder="请输入 父订单(支付订单)id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="收货人手机号" prop="phoneNum">
        <el-input
          v-model="queryParams.phoneNum"
          placeholder="请输入 收货人手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择订单状态" clearable>
          <el-option
            v-for="dict in dict.type.app_goods_order_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="交易成功时间" prop="transactionTime">
        <el-date-picker clearable
          v-model="queryParams.transactionTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择交易成功时间">
        </el-date-picker>
      </el-form-item>-->
      <el-form-item label="UID" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入 用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="日期" prop="daterange">
        <el-date-picker
          v-model="daterange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="dateChage"
        ></el-date-picker>
      </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="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:order:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:order:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:order:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="用户id" align="center" prop="uid" />
      <el-table-column label="商品" align="center" prop="productNames" width="150"/>
      <el-table-column label="下单时间" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货人地址" align="center" prop="address" width="200"/>
<!--      <el-table-column label=" 收货人地址id" align="center" prop="addressId" />
      <el-table-column label=" 用户id" align="center" prop="anchorId" />-->
<!--      <el-table-column label=" 申请退款审核不通过原因" align="center" prop="auditFailureReason" />-->
<!--      <el-table-column label=" 商家id" align="center" prop="businessId" />-->
<!--      <el-table-column label=" 商家logo" align="center" prop="businessLogo" />-->
      <el-table-column label="商家名称" align="center" prop="businessName" />
<!--      <el-table-column label=" 订单来源id" align="center" prop="goodsChannelId" />-->
<!--      <el-table-column label="是否人工退款 1：是 2：否" align="center" prop="isManualRefund" />-->
      <el-table-column label="发货物流id" align="center" prop="logisticsId" />
<!--      <el-table-column label="人工退款金额" align="center" prop="manualRefundMoney" />-->
<!--      <el-table-column label="人工退款操作人" align="center" prop="manualRefundOperator" />-->
<!--      <el-table-column label="人工退款方式 1:支付宝 2:微信 3:人工转账" align="center" prop="manualRefundType" />-->
      <el-table-column label="收货人姓名" align="center" prop="name" />
      <el-table-column label="订单金额" align="center" prop="orderAmount" />
      <el-table-column label="订单号" align="center" prop="orderNum" />
      <el-table-column label="父订单(支付订单)id" align="center" prop="payId" />
      <el-table-column label="收货人手机号" align="center" prop="phoneNum" />
      <el-table-column label="用户支付订单号" align="center" prop="platformPayOrder" />
<!--      <el-table-column label="退货订单状态 1:待审核 2:待发货 3:待收货 4:退款中 5:退款完成 6:退款失败 7:审核拒绝" align="center" prop="quitStatus" />-->
<!--      <el-table-column label=" 申请退款原因" align="center" prop="reason" />-->
<!--      <el-table-column label=" 退货物流id" align="center" prop="refundLogisticsId" />-->
<!--      <el-table-column label="申请退款备注" align="center" prop="refundNotes" />-->
<!--      <el-table-column label="退款备注图片" align="center" prop="refundNotesImages" />-->
<!--      <el-table-column label="退款订单号" align="center" prop="refundOrderNum" />-->
<!--      <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="退款类型 1.仅退款 2.退货退款" align="center" prop="refundType" />-->
      <el-table-column label="备注" align="center" prop="remake" />
      <el-table-column label="订单状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.app_goods_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
<!--      <el-table-column label=" 任务id" align="center" prop="taskId" />-->
      <el-table-column label="交易金额(实付金额)" align="center" prop="transactionAmount" />
      <el-table-column label="交易成功时间" align="center" prop="transactionTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.transactionTime, '{y}-{m}-{d}').indexOf("1970")!=-1?"-":parseTime(scope.row.transactionTime, '{y}-{m}-{d}')}}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleSend(scope.row)"
            v-hasPermi="['business:order:send']"
            v-if="scope.row.status == 2"
          >发货</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:order:remove']"
          >删除</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="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="添加时间" prop="addTime">
          <el-date-picker clearable
            v-model="form.addTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择添加时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label=" 收货人地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入 收货人地址" />
        </el-form-item>
        <el-form-item label=" 收货人地址id" prop="addressId">
          <el-input v-model="form.addressId" placeholder="请输入 收货人地址id" />
        </el-form-item>
        <el-form-item label=" 用户id" prop="anchorId">
          <el-input v-model="form.anchorId" placeholder="请输入 用户id" />
        </el-form-item>
        <el-form-item label=" 申请退款审核不通过原因" prop="auditFailureReason">
          <el-input v-model="form.auditFailureReason" placeholder="请输入 申请退款审核不通过原因" />
        </el-form-item>
        <el-form-item label=" 商家id" prop="businessId">
          <el-input v-model="form.businessId" placeholder="请输入 商家id" />
        </el-form-item>
        <el-form-item label=" 商家logo" prop="businessLogo">
          <el-input v-model="form.businessLogo" placeholder="请输入 商家logo" />
        </el-form-item>
        <el-form-item label=" 商家名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入 商家名称" />
        </el-form-item>
        <el-form-item label=" 订单来源id" prop="goodsChannelId">
          <el-input v-model="form.goodsChannelId" placeholder="请输入 订单来源id" />
        </el-form-item>
        <el-form-item label="是否人工退款 1：是 2：否" prop="isManualRefund">
          <el-input v-model="form.isManualRefund" placeholder="请输入是否人工退款 1：是 2：否" />
        </el-form-item>
        <el-form-item label=" 发货物流id" prop="logisticsId">
          <el-input v-model="form.logisticsId" placeholder="请输入 发货物流id" />
        </el-form-item>
        <el-form-item label="人工退款金额" prop="manualRefundMoney">
          <el-input v-model="form.manualRefundMoney" placeholder="请输入人工退款金额" />
        </el-form-item>
        <el-form-item label="人工退款操作人" prop="manualRefundOperator">
          <el-input v-model="form.manualRefundOperator" placeholder="请输入人工退款操作人" />
        </el-form-item>
        <el-form-item label=" 收货人姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入 收货人姓名" />
        </el-form-item>
        <el-form-item label=" 订单金额" prop="orderAmount">
          <el-input v-model="form.orderAmount" placeholder="请输入 订单金额" />
        </el-form-item>
        <el-form-item label=" 订单号" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入 订单号" />
        </el-form-item>
        <el-form-item label=" 父订单(支付订单)id" prop="payId">
          <el-input v-model="form.payId" placeholder="请输入 父订单(支付订单)id" />
        </el-form-item>
        <el-form-item label=" 收货人手机号" prop="phoneNum">
          <el-input v-model="form.phoneNum" placeholder="请输入 收货人手机号" />
        </el-form-item>
        <el-form-item label=" 用户支付订单号" prop="platformPayOrder">
          <el-input v-model="form.platformPayOrder" placeholder="请输入 用户支付订单号" />
        </el-form-item>
        <el-form-item label=" 申请退款原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入 申请退款原因" />
        </el-form-item>
        <el-form-item label=" 退货物流id" prop="refundLogisticsId">
          <el-input v-model="form.refundLogisticsId" placeholder="请输入 退货物流id" />
        </el-form-item>
        <el-form-item label="申请退款备注" prop="refundNotes">
          <el-input v-model="form.refundNotes" placeholder="请输入申请退款备注" />
        </el-form-item>
        <el-form-item label="退款备注图片" prop="refundNotesImages">
          <el-input v-model="form.refundNotesImages" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="退款订单号" prop="refundOrderNum">
          <el-input v-model="form.refundOrderNum" placeholder="请输入退款订单号" />
        </el-form-item>
        <el-form-item label="退款完成时间" prop="refundTime">
          <el-date-picker clearable
            v-model="form.refundTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择退款完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remake">
          <el-input v-model="form.remake" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="订单状态 1:待付款 2:待发货 3:待收货 4:订单完成 5:已取消" prop="status">
          <el-select v-model="form.status" placeholder="请选择订单状态 1:待付款 2:待发货 3:待收货 4:订单完成 5:已取消">
            <el-option
              v-for="dict in dict.type.app_goods_order_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label=" 任务id" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入 任务id" />
        </el-form-item>
        <el-form-item label=" 交易金额(实付金额)" prop="transactionAmount">
          <el-input v-model="form.transactionAmount" placeholder="请输入 交易金额(实付金额)" />
        </el-form-item>
        <el-form-item label="交易成功时间" prop="transactionTime">
          <el-date-picker clearable
            v-model="form.transactionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择交易成功时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label=" 用户id" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入 用户id" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改购物订单对话框 -->
    <el-dialog :title="title" :visible.sync="wlOpen" width="500px" append-to-body>
      <el-form ref="wlForm" :model="wlForm" :rules="wlRules" label-width="80px">
        <el-form-item label="物流名称" prop="logisticsName">
          <el-input v-model="wlForm.logisticsName" placeholder="请输入 物流名称" />
        </el-form-item>
        <el-form-item label="物流单号" prop="logisticsNum">
          <el-input v-model="wlForm.logisticsNum" placeholder="请输入 物流单号" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSendForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrder, getOrder, delOrder, addOrder, updateOrder,send } from "@/api/business/order";

export default {
  name: "Order",
  dicts: ['app_goods_order_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 购物订单表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      wlOpen:false,
      daterange:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        addTime: null,
        address: null,
        addressId: null,
        anchorId: null,
        auditFailureReason: null,
        businessId: null,
        businessLogo: null,
        businessName: null,
        goodsChannelId: null,
        isManualRefund: null,
        logisticsId: null,
        manualRefundMoney: null,
        manualRefundOperator: null,
        manualRefundType: null,
        name: null,
        orderAmount: null,
        orderNum: null,
        payId: null,
        phoneNum: null,
        platformPayOrder: null,
        quitStatus: null,
        reason: null,
        refundLogisticsId: null,
        refundNotes: null,
        refundNotesImages: null,
        refundOrderNum: null,
        refundTime: null,
        refundType: null,
        remake: null,
        status: null,
        taskId: null,
        transactionAmount: null,
        transactionTime: null,
        uid: null,
        beginTime:null,
        endTime:null
      },
      // 表单参数
      form: {},
      wlForm:{},
      // 表单校验
      rules: {
      },
      // 表单校验
      wlRules: {
        logisticsName: [
          { required: true, message: "物流名称不能为空", trigger: "blur" }
        ],
        logisticsNum: [
          { required: true, message: "物流单号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    dateChage(val){
      if(undefined != val && null != val && val.length > 1){
        this.queryParams.beginTime = val[0];
        this.queryParams.endTime = val[1];
      }else{
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
    },
    /** 查询购物订单列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.wlOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        addTime: null,
        address: null,
        addressId: null,
        anchorId: null,
        auditFailureReason: null,
        businessId: null,
        businessLogo: null,
        businessName: null,
        goodsChannelId: null,
        isManualRefund: null,
        logisticsId: null,
        manualRefundMoney: null,
        manualRefundOperator: null,
        manualRefundType: null,
        name: null,
        orderAmount: null,
        orderNum: null,
        payId: null,
        phoneNum: null,
        platformPayOrder: null,
        quitStatus: null,
        reason: null,
        refundLogisticsId: null,
        refundNotes: null,
        refundNotesImages: null,
        refundOrderNum: null,
        refundTime: null,
        refundType: null,
        remake: null,
        status: null,
        taskId: null,
        transactionAmount: null,
        transactionTime: null,
        uid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.daterange = [];
      this.queryParams.beginTime = null;
      this.queryParams.endTime = null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加购物订单";
    },
    /** 修改按钮操作 */
    handleSend(row) {
      this.wlForm = {
        id:row.id,
        logisticsName:null,
        logisticsNum:null
      };
      this.wlOpen = true;
      this.title = "发货";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改购物订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrder(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitSendForm() {
      this.$refs["wlForm"].validate(valid => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: '加载中',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          });
          setTimeout(function (){loading.close();},1000);
          send(this.wlForm).then(response => {
            this.$modal.msgSuccess("发货成功");
            this.wlOpen = false;
            this.getList();
            loading.close();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除购物订单编号为"' + ids + '"的数据项？').then(function() {
        return delOrder(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/order/export', {
        ...this.queryParams
      }, `order_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
