<template >
  <div class="app-container">
    <el-row>
      <el-col :xs="24" :sm="12" :lg="12">
        <div class="styl">现金钱包</div>
      </el-col>
      <el-col :xs="24" :sm="12" :lg="12">
        <div class="fr">
          <el-button size="small" type="primary" @click="generateFn"
            >一键生成钱包</el-button
          >
        </div>
      </el-col>
    </el-row>
    <el-divider></el-divider>
    <!-- 列表 -->
    <div class="mymain-container">
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="walletList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column
            align="center"
            label="经销商名称"
            prop="customerName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="钱包类型"
            prop="mainName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="钱包"
            prop="name"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="余额"
            prop="amount"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="可用信用额度"
            prop="freeCreditAmount"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="已用信用额度"
            prop="usedCreditAmount"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="更新时间"
            prop="updateTime"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
        </el-table>
      </div>
    </div>
    <span>返利钱包</span>
    <el-divider></el-divider>
    <!-- 列表 -->
    <div class="mymain-container">
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="rebateList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column
            align="center"
            label="经销商名称"
            prop="customerName"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="返利钱包"
            prop="name"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="返利比例"
            prop="rebateRate"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="钱包余额"
            prop="amount"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>

          <el-table-column
            align="center"
            label="更新时间"
            prop="updateTime"
            min-width="160"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="操作"
            min-width="160"
            show-overflow-tooltip
          >
            <template v-slot="{ row }">
              <el-button type="text" class="textColor" @click="editFn(row)">
                修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 弹窗 -->
    <el-dialog
      title="配置返利比例"
      :visible.sync="dialogForm"
      width="30%"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form
        ref="addForm"
        :rules="rules"
        :model="addForm"
        label-width="100px"
      >
        <el-form-item label="返利比例" prop="rebateRate">
          <el-input
            v-model.number="addForm.rebateRate"
            oninput="if(value>100)value=100;if(value<0)value=0"
            ><i class="el-input__icon" slot="suffix">% </i></el-input
          >
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="determineFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  getfinanceInit,
  getWalletRebateRateUpdate,
} from "@/api/finance/wallet";
import { getWalletCustomerList } from "@/api/finance/change_apply";
export default {
  data() {
    return {
      listLoading: false, // 列表加载loading
      walletList: [],
      rebateList: [],
      addForm: {
        rebateRate: null,
      },
      dialogForm: false,
      rules: {
        rebateRate: [
          {
            required: true,
            message: "请输入返利比例",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    console.log(this.$route.query.id, 11, this.customerId);
    if (this.$route.query.id) {
      this.getDataWallet({
        customerId: this.$route.query.id,
        type: "COMMONLY",
      });
      this.getDataRebate({
        customerId: this.$route.query.id,
        type: "REBATE",
      });
    } else {
      this.getDataWallet({
        customerId: this.customerId,
        type: "COMMONLY",
      });
      this.getDataRebate({
        customerId: this.customerId,
        type: "REBATE",
      });
    }
  },
  computed: {
    ...mapGetters(["customerId"]),
  },
  methods: {
    //取消
    async cancelFn() {
      this.dialogForm = false;
      await this.$refs.addForm.clearValidate();
    },
    //确定
    async determineFn() {
      await this.$refs.addForm.validate();
      const res = this.addForm.rebateRate / 100;
      this.addForm.rebateRate = res;
      await getWalletRebateRateUpdate({ ...this.addForm });
      if (this.$route.query.id) {
        this.getDataRebate({
          customerId: this.$route.query.id,
          type: "REBATE",
        });
      } else {
        this.getDataRebate({
          customerId: this.customerId,
          type: "REBATE",
        });
      }
      this.$message.success("修改成功");
      this.dialogForm = false;
    },
    //修改
    editFn(v) {
      this.addForm.customerWalletId = v.customerWalletId;
      this.addForm.rebateRate = v.rebateRate * 100;
      this.dialogForm = true;
    },
    //一键生成
    async generateFn() {
      await getfinanceInit({ customerId: this.customerId });
      this.getDataWallet();
      this.getDataRebate();
      this.$message.success("已生成");
    },
    //
    async getDataRebate(data) {
      let res = await getWalletCustomerList(data);
      this.rebateList = res.data;
    },
    async getDataWallet(data) {
      let res = await getWalletCustomerList(data);
      this.walletList = res.data;
    },
  },
};
</script>

<style lang="scss" scoped>
.styl {
  padding-top: 14px;
}
</style>