<template>
  <div class="app-container mod-order-order">
    <!-- 筛选条件 -->
    <div class="screen-container">
      <el-form ref="screenForm" :model="screenForm" label-width="70px" size="small" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="订单号" prop="orderId">
              <el-input v-model="screenForm.orderId" placeholder="请输入订单号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="售后编号" prop="orderRefundId">
              <el-input v-model="screenForm.orderRefundId" placeholder="请输入售后编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="会员昵称" prop="userName">
              <el-input v-model="screenForm.userName" placeholder="请输入会员昵称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="screenForm.phone" placeholder="请输入手机号"></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="网点名称" prop="websitId">
              <el-select v-model="screenForm.websitId" placeholder="全部" filterable>
                <el-option label="全部" value=""></el-option>
                <el-option :label="item.name" :value="item.id" v-for="(item, index) in websiteList" :key="index"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <!--          <el-col :xs="24" :sm="12" :lg="6">-->
          <!--            <el-form-item label="当前状态" prop="orderStatus">-->
          <!--              <el-select v-model="screenForm.orderStatus" placeholder="全部">-->
          <!--                <el-option label="全部" value=""></el-option>-->
          <!--                <el-option :label="item.label" :value="item.value" v-for="(item, index) in screen_status" :key="index"></el-option>-->
          <!--              </el-select>-->
          <!--            </el-form-item>-->
          <!--          </el-col>-->

          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="申请时间" prop="createDate">
              <el-date-picker
                v-model="screenForm.createDate"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="处理时间" prop="examineDate">
              <el-date-picker
                v-model="screenForm.examineDate"
                type="datetimerange"
                value-format="yyyy-MM-dd HH:mm:ss"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="6" class="tr">
            <el-form-item label="">
              <el-button size="small" type="primary" @click="submitScreenForm">搜索</el-button>
              <el-button size="small" @click="resetScreenForm">清空</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>

    <div class="tabs-container order-tabs-container clearfix">
      <div class="fl">
        <el-tabs v-model="tabCurrent" type="card" @tab-click="changeTabs">
          <el-tab-pane label="全部" name="ALL"></el-tab-pane>
          <el-tab-pane label="待商家处理" name="DSJCL"></el-tab-pane>
          <el-tab-pane label="待买家处理" name="DMJCL"></el-tab-pane>
          <el-tab-pane label="待商家收货" name="DSJSH"></el-tab-pane>
          <el-tab-pane label="已完成" name="OVER"></el-tab-pane>
        </el-tabs>
      </div>
      <div class="fr">
        <el-button v-if="$restrict('exp')" size="small" type="primary" @click="handleExport">导出报表</el-button>
      </div>
    </div>
    <div class="order-main-container">
      <div class="table-top">
        <el-row style="width:100%">
          <el-col :span="6"><span class="item product">商品信息</span></el-col>
          <el-col :span="3" style="text-align: right"><span class="item">单价(元)/数量</span></el-col>
          <el-col :span="3" style="text-align: center"><span class="item">买家信息</span></el-col>
          <el-col :span="3" style="text-align: center"><span class="item">订单金额</span></el-col>
          <el-col :span="3" style="text-align: center"><span class="item">退款金额</span></el-col>
          <el-col :span="3" style="text-align: center"><span class="item">维权类型/状态</span></el-col>
          <el-col :span="3" style="text-align: center"><span class="item">操作</span></el-col>
        </el-row>
      </div>
      <div class="order-item" v-for="(order, index) in dataList" :key="order.orderRefundId">
        <div class="order-top">
          <el-row>
            <el-col :xs="24" :sm="24" :lg="20">
              <span><b>售后编号</b>：{{order.orderRefundId}}</span>
              <span><b>申请时间</b>：{{order.createTime}}</span>
              <span><b>商户</b>：{{order.companyWechatName || '无'}}</span>
              <span><b>发货状态</b>：{{order.logisticsNo ? '已发货' : '未发货'}}</span>
            </el-col>
            <el-col :xs="24" :sm="24" :lg="4" style="text-align: right;">
              <el-link type="primary" v-if="$restrict('orderNote')" :underline="false" @click="showRemark(order.orderRefundId, order.remark)">订单备注</el-link>
              <el-link type="primary" v-if="$restrict('detail')" :underline="false" @click="showDetail(order.orderRefundId)">查看详情</el-link>
              <!-- <el-link type="primary" :underline="false" @click="queryWorkOrder(order.orderId)">订单工单</el-link> -->
            </el-col>
          </el-row>
        </div>
        <div class="order-content">
          <el-row style="width:100%">
            <el-col :span="9" style="height: 100%;">
              <div class="col-item-pro">
                <div v-if="order.examineStatus === 'FAIL'">
                  <div v-for="orderItem in order.orderDetails" :key="orderItem.orderDetailId">
                    <div class="pro-item">
                      <div class="left">
                        <div class="image">
                          <img :src="orderItem.imgUrl" style="height:80px;width: 80px;">
                        </div>
                        <div class="main">
                          <span>{{orderItem.goodsName}}</span>
                          <span class="spec">{{orderItem.goodsSpecName}}-{{orderItem.goodsSpecValue}}</span>
                        </div>
                      </div>
                      <div class="right">
                        <span>￥{{orderItem.price}}</span>
                        <span>x{{orderItem.num}}</span>
                      </div>
                    </div>
                  </div>
                </div>
                <div v-else>
                  <div v-for="orderItem in order.orderDetails" :key="orderItem.orderDetailId">
                    <div class="pro-item" v-if="orderItem.refund">
                      <div class="left">
                        <div class="image">
                          <img :src="orderItem.imgUrl" style="height:80px;width: 80px;">
                        </div>
                        <div class="main">
                          <span>{{orderItem.goodsName}}</span>
                          <span class="spec" v-if="orderItem.goodsSpecName === '默认' && orderItem.goodsSpecValue === '默认'">默认</span>
                          <span class="spec" v-else>{{orderItem.goodsSpecName}}-{{orderItem.goodsSpecValue}}</span>
                        </div>
                      </div>
                      <div class="right">
                        <span>￥{{orderItem.price}}</span>
                        <span>x{{orderItem.refundNum}}</span>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </el-col>
            <el-col :span="3" style="height: 100%;">
              <div class="col-item">
                <div>{{ order.userName }}</div>
                <div>{{ order.phone }}</div>
              </div>
            </el-col>
            <el-col :span="3" style="height: 100%;">
              <div class="col-item">
                <div class="totalprice">￥{{ order.payAmount }}</div>
              </div>
            </el-col>
            <el-col :span="3" style="height: 100%;">
              <div class="col-item">
                <div class="totalprice">￥{{ order.refundAmount }}</div>
              </div>
            </el-col>
            <el-col :span="3" style="height: 100%;">
              <div class="col-item">
                <div>{{order.refundType | REFUND_CURRENT_TYPE_FILTER}}</div>
                <div>{{ order.orderStatus | ORDER_REFUND_CURRENT_STATUS_FILTER }}</div>
              </div>
            </el-col>
            <el-col :span="3" style="height: 100%;">
              <div class="col-item">
                <div class="operate" v-if="order.orderStatus === 'DSJCL'">
                  <div>
                    <el-button v-if="$restrict('agree')" slot="reference" type="text" size="small" @click="changeExamineStatus(order.orderRefundId,'OK')">同意</el-button>
                  </div>
                  <div>
                    <el-button v-if="$restrict('reject')" slot="reference" type="text" size="small" @click="changeExamineStatus(order.orderRefundId,'FAIL')">拒绝</el-button>
                  </div>
                  <div>
                    <!-- toWorkOrderDetail(order.orderId) -->
                    <el-button type="text" size="small" v-if="order.toWorkOrder && $restrict('workOrder_detail')" @click="queryWorkOrder(order.orderId)">查看工单</el-button>
                  </div>
                </div>
                <div class="operate" v-if="order.orderStatus === 'DSJSH'">
                  <div v-if="$restrict('confirmReceipt')">
                    <el-popconfirm title="确定要收货吗？" @confirm="receiveProd(order.orderRefundId)">
                      <el-button slot="reference" type="text" size="small">确认收货</el-button>
                    </el-popconfirm>
                  </div>
                  <div v-if="order.logisticsNo && $restrict('viewLogistics')">
                    <el-button type="text" size="small" @click="queryLogistics(order.logisticsNo)">查看物流</el-button>
                  </div>
                  <div>
                    <el-button type="text" size="small" v-if="order.toWorkOrder && $restrict('workOrder_detail')" @click="toWorkOrderDetail(order.orderId)">查看工单</el-button>
                  </div>
                </div>
              </div>
            </el-col>
          </el-row>
        </div>
        <div class="order-remark buyer" v-if="order.buyerMsg">买家留言：{{order.buyerMsg}}</div>
        <div class="order-remark saler" v-if="order.remark">商家备注：{{order.remark}}</div>
      </div>
      <div class="no-data" v-if="dataList.length <= 0">暂无订单</div>
    </div>
    <div class="pagination clearfix">
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal">
        </el-pagination>
      </div>
    </div>
    <el-dialog :title="'订单备注'" :visible.sync="remarkVisible" :show-close="false" width="50%" :close-on-click-modal="false">
      <el-input
        type="textarea"
        :autosize="{ minRows: 8, maxRows: 16}"
        placeholder="请输入备注内容"
        v-model="remark"
        maxlength="100"
        show-word-limit>
      </el-input>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="saveRemark">保 存</el-button>
        <el-button @click="cancelRemark">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="'物流信息'" :visible.sync="logisticsVisible" :show-close="false" width="80%" :close-on-click-modal="false">
      <el-table
        :data="logistics"
        style="width: 100%"
        size="mini">
        <el-table-column prop="logisticsNo" label="快递单号" align="center"></el-table-column>
        <el-table-column prop="comName" label="物流公司" align="center"></el-table-column>
        <el-table-column prop="time" label="物流时间" align="center"></el-table-column>
        <el-table-column prop="state" label="物流状态" align="center">
          <template slot-scope="scope">
            {{ scope.row.state | EXPRESS_CURRENT_STATE_FILTER }}
          </template>
        </el-table-column>
        <el-table-column prop="context" label="物流详情" align="center"></el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button @click="logisticsVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getRefundList, saveRefundRemark, changeExamineStatus, receiveProd, getWebsiteList} from "@/api/order"
import {getExpress} from "@/api/common"
import {downloadFiles} from '@/utils/util'
import '@/styles/order-list.scss'

export default {
  name: "order_refund",
  data() {
    return {
      screenForm: { // 筛选表单数据
        orderId: '', // 订单号
        orderRefundId: '', // 售后编号
        userName: '', // 会员昵称
        phone: '', // 手机号
        websitId: '', // 网点名称
        createDate: '', // 创建时间
        examineDate: '', // 支付时间
        orderStatus: '',
      },
      tabCurrent: 'ALL',
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [],
      remarkVisible: false,
      editOrderId: '',
      remark: '', // 订单备注
      logisticsVisible: false,
      logistics: [],
      detail: {},
      websiteList: [],
    }
  },
  created() {
    this.getList();
    this.getWebsiteList();
	if(this.$route.query.id){
		this.showDetail(this.$route.query.id)
	}
  },
  methods: {
    queryWorkOrder(id) {
		this.$router.push({
			name: 'workOrderPool',
			params: {
				pageType: 'saleOrderId',
				pageCode: id,
			},
		})
    },
    changeTabs(tab, event) {
      // this.$refs.screenForm.resetFields();
      this.screenForm.orderStatus = this.tabCurrent === 'ALL' ? '' : tab.$options.propsData.name
      this.currentPage = 1;
      this.getList();
    },
    // 获取数据列表
    getList () {
      this.listLoading = true;
      let pageData = {
        pageNum: this.currentPage,
        pageSize: this.pageSize
      }
      let screenData = {
        orderId: this.screenForm.orderId, // 订单号
        orderRefundId: this.screenForm.orderRefundId, // 售后编号
        userName: this.screenForm.userName, // 会员昵称
        phone: this.screenForm.phone, // 手机号
        websitId: this.screenForm.websitId,
        orderStatus: this.screenForm.orderStatus, // 状态
        startCreateTime: this.screenForm.createDate ? this.screenForm.createDate[0] : '', // 创建时间
        endCreateTime: this.screenForm.createDate ? this.screenForm.createDate[1] : '', // 创建时间
        startExamineTime: this.screenForm.examineDate ? this.screenForm.examineDate[0] : '', // 处理时间
        endExamineTime: this.screenForm.examineDate ? this.screenForm.examineDate[1] : '', // 处理时间
      };
      let params = {};
      Object.assign(params, screenData, pageData);

      getRefundList(params).then(res => {
        this.dataList = res.data.records;
        this.listTotal = res.data.total;
        this.listLoading = false;
      })
    },
    // 查看详情
    queryDetail (orderRefundId) {
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getList();
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    // 提交筛选表单
    submitScreenForm() {
      this.currentPage = 1;
      this.getList();
    },
    // 重置筛选表单
    resetScreenForm() {
      this.$refs.screenForm.resetFields();
      this.currentPage = 1;
      this.getList();
    },

    // 网点列表
    getWebsiteList() {
      getWebsiteList({
        pageNum: 1,
        pageSize: 1000
      }).then(res => {
        this.websiteList = res.data.records;
      })
    },

    showRemark(orderId, remark) {
      this.remarkVisible = true
      this.editOrderId = orderId
      this.remark = remark
    },
    cancelRemark() {
      this.remarkVisible = false
      this.editOrderId = ''
      this.remark = ''
    },
    saveRemark() {
      const params = {
        orderRefundId: this.editOrderId,
        remark: this.remark
      }
      saveRefundRemark(params).then(() => {
        this.cancelRemark();
        this.getList();
        this.$successMsg('保存成功');
      })
    },
    // 变更维权处理状态
    changeExamineStatus(orderRefundId, examineStatus) {
      this.$prompt('请输入卖家留言', examineStatus == 'OK' ? '同意':'拒绝', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        changeExamineStatus({
          orderRefundId,
          examineStatus,
          refundMessage: value
        }).then(() => {
          this.getList();
          this.$successMsg('操作成功');
        })
      }).catch(() => {});
    },
    // 查看物流
    queryLogistics(logisticsNo) {
      this.logisticsVisible = true
      getExpress({ logisticsNo }).then(res => {
        this.logistics = res.data
      })
    },
    // 查看详情
    showDetail(orderRefundId) {
      // const orderRefundId = this.dataList[index].orderRefundId
      // let {href} = this.$router.resolve({path: `/order/order_refund_detail?orderRefundId=${orderRefundId}`});
      // window.open(href, '_blank');
      this.$router.push({
        name:"order_refund_detail",
        query: {
          orderRefundId
        }
      })
    },
    receiveProd(orderRefundId) {
      receiveProd({orderRefundId}).then(() => {
        this.getList();
        this.$successMsg('操作成功');
      })
    },

    // 查看工单
    toWorkOrderDetail(orderId) {
      // let {href} = this.$router.resolve({path: `/order/workOrder_detail?orderId=${orderId}`});
      // window.open(href, '_blank');
      this.$router.push({
        name:"workOrder_detail",
        query: {
          orderId
        }
      })
    },

    // 导出
    handleExport() {
      let screenData = {
        orderId: this.screenForm.orderId, // 订单号
        orderRefundId: this.screenForm.orderRefundId, // 售后编号
        userName: this.screenForm.userName, // 会员昵称
        phone: this.screenForm.phone, // 手机号
        websitId: this.screenForm.websitId,
        orderStatus: this.screenForm.orderStatus, // 状态
        startCreateTime: this.screenForm.createDate ? this.screenForm.createDate[0] : '', // 创建时间
        endCreateTime: this.screenForm.createDate ? this.screenForm.createDate[1] : '', // 创建时间
        startExamineTime: this.screenForm.examineDate ? this.screenForm.examineDate[0] : '', // 处理时间
        endExamineTime: this.screenForm.examineDate ? this.screenForm.examineDate[1] : '', // 处理时间
        pageNum: this.currentPage,
        pageSize: this.pageSize
      };
      downloadFiles('order/refund/downLoadExcel', screenData);
    },
  }
}
</script>
