import * as echarts from "echarts";
import { msgBoradTabel, msgBoradNum, responMsg } from "../../api/messageBoard"
import { historyAll, Handle } from "../../api/pay"
import { Debounce } from "../../util/processingData"
import Vue from 'vue'
export default {
  data() {
    return {
      tableData0: [{
        date: '2016-05-03',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-02',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-04',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-01',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-08',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-06',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }, {
        date: '2016-05-07',
        name: '王小虎',
        province: '上海',
        city: '普陀区',
        address: '上海市普陀区金沙江路 1518 弄',
        zip: 200333
      }],
      msg: "",
      dialogVisible: false,
      tableData: [{
        appliedAmount: "50",
        coin: "BUSD",
        chain: "",
        userAddress: "",
        fromAddres: "",
        appliedTime: "",
        id: "1",
      }],
      tableData: [{
        appliedAmount: "50",
        coin: "BUSD",
        chain: "",
        userAddress: "",
        fromAddres: "",
        appliedTime: "",
        id: "1",
      }],
      successData: [{
        appliedAmount: "50",
        coin: "BUSD",
        chain: "",
        userAddress: "",
        fromAddres: "",
        appliedTime: "",
        id: "1",
      }],
      failData: [{
        appliedAmount: "50",
        coin: "BUSD",
        chain: "",
        userAddress: "",
        fromAddres: "",
        appliedTime: "",
        id: "1",
      }],
      params: {
        status: "",
        page: 1,
        limit: 300,
        address: ""
      },
      backendWithdrawalLoading: false,
      handleParams: {
        id: "",
        oper: "",
        refundTxid: "",
        msg: "",
        refundAddress: "",
      },
      refuseDialogVisible: false,
      backendLoading: false,
      appliedAmount: "",
      activeName: "",


    }
  },

  mounted() {
    this.fetchWithdrawal2({ page: 1, limit: 300, status: 2 })
    this.fetchWithdrawal0({ page: 1, limit: 300, status: 0 })
    this.fetchWithdrawal1({ page: 1, limit: 300, status: 1 })
   
    this.activeName = sessionStorage.getItem('WithdrawalActiveName')
    if (!this.activeName) {
      this.activeName="pending"
    }
   
  },
  watch: {
    activeName(val){
      console.log(val,3333);
    }
  },
  methods: {
    //请求待处理订单
    async fetchWithdrawal2(params) {
      this.backendWithdrawalLoading = true
      const { data } = await historyAll(params)


      this.tableData = data.rows
      // this.params.address = ""
      this.backendWithdrawalLoading = false

    },
    //请求成功订单
    async fetchWithdrawal1(params) {
      this.backendWithdrawalLoading = true
      const { data } = await historyAll(params)


      this.successData = data.rows
      // this.params.address = ""
      this.backendWithdrawalLoading = false

    },
    //请求失败订单
    async fetchWithdrawal0(params) {
      this.backendWithdrawalLoading = true
      const { data } = await historyAll(params)


      this.failData = data.rows
      // this.params.address = ""
      this.backendWithdrawalLoading = false

    },
    //请求处理提现订单
    async fetchHandle(params) {
      this.backendLoading = true
      const { data } = await Handle(params)

      if (data.code == 200) {
        this.$message({
          type: 'success',
          message: data.msg,
          duration: 5000,
          showClose: true,
        });
        this.dialogVisible = false
        this.refuseDialogVisible = false

        Object.keys(this.handleParams).forEach(key => {//值清空
          this.handleParams[key] = "";
        });

        this.fetchWithdrawal0({ page: 1, limit: 300, status: 2 })//处理成功后重新请求一遍 刷新数据
      }



      this.backendLoading = false

    },

    //点击搜索
    handelSearch(status) {

      for (const key in this.params) { //判空
        if (key !== `status` && this.params[key] == "") {
          this.$message({
            message: this.$t(`pay.search`),
            type: "error",
          });
          return
        }
      }

      switch (status) {
        case 0:
          this.params.status = 0
          this.fetchWithdrawal0(this.params)
          break;
        case 1:
          this.params.status = 1
          this.fetchWithdrawal1(this.params)

          break;
        case 2:
          this.params.status = 2
          this.fetchWithdrawal2(this.params)
          break;

        default:
          break;
      }


    },

    //点击表格提现按钮
    handleEdit(index, row) {
      this.appliedAmount = row.appliedAmount
      this.dialogVisible = true
      this.msg = "确认提现"
      this.handleParams.id = row.id
    },
    //点击表格拒绝按钮
    handleDelete(index, row) {
      this.refuseDialogVisible = true
      this.msg = "拒绝提现"
      this.handleParams.id = row.id

    },
    handelTime(date) {
      if (date) {
        return `${date.split("T")[0]} ${date.split("T")[1].split(".")[0]}`
      }
    },
    //确认通过提现
    handelConfirm() {
      this.handleParams.oper = 1

      for (const key in this.handleParams) { //判空
        if (this.handleParams[key] == "" && key !== "oper" && key !== "msg") {


          switch (key) {
            case "refundTxid":
              this.$message({
                message: this.$t(`pay.hashError`),
                type: "error",
              });
              break;

            case "refundAddress":
              this.$message({
                message: this.$t(`pay.paymentAddress`),
                type: "error",
              });
              break;




            default:
              break;
          }




          return
        }
      }

      // this.handleParams.refund = Number(this.handleParams.refund)


      this.fetchHandle(this.handleParams)
    },
    //确认拒绝提现
    handelRefuse() {
      this.handleParams.oper = 0

      if (!this.handleParams.msg) { //备注不能为空
        this.$message({
          message: this.$t(`help.resubmit`),
          type: "error",
        });
        return
      }


      this.fetchHandle(this.handleParams)
    },
    //弹窗取消
    handelCancel() {
      this.dialogVisible = false
      this.backendLoading = false
      Object.keys(this.handleParams).forEach(key => {//值清空
        this.handleParams[key] = "";
      });
    },
    //点击查看提现详情
    handelDetails(row) {
      this.$router.push({ path: "/withdrawalDetails", query: { id: row.id } })
    },

    //点击刷新
    handelRefresh: Debounce(function () {
      this.fetchWithdrawal2({ page: 1, limit: 300, status: 2 })
      this.fetchWithdrawal0({ page: 1, limit: 300, status: 0 })
      this.fetchWithdrawal1({ page: 1, limit: 300, status: 1 })
    }, 1000),

    handleClick(activeName2) {
      this.params.address = ""
      // 将activeName添加到浏览器的历史记录中
      let activeName = activeName2.name   
      sessionStorage.setItem(`WithdrawalActiveName`,activeName)
    }

  }
}