<template>
  <div class="app-container">
    <span>返利互转单</span>
    <el-divider></el-divider>
    <!-- 筛选条件 -->
    <div>
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="100px"
        size="small"
        label-position="left"
      >
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="返利互转单号" prop="">
              <el-input placeholder="请输入" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="单据日期" prop="">
              <el-input placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="制单人" prop="">
              <el-input placeholder="请输入" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="经销商编号" prop="">
              <el-input
                placeholder="请输入"
                v-model="searchForm.customerId"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="经销商名称" prop="">
              <el-input
                placeholder="请输入"
                v-model="searchForm.customerName"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="备注信息" prop="">
              <el-input placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 列表 -->
    <div class="mymain-container">
      <div class="table">
        <el-table
          v-loading="listLoading"
          :data="dataList"
          element-loading-text="Loading"
          border
          fit
          highlight-current-row
          stripe
        >
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="100"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            align="center"
            label="经销商名称"
            prop="customerName"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-select
                filterable
                v-model="scope.row.customerId"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in customerData"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="返利类型"
            prop="name"
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-select
                filterable
                v-model="scope.row.name"
                placeholder="请选择"
                @focus="typeFn(scope.row)"
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="返利金额"
            prop=""
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.money"></el-input>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            label="备注"
            prop=""
            min-width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <el-input v-model="scope.row.bz"></el-input>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fl">
        <el-button type="primary" size="small" @click="preservationFn"
          >保存</el-button
        >

        <el-button type="primary" size="small" @click="resetFn">重置</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import {
  getUserInfo,
  getWalletCustomerList,
  getTransferAdd,
  getTransferSubmit,
  getCustomerGroupList,
} from "@/api/finance/change_apply";

export default {
  data() {
    return {
      listLoading: false, // 列表加载loading
      dataList: [],
      searchForm: {
        customerId: "",
        customerName: "",
      },
      customerData: [], //经销商数据
      typeList: [], //返利类型数据
    };
  },
  computed: {
    ...mapGetters(["userid"]),
  },
  created() {
    this.getUserInfoFn();
    this.getCustomerData();
  },
  methods: {
    func(arr, target) {
      var obj = {};
      for (var i = 0; i < arr.length; i++) {
        var item = arr[i].money;
        if (obj[item] === undefined) {
          var x = target - item;
          obj[x] = i;
        } else {
          return [obj[item], i];
        }
      }
      return null;
    },

    resetFn() {
      this.dataList.forEach((v) => {
        v.money = "";
        v.bz = "";
      });
    },
    //返利类型数据
    async typeFn(row) {
      let res = await getWalletCustomerList({
        customerId: row.customerId,
        type: "REBATE",
      });
      this.typeList = res.data;
    },

    //查询同个集团的经销商数据
    async getCustomerData() {
      const res = await getCustomerGroupList();
      console.log(res, 123214);
      this.customerData = res.data;
    },

    //保存
    async preservationFn() {
      let receiverWalletRebateId;
      let initiatorWalletRebateId;
      let initiatorRemark;
      let receiverRemark;
      let amount;
      let res = this.func(this.dataList, 0);
      console.log(res);

      if (this.dataList[res[0]].money > 0) {
        receiverWalletRebateId = this.dataList[res[0]].customerWalletId;
        initiatorWalletRebateId = this.dataList[res[1]].customerWalletId;
        receiverRemark = this.dataList[res[0]].bz;
        initiatorRemark = this.dataList[res[1]].bz;
        amount = this.dataList[res[0]].money;
      } else {
        receiverWalletRebateId = this.dataList[res[1]].customerWalletId;
        initiatorWalletRebateId = this.dataList[res[0]].customerWalletId;
        receiverRemark = this.dataList[res[1]].bz;
        initiatorRemark = this.dataList[res[0]].bz;
        amount = this.dataList[res[1]].money;
      }

      await getTransferAdd({
        receiverWalletRebateId,
        initiatorWalletRebateId,
        receiverRemark,
        initiatorRemark,
        amount: amount * 1,
      });
      this.$message.success("申请成功");
    },
    async getUserInfoFn() {
      const res = await getUserInfo({ adminUserId: this.userid });
      console.log(res);
      this.searchForm.customerId = res.data.customerId;
      this.searchForm.customerName = res.data.customerName;

      const res2 = await getWalletCustomerList({
        customerId: res.data.customerId,
        type: "REBATE",
      });
      console.log(res2);

      res2.data.forEach((v) => {
        v.money = "";
        v.bz = "";
      });
      this.dataList = res2.data;
      console.log(this.dataList);
    },
  },
};
</script>

<style>
</style>