<template>
  <div v-loading="loading">
    <div class="heior_box">
      <el-button size="small" type="primary" icon="el-icon-sold-out" @click="dataExport" v-if="$local.isRoot()">导出</el-button>
      <div class="search-box">
         <el-select v-model="searchModel.ProductId" placeholder="选择产品" clearable size="small">
          <el-option v-for="item in products" :key="item.Name" :label="item.Name" :value="item.Id"></el-option>
        </el-select>
        <el-select
          v-model="searchModel.OrderTypes"
          multiple
          collapse-tags
          clearable
          size="small"
          placeholder="选择类型"
        >
          <el-option v-for="item in types" :key="item.text" :label="item.text" :value="item.value"></el-option>
        </el-select>
        <el-date-picker
          size="small"
          v-model="dateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
        <el-input v-model="searchModel.keyWord" placeholder="请输入订单号、订单名称、用户名" clearable size="small">
          <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
        </el-input>
      </div>
    </div>
    <el-table :data="retData.Data" @filter-change="filterHandler" row-key="Id" border size="medium" :height="$getHeight(100)">
      <el-table-column prop="CreateTime" label="创建日期" width="180"></el-table-column>
      <el-table-column prop="OrderNo" label="订单号" width="220"></el-table-column>
      <!-- <el-table-column prop="OrderType" label="类型"  :formatter="typeFormat"></el-table-column> -->
      <el-table-column prop="UserName" label="用户" >
        <template slot-scope="scope"> 
          <phoneHide :text="scope.row.UserName"></phoneHide>
        </template>
      </el-table-column>
      <el-table-column prop="ProductName" label="产品"></el-table-column>
      <el-table-column prop="PackageName" label="套餐"></el-table-column>
      <!-- <el-table-column prop="DayPrice" label="单价"></el-table-column> -->
      <el-table-column prop="ConnectCount" label="连接数"></el-table-column>     
      <el-table-column prop="Accounts" label="账号"></el-table-column>
      <el-table-column prop="PaymentAmount" label="实付金额"></el-table-column> 
      <el-table-column prop="RefundAmount" label="退款金额"></el-table-column>    
       <el-table-column prop="RefundRestTime" label="剩余时间"></el-table-column>    
       <el-table-column prop="IsAutoRefund" label="自动退款" :formatter="autoFormat"></el-table-column>    
       <el-table-column prop="BackAmount" label="返点"></el-table-column>     
       <el-table-column prop="RefundReason" label="退款原因"></el-table-column>
       <el-table-column prop="OrderState" label="退款状态">
         <template slot-scope="scope">          
           <span :class="{red:scope.row.OrderState==30}"> {{refundTypeFormat(scope.row.OrderState)}}</span>
           <span v-if="scope.row.OrderState==30" class="handel cursor edit" @click="showRefund(scope.row)">处理</span>
         </template>
         </el-table-column>     

    </el-table>
    <div class="block">
      <el-pagination
        v-if="retData.Data.length>0"
        @current-change="get"
        :current-page.sync="searchModel.PageIndex"
        :page-size="50"
        layout="total,prev, pager, next"
        :total="retData.TotalCount"
      ></el-pagination>
    </div>

    <el-dialog title="退款处理" :visible.sync="refundModel.Show"  width="300px">
      <el-form :model="refundModel" ref="inputForm" label-width="80px">
        <el-form-item prop="Remark" label="备注说明">
          <el-input v-model="refundModel.Remark" size="small"></el-input>
        </el-form-item>   
        <el-form-item prop="BackAmount" label="返点">
          <el-input v-model="refundModel.BackAmount" size="small" type="number"></el-input>
        </el-form-item>        
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('inputForm')">取 消</el-button>
        <el-button type="primary" @click="doRefund('inputForm')">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  name: "treeTable",
  components: {
     phoneHide: () => import("@/components/common/phonehide")
  },
  data() {
    return {
      loading: false,     
      retData: { TotalCount: 0, Data: [] },
      dateRange:[],
      searchModel: {
        PageIndex: 1,
        keyWord: "",
        Status: 0,
        PayType: 0,
        OrderTypes:[],
        ProductId:null,
        Btime: "",
        Etime: ""
      },
      refundModel:{
        Id:0,
        BackAmount:0,
        Remark:'',
        Show:false,
      },
      types: [
        { text: "新开", value: 1 },
        { text: "批量新开", value: 2 },
        { text: "续费", value: 3 },
        { text: "批量续费", value: 4 },
        { text: "老账号认证", value: 100 },
        { text: "测试", value: 200 }
      ],
      typeFormat(row, column, cellValue, index){
        return "退款"        
       },
       autoFormat(row, column, cellValue, index){
        return cellValue&&cellValue==1?"是":"否";       
       },
        refundTypeFormat(orderStatus){
        switch (orderStatus) {
          case 30:
            return "未自动退款";
          case 40:
            return "已人工处理";
          case 50:
            return "已自动退款";
          default:
            return "";        
        }
        return "";
       },
    }
  },
  created() {
     this.$store.commit("setCurrentNav","订单管理>>退款订单")
    this.get();
     this.getProduct();
  },
  methods: {
    search(){
      this.searchModel.PageIndex=1;
      this.get();
    },
    get() {
       if (this.dateRange && this.dateRange.length == 2) {
        this.searchModel.Btime = this.dateRange[0];
        this.searchModel.Etime = this.dateRange[1];
      }
      this.$api.get("course/v1/order/RefundOrders", this.searchModel).then(res => {
        this.retData = res;
      });
    },
        getProduct() {
      this.loading = true;
      this.$api.get("course/v1/product/page", this.searchModel).then(res => {
        this.products = res.Data;
        this.loading = false;
      });
    },
    filterHandler(filters) {
      console.log(filters)
      var values=Object.values(filters);
      console.log(...values)
      values.forEach(m=>{
         this.searchModel.OrderTypes.push(...m)
      });     
      this.get(); 
    },
    dataExport(){
      this.$api.getDownloadFile("/course/v1/order/ExportRefundOrders", this.searchModel);
    },
    showRefund(item){
      this.refundModel.Show = true;   
      this.refundModel.Id=item.Id;
    },
     resetForm(formName) {
      this.$refs[formName].resetFields();    
      this.refundModel.Show = false;   
    },
    doRefund(formName){
      this.$api.post("course/v1/order/RefundProcess", this.refundModel).then(res => {
         this.get(); 
         this.resetForm(formName)
      })
    }
  }
};
</script>
<style scoped>
.red{
  color: red;
}
</style>
