<template>
  <div class="refund">
    <el-form ref="searchFrom" :model="searchFrom" label-width="100px" inline>
          <el-form-item label="用户昵称">
            <el-input v-model="searchFrom.nickname"></el-input>
          </el-form-item>
          <el-form-item label="用户ID">
            <el-input v-model="searchFrom.user_id"></el-input>
          </el-form-item>
          <el-form-item label="订单编号">
            <el-input v-model="searchFrom.out_trade_no"></el-input>
          </el-form-item>
          <el-form-item label-width="10px">
            <el-button type="primary" @click="getRefundList">搜索</el-button>
            <el-button type="primary" plain @click="exportTable" v-if="$store.state.export">导出</el-button>
          </el-form-item>
    </el-form>
    <el-tabs v-model="searchFrom.status" type="card" style="background: white;padding-top: 10px" @tab-click="getRefundList">
      <el-tab-pane label="退款成功" name="1"/>
      <el-tab-pane label="退款中" name="0"/>
      <el-tab-pane label="退款失败" name="2"/>
      <el-tab-pane label="全部" name="-1"/>
    </el-tabs>
    <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="refund_no"
        label="退款编号">
      </el-table-column>
      <el-table-column
        prop="out_trade_no"
        label="订单号">
      </el-table-column>
      <el-table-column
        label="用户信息"
        min-width="140"
        className="userInfo"
      >
        <template slot-scope="scope">
          <img class="avatar" :src="scope.row.user_avatar">
          {{scope.row.user_nickname}}(ID:{{scope.row.user_id}})
        </template>
      </el-table-column>
      <el-table-column
        prop="order_money"
        label="订单金额">
        <template slot-scope="scope">
          {{parseFloat(scope.row.order_money / 100)}}元
        </template>
      </el-table-column>
      <el-table-column
        prop="refund_money"
        label="退款金额">
        <template slot-scope="scope">
          {{parseFloat(scope.row.refund_money / 100)}}元
        </template>
      </el-table-column>
      <el-table-column
        prop="desc"
        label="备注">
      </el-table-column>
      <el-table-column
        label="退款状态">
        <template slot-scope="scope">
          {{scope.row|filterStatus}}
        </template>
      </el-table-column>
      <el-table-column
        prop="success_at"
        label="退款成功时间" sortable>
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="80"
        v-if="!$store.state.readonly"
      >
        <template slot-scope="scope">
          <el-button
            @click="editComment(scope.row.id, scope.row.desc)"
            type="warning"
            plain
            size="mini">
            备注
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <page :nowPage="nowPage" :total="total" @pageChange="onPageChange" @sizeChange="onSizeChange"/>
  </div>
</template>

<script>
  import {getRefundListApi,editOrderDescApi,exportExcelApi} from "../../service/api";
  import page from '../framework/page'
  export default {
    name: "index",
    components:{
      page
    },
    data(){
      return {
        nowPage: 1,
        total: 0,
        limit: 10,
        refundTypeList:[
          {
            id:0,
            value:'退款中'
          },
          {
            id:1,
            value:'退款成功'
          },
          {
            id:2,
            value:'退款失败'
          },
        ],
        searchFrom: {
          nickname:'',
          user_id: '',
          out_trade_no:'',
          status:'1'
        },
        list: []
      }
    },
    filters:{
      filterStatus:function (value) {
        let msg = '';
        if(value.status === 0){
          msg = '退款中'
        }else if(value.status === 1){
          msg = '退款成功'
        }else if(value.status === 2){
          msg = '退款失败'+'('+value.err_msg+")"
        }
        return msg;
      }
    },
    mounted(){
      this.getRefundList()
    },
    methods: {
      editComment(id, desc) {
        this.$prompt('', '编辑备注', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputType:'textarea',
          inputValue: desc
        }).then(({ value }) => {
          editOrderDescApi(id,'refund',{desc: value}).then(res=>{
            this.$message({
              type: 'success',
              message: '编辑备注成功'
            });
            this.getRefundList()
          });
        })
      },
      onPageChange(val){
        this.nowPage = val;
        this.getRefundList()
      },
      onSizeChange(val){
        this.nowPage = 1;
        this.limit = val;
        this.getRefundList()
      },
      getRefundList(){
        let json = {
          limit: this.limit,
          page: this.nowPage,
        };
        if(this.searchFrom.nickname){
            json.nickname = this.searchFrom.nickname
        }
        if(this.searchFrom.user_id){
          json.user_id = this.searchFrom.user_id
        }
        if(this.searchFrom.status && this.searchFrom.status !== '-1'){
          json.status = this.searchFrom.status
        }
        if(this.searchFrom.out_trade_no){
          json.out_trade_no = this.searchFrom.out_trade_no
        }
        getRefundListApi(json).then((res)=>{
          this.total = res.total;
          this.list = res.list
        })
      },
      exportTable(){
        let json = {};
        if(this.searchFrom.nickname){
          json.nickname = this.searchFrom.nickname
        }
        if(this.searchFrom.user_id){
          json.user_id = this.searchFrom.user_id
        }
        if(this.searchFrom.status){
          json.status = this.searchFrom.status
        }
        if(this.searchFrom.out_trade_no){
          json.out_trade_no = this.searchFrom.out_trade_no
        }
        exportExcelApi('/api/admin/refund/export',json,'退款列表')
      },
    }
  }

</script>

<style scoped>
  .refund{
    padding: 20px 0;
  }
  .avatar {
    width:50px;
    height: 50px;
    border-radius: 50%;
  }
</style>
<style>
  .userInfo >div{
    display: flex;
    flex-flow: row nowrap;
    justify-content: flex-start;
    align-items: center;
  }
</style>
