<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" :permission="permissionList"
      :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel"
      @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger" size="small" icon="el-icon-delete" plain v-if="permission.withdrawrecords_delete"
          @click="handleDelete">删 除
        </el-button>
      </template>

      <template slot="userInfo" slot-scope="scope">
        <div style="display: flex;">
          <div style="display: flex;justify-content: center;align-items: center;">
            <el-avatar size="large" :src="scope.row.avatar"></el-avatar>
          </div>
          <div style="padding: 10px;">
            <div>昵称：{{scope.row.nickName}}</div>
            <div>手机：{{scope.row.phone}}</div>
          </div>
        </div>
      </template>

      <!-- 操作菜单 -->

      <template slot="timeInfo" slot-scope="scope">
        <div>下单时间：{{scope.row.orderTime}}</div>
        <div>完成时间：{{scope.row.finishTime}}</div>
      </template>

      <template slot="accountInfo" slot-scope="scope">
        <div>银行账号：{{scope.row.receiverAccountNo}}</div>
        <div>开户姓名：{{scope.row.receiverAccountName}}</div>
      </template>



      <template slot="cashInfo" slot-scope="scope">
        <div>提现金额：<span style="color: #ff0000;padding-right: 5px;">{{scope.row.cashValue}}</span> 元</div>
        <div>到账金额：<span style="color: #ff0000;padding-right: 5px;">{{scope.row.receiveAmount}}</span> 元 </div>
        <div>手续费：<span style="color: #ff0000;padding-right: 5px;">{{scope.row.fee}}</span> 元</div>
      </template>

      <template slot="receiveType" slot-scope="scope">
        <el-tag type="success" v-if="scope.row.receiveType=='REAL_TIME'">实时到账</el-tag>
      </template>

      <template slot="withdrawStatus" slot-scope="scope">
        <el-tag type="warning" v-if="scope.row.withdrawStatus==0">处理中</el-tag>
        <el-tag type="success" v-if="scope.row.withdrawStatus==1">已处理</el-tag>
        <el-tag type="danger" v-if="scope.row.withdrawStatus==2">提现失败</el-tag>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {
    getList,
    getDetail,
    add,
    update,
    remove
  } from "@/api/withdrawrecords/withdrawrecords";
  import {
    dateFormat
  } from "../../util/date.js"
  import {
    mapGetters
  } from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          selection: true,
          dialogClickModal: false,
          column: [{
              label: "用户id",
              hide: true,
              prop: "userId",
              rules: [{
                required: true,
                message: "请输入用户id",
                trigger: "blur"
              }]
            },
            {
              slot: true,
              width:180,
              label: "用户信息",
              prop: "userInfo"
            },
            {
              label:"账户信息",
              width:200,
              slot:true,
              prop:"accountInfo"
            },
            {
              hide:true,
              label: "手机号",
              prop: "phone",
              search: true
            },
            {
              hide:true,
              label: "昵称",
              prop: "nickName",
              search: true
            },
            {
              label: "openid",
              hide:true,
              prop: "openid",
              rules: [{
                required: true,
                message: "请输入用户id",
                trigger: "blur"
              }]
            },
            {
              label: "提现状态",
              width:100,
              search: true,
              type: "select",
              dicData: [{
                  label: '处理中',
                  value: 0
                },
                {
                  label: '已处理',
                  value: 1
                },
                {
                  label: '提现失败',
                  value: 2
                }
              ],
              prop: 'withdrawStatus'
            },
            {
              label: "备注说明",
              width:100,
              prop: "note",
              rules: [{
                required: true,
                message: "请输入备注说明",
                trigger: "blur"
              }]
            },
            {
              label: "消耗的热力值",
              width:100,
              prop: "hotValue",
              rules: [{
                required: true,
                message: "请输入消耗的热力值",
                trigger: "blur"
              }]
            },
            {
              label:"金额信息",
              prop:'cashInfo',
              slot:true
            },
            {
              hide:true,
              label: "提现金额",
              prop: "cashValue",
              rules: [{
                required: true,
                message: "请输入提现金额",
                trigger: "blur"
              }]
            },
            {
              hide:true,
              label: "提现到账的金额",
              prop: "receiveAmount",
              rules: [{
                required: true,
                message: "请输入提现到账的金额",
                trigger: "blur"
              }]
            },
            {
              hide:true,
              label: "提现手续费",
              prop: "fee",
              rules: [{
                required: true,
                message: "请输入提现手续费",
                trigger: "blur"
              }]
            },
            {
              label:"时间",
              width:210,
              prop:'timeInfo',
              slot:true
            },
            {
              label: "订单时间",
              hide:true,
              prop: "orderTime",
              rules: [{
                required: true,
                message: "请输入订单时间",
                trigger: "blur"
              }]
            },
            {
              label: "完成时间",
              hide:true,
              prop: "finishTime",
              rules: [{
                required: true,
                message: "请输入完成时间",
                trigger: "blur"
              }]
            },

            {
              label: "接收人真实姓名",
              hide:true,
              prop: "receiverAccountName",
              rules: [{
                required: true,
                message: "请输入接收人真实姓名",
                trigger: "blur"
              }]
            },
            {
              label: "接收人的银行账号",
              hide:true,
              prop: "receiverAccountNo",
              rules: [{
                required: true,
                message: "请输入接收人的银行账号",
                trigger: "blur"
              }]
            },
            {
              label: "到账类型",
              slot:true,
              prop: "receiveType",
              rules: [{
                required: true,
                message: "请输入到账类型，默认实时到账",
                trigger: "blur"
              }]
            },
            {
              label: "失败的原因",
              prop: "failReason",
              rules: [{
                required: true,
                message: "请输入失败的原因",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.withdrawrecords_add, false),
          viewBtn: this.vaildData(this.permission.withdrawrecords_view, false),
          delBtn: this.vaildData(this.permission.withdrawrecords_delete, false),
          editBtn: this.vaildData(this.permission.withdrawrecords_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      confirm(row) {
        let _this = this
        this.$alert('确定发款？', '提示', {
          confirmButtonText: '确定',
          callback: () => {
            _this.doConfirm(row)
          }
        });
      },
      async doConfirm(row) {
        let grantTime = dateFormat(new Date())
        let params = {
          id: row.id,
          grantTime,
          withdrawStatus: 1
        }
        let res = await update(params)
        if (res.data.success) {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: "发款成功!"
          });
        }
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
