<template>
  <div class="withdraw app-container">
   
   <div class="data-box">

    <div class="left" v-loading="moneyLoading">
      <div class="item">
        <div class="label-box">
          <div class="text"> 帐户资金余额（元）</div>
            <el-tooltip class="item" effect="dark" content="交易完成的订单金额=可提现金额" placement="top-start">
            <i class="el-icon-warning-outline"></i>
          </el-tooltip>
        </div>

        <div class="value-box">
          <div class="text">{{money.ready}}</div>
           <el-button class="btn" size="small" type="primary" @click="handleWithdraw">提现</el-button>
        </div>
      </div>
       <div class="item">
        <div class="label-box">
          <div class="text">待结算金额（元）</div>
            <el-tooltip class="item" effect="dark" content="交易未完成的订单总金额，消费者确认收货/核销48小时后后将自动转入帐户资金余额" placement="top-start">
            <i class="el-icon-warning-outline"></i>
          </el-tooltip>
        </div>

        <div class="value-box">
          <div class="text">{{money.await}}</div>
        </div>
      </div>
       <div class="item">
        <div class="label-box">
          <div class="text"> 不可用现金额（元）</div>
            <el-tooltip class="item" effect="dark" content="提现中或者退款中的冻结帐户资金余额" placement="top-start">
            <i class="el-icon-warning-outline"></i>
          </el-tooltip>
        </div>

        <div class="value-box">
          <div class="text">{{money.freeze}}</div>
        </div>
      </div>

    </div>
    <div class="right">
       <el-select
            v-model="query.channel"
            size="small"
            class="item-width-200 ml10"
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
    </div>
   </div>
   
     <div class="search">
      <div>

          <el-input
            v-model="query.noOrGName"
            size="small"
            placeholder="请输入订单编号"
            class="item-width-200 "
          ></el-input>

         

          <el-date-picker
            size="small"
          v-model="date"
          value-format="timestamp"
          class="item-width-400 ml10"
          type="daterange"
          :default-time="['00:00:00','23:59:59']"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right">
      </el-date-picker>
      <el-button
          class="ml10"
          type="primary"
          size="small"
          icon="el-icon-search"
          @click="handleSearch"
        >
          搜索
        </el-button>
      </div>
    </div>

    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      v-loading="loading"
      style="width: 100%"
    >
     
      <el-table-column
        prop="payTime"
        align="center"
        label="订单时间"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="shopName"
        align="center"
        label="商品名称"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{scope.row.goodsInfo.activityName||scope.row.goodsInfo.goodsName}}
        </template>
      </el-table-column>
       <el-table-column
        align="center"
        label="交易订单号"
        prop="orderNo"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        align="center"
        label="金额(元)"
        show-overflow-tooltip
      >
      <template slot-scope="scope">
        <span class="green">+{{scope.row.platform}}</span>
        </template>
      </el-table-column>
  
       <el-table-column
        prop="createTime"
        align="center"
        label="状态"
        show-overflow-tooltip
      >
      <template slot-scope="scope">
        <span :class="[scope.row.transfer?'green':'red']"> {{scope.row.transfer ?'交易成功':'交易未完成'}}</span>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="query.page"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="query.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>


    <el-dialog title="提现" width="900px" :close-on-click-modal="false" :visible.sync="dialogFormVisible">
        <div class="notice">共 {{total1}}笔订单，提现金额 {{money.ready}}元</div>
    <el-table
        :data="tableData1"
        tooltip-effect="dark"
        border
        v-loading="loading1"
        style="width: 100%"
        >
        
        <el-table-column
            prop="createTime"
            align="center"
            label="订单时间"
            show-overflow-tooltip
        >
            <template slot-scope="scope">
            {{timeFormat(Number(scope.row.createTime) )}}
            </template>
        </el-table-column>
        <el-table-column
            prop="goodsName"
            align="center"
            label="商品名称"
            show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
            align="center"
            label="订单编号"
            prop="orderNo"
            show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
            prop="amount"
            align="center"
            label="金额"
            show-overflow-tooltip
        >
        <template slot-scope="scope">
            <span class="green"> +{{scope.row.amount}}</span>
            </template>
        </el-table-column>
        <!-- <el-table-column
            prop="createTime"
            align="center"
            label="状态"
            show-overflow-tooltip
        >
        <template slot-scope="scope">
            {{scope.row.transferAll?'交易成功':'交易未完成'}}
            </template>
        </el-table-column> -->
        </el-table>
        <div class="page-box">
        <el-pagination
            @size-change="handleSizeChange1"
            @current-change="handleCurrentChange1"
            background
            :current-page="query1.page"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="query1.size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total1"
        >
        </el-pagination>
        </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="!tableData1.length" :loading="btnLoading" @click="withdraw">全部提现</el-button>
      </div>
  </el-dialog>

  </div>
</template>
  
  <script>
  import {withdrawList,withdrawals,getDetail} from '@/api/order'
  import {getTransferList} from '@/api/payment'
  
  import {debounce,timeFormat} from '@/utils/index'
export default {
  name: "withdraw",
  data() {
    return {
      that:this,
      date:'',
      shopName:localStorage.getItem('shopName'),
      dialogFormVisible:false,
      tableData: [],
      tableData1: [],
      btnLoading: false,
      loading: false,
      loading1: false,
      moneyLoading: false,
       query: {
        noOrGName: "",
        startTime: "",
        endTime: "",
        channel:"ZhongShu",
        page: 1,
        size: 10,
      },
       query1: {
        channel:"",
        page: 1,
        size: 10,
      },
      total: 0,
      total1: 0,
      money:{
        await:0,
        freeze:0,
        ready:0
      },
      options: [
        {
          value: "ZhongShu",
          label: "中数未来",
        },
        {
          value: "GuiDa",
          label: "印象贵大",
        }
      ],
      pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 6);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          }]
        },
    };
  },
  watch: {
    'query.channel'(val) {
        this.query.page = 1
        this.getTransferList();
        this.getDetail()
    },
    date(val){
      if(val){
        this.query.startTime = val[0]
        this.query.endTime = val[1]
      }else{
        this.query.startTime = ''
        this.query.endTime = ''
      }
    }
  },
  filters: {
    filterName: function(value,that) {
      let msg
      that.options.map(item=>{
        if(value == item.value){
          msg = item.label
        }
      })
      return msg;
    }
  },
  methods: {
    handleSearch(){
      this.query.page = 1
      this.getTransferList();
      this.getDetail()
    },
    withdraw(){
         this.$confirm(`此操作将提现${this.money.ready}元, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
             this.btnLoading = true
            withdrawals({channel:this.query.channel}).then(res=>{
                this.btnLoading = false
                if(res.state == 'Success'){
                this.dialogFormVisible = false
                this.handleSearch()
                 this.$notify({
                    title: "成功",
                    message: "操作成功",
                    type: "success",
                });
                }
            })
        })
       
    },
    
    handleWithdraw(){
      this.dialogFormVisible = true
      this.withdrawList()

    },
    timeFormat(val){
      return timeFormat(val)
    },
    handleSizeChange(val) {
      this.query.page = 1;
      this.query.size = val;
      this.getTransferList();
      
    },
    handleCurrentChange(val) {
      this.query.page = val;
      this.getTransferList();

    },
     handleSizeChange1(val) {
      this.query1.page = 1;
      this.query1.size = val;
      this.withdrawList()
    },
    handleCurrentChange1(val) {
      this.query1.page = val;
      this.withdrawList()

    },
    withdrawList(){
      this.loading1 = true
      let obj = JSON.parse(JSON.stringify(this.query1))
      obj.channel = this.query.channel
      obj.shopId = this.query.channel
      --obj.page
      withdrawList(obj).then(res=>{
        this.loading1 = false
        if(res.state == 'Success'){
          this.tableData1 = res.content.content
          this.total1 = res.content.totalElements
        }
      })
    },
     getTransferList(){
      this.loading = true
      let obj = JSON.parse(JSON.stringify(this.query))
      --obj.page
      getTransferList(obj).then(res=>{
        this.loading = false
        if(res.state == 'Success'){
          this.tableData = res.content.content
          this.total = res.content.totalElements
        }
      })
    },
    getDetail(){
      this.moneyLoading = true
      getDetail({channel:this.query.channel}).then(res=>{
        this.moneyLoading = false
         if(res.state == 'Success'){
          this.money = res.content
          
        }
      })
    }
  },
  created() {
   this.getTransferList()
   this.getDetail()
  },
};
</script>
  
<style lang="scss" scoped>
// @import '@/styles/element-variables.scss';
.withdraw {

  .data-box{
    display: flex;
    justify-content: space-between;
    margin-bottom: 30px;
     .left,.right{
      background: rgba(250, 250, 250, 1);
      border-radius: 12px;
      padding: 30px;
    }
    .left{
      flex: 1;
     display: flex;
     .item{
      flex: 1;
      .label-box{
        display: flex;
        align-items: center;
        .text{
          color: #666;
          font-size: 16px;
        }
        i{
          width: 16px;
        }
      }
      .value-box{
         display: flex;
        align-items: center;
        margin-top: 30px;
        .text{
          color: #222;
          font-size: 26px;
          font-weight: 600;
        }
        .btn{
          margin-left: 20px;
        }
      }
     }
    }
    .right{
      width: 350px;
      font-size: 16px;
      margin-left: 30px;
      display: flex;
      align-items: center;
    }
  }
  .notice{
    margin-bottom: 20px;
  }
}

</style>
  