<template>
  <div class="refund">
    <el-form ref="searchFrom" :model="searchFrom" label-width="100px" inline>
          <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="使用状态">
            <el-select v-model="searchFrom.status" placeholder="请选择" @change="getList" clearable>
              <el-option
                v-for="item in useTypeList"
                :key="item.id"
                :label="item.value"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getList">搜索</el-button>
          </el-form-item>
    </el-form>
    <el-table
      :data="list"
      style="width: 100%">
      <el-table-column
        prop="coupon_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
        label="优惠券金额">
        <template slot-scope="scope">
          {{parseFloat(scope.row.money / 100)}}元
        </template>
      </el-table-column>
      <el-table-column
        label="使用状态">
        <template slot-scope="scope">
          {{scope.row.status|filterStatus}}
        </template>
      </el-table-column>
      <el-table-column
        prop="desc"
        label="备注"  >
      </el-table-column>
      <el-table-column
        prop="deadline_at"
        label="过期时间" :formatter="dayline" >
      </el-table-column>
      <el-table-column
        prop="use_at"
        label="使用时间" sortable>
      </el-table-column>
      <el-table-column
        label="操作"
        min-width="150"
        v-if="!$store.state.readonly">
        <template slot-scope="scope">
          <el-button
            @click="editTime(scope.row)"
            type="text"
            plain
            size="mini">
            修改过期时间
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="修改过期时间"
      :visible.sync="dialogObj.show">
       <el-date-picker
      v-model="value1"
      type="datetime"
      value-format="yyyy-MM-dd-HH-mm-ss"
      placeholder="选择日期时间">
    </el-date-picker>
    <span slot="footer" class="dialog-footer">
        <el-button @click="dialogObj.show = false">取 消</el-button>
        <el-button type="primary" @click="onConfirm">确 定</el-button>
      </span>
    </el-dialog>
    <page :nowPage="nowPage" :total="total" @pageChange="onPageChange" @sizeChange="onSizeChange"/>
  </div>
</template>

<script>
  import {getCouponListApi,editCouponDeadlineApi} from "../../service/api";
  import page from '../framework/page'
  export default {
    name: "index",
    components:{
      page
    },
    data(){
      return {
        nowPage: 1,
        total: 0,
        limit: 10,
        useTypeList:[
          {
            id:0,
            value:'未使用'
          },
          {
            id:1,
            value:'已使用'
          },
          {
            id:2,
            value:'禁用'
          }
        ],
        searchFrom: {
          user_id: '',
          out_trade_no:'',
          status:0
        },
        list: [],
        dialogObj:{
          show:false
        },
        value1:'',
        rowId:''
      }
    },
    filters:{
      filterStatus:function (value) {
        let msg = '';
        if(value === 0){
          msg = '未使用'
        }else if(value === 1){
          msg = '已使用'
        }else if(value === 2){
          msg = '禁用'
        }
        return msg;
      }
    },
    mounted(){
      this.getList()
    },
    methods: {
      editTime(row){
        this.dialogObj.show = true
        this.value1 = new Date(row.deadline_at)
        this.rowId = row.id
        console.log(row)
      },
      onConfirm(){
        console.log(this.value1)
        editCouponDeadlineApi({deadline_at:this.value1},this.rowId).then(res =>{
          this.dialogObj.show = false
          this.getList()
        })
      },
      dayline(val){
        if(val.deadline_at=='0000-00-00 00:00:00'){
          return '永不过期'
        }else{
          return val.deadline_at
        }
      },
      onPageChange(val){
        this.nowPage = val;
        this.getList()
      },
      onSizeChange(val){
        this.nowPage = 1;
        this.limit = val;
        this.getList()
      },
      getList(){
        let json = {
          limit: this.limit,
          page: this.nowPage,
        };
        if(this.searchFrom.user_id){
          json.user_id = this.searchFrom.user_id
        }
        if(this.searchFrom.status || this.searchFrom.status === 0){
          json.status = this.searchFrom.status
        }
        if(this.searchFrom.out_trade_no){
          json.out_trade_no = this.searchFrom.out_trade_no
        }
        getCouponListApi(json).then((res)=>{
          this.total = res.total;
          this.list = res.list ? res.list : []
        })
      }
    }
  }

</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>
