<template>
  <div class="app-container">
    <div v-if="showRecord">
      <!-- 筛选条件 -->
      <div>
        <el-form ref="searchForm" :model="searchForm" label-width="100px" size="mini" label-position="left">
          <el-row :gutter="20">
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="经销商名称" prop="customerId">
                <el-select class="selectStyle" v-model="searchForm.customerId" placeholder="请选择" filterable>
                  <el-option v-for="v in customerList" :key="v.id" :label="v.name" :value="v.id">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="18">
              <el-form-item label="" class="fr">
                <el-button size="mini" @click="clearFn">清空</el-button>
                <el-button size="mini" type="primary" @click="searchFn">搜索</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!-- 按钮 -->
      <div class="btn-group clearfix">
        <div class="fr">
          <ExportButton :exUrl="'/wallet/customer/list/export'" :exParams="exParams" class="exportClass" />
          <el-button type="primary" size="mini">打印</el-button>
        </div>
      </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 show-summary :summary-method="getSummaries">
            <el-table-column label="序号" align="left" type="index" width="100" show-overflow-tooltip></el-table-column>
            <el-table-column align="left" label="经销商编码" prop="customerNumber" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.customerNumber" />
                <span>{{scope.row.customerNumber}}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="经销商名称" prop="customerName" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.customerName" />
                <span>{{scope.row.customerName}}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="关联钱包" prop="name" min-width="160" show-overflow-tooltip></el-table-column>

            <el-table-column align="right" label="总信用额度" prop="amount" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{
                  (scope.row.usedCreditAmount + scope.row.freeCreditAmount)
                    | numToFixed
                }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-button type="text" class="textColor" v-if="$checkBtnRole('edit', $route.meta.roles)" @click="editFn(scope.row)">设置额度</el-button>
                <el-button type="text" class="textColor" @click="recordFn(scope.row.customerWalletId)">记录</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页 -->
        <div class="fr">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 50]" :page-size="10" layout="total, sizes, prev, pager, next, jumper" :total="listTotal">
          </el-pagination>
        </div>
      </div>
    </div>
    <CreditListDetail :recordsListId="recordsListId" v-else />
    <!-- 新增弹窗 -->
    <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="120px">
        <el-form-item label="信用变更额度" prop="amount">
          <el-input v-model.number="addForm.amount"></el-input>
        </el-form-item>

        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker class="selectStyle" v-model="addForm.startTime" type="date" placeholder="选择日期时间" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker class="selectStyle" v-model="addForm.endTime" type="date" placeholder="选择日期时间" value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelFn">取 消</el-button>
        <el-button type="primary" @click="addDataFn">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getCreditListEdit,
  getWalletCustomerList,
  getCustomerList,
} from "@/api/finance/credit_list";
import CreditListDetail from "./components/credit_list-detail";
import { downloadFiles } from "@/utils/util";
export default {
  components: {
    CreditListDetail,
  },
  data() {
    return {
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [], // 列表数据
      searchForm: {
        customerId: "",
      }, //搜索表单
      listLoading: false, // 列表加载loading
      showRecord: true,
      dialogForm: false, //弹窗表单
      addForm: {
        startTime: "",
        endTime: "",
        amount: null,
      },
      data: null,
      recordsListId: {}, //记录
      customerList: null,

      rules: {
        startTime: [
          { required: true, message: "请选择开始时间", trigger: "blur" },
        ],
        endTime: [
          { required: true, message: "请选择结束时间", trigger: "blur" },
        ],
        amount: [
          {
            required: true,
            message: "请输入数字",
            type: "number",
            trigger: "blur",
          },
        ],
        customerId: [
          { required: true, message: "请选择经销商", trigger: "blur" },
        ],
        customerWalletId: [
          { required: true, message: "请选择钱包", trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    exParams() {
      return {
        customerId: this.searchForm.customerId,
        type: "COMMONLY",
      };
    },
  },
  created() {
    this.getDataList({
      type: "COMMONLY",
      pageSize: this.pageSize,
      pageNum: this.currentPage,
    });
    this.getCustomerData({ pageSize: -1, pageNum: 1 });
  },
  methods: {
    //列表合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
        }
        if (index === 4) {
          let map1 = data.map((v) => {
            return v.usedCreditAmount + v.freeCreditAmount;
          });
          sums[index] = map1
            .reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0)
            .toFixed(2);
        }
      });
      return sums;
    },
    //获取经销商列表
    async getCustomerData(data) {
      const res = await getCustomerList(data);
      console.log(res);
      this.customerList = res.data.records;
    },
    //设置额度
    editFn(value) {
      this.data = {
        customerId: value.customerId,
        customerWalletId: value.customerWalletId,
      };
      this.dialogForm = true;
    },
    //清空
    clearFn() {
      this.$refs.searchForm.resetFields();
    },
    //搜索
    searchFn() {
      this.getDataList({
        ...this.searchForm,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        type: "COMMONLY",
      });
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getDataList({ pageNum: 1, pageSize: this.pageSize });
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDataList({ pageNum: val, pageSize: 10 });
    },
    //列表数据
    async getDataList(data) {
      const res = await getWalletCustomerList(data);
      // console.log(res);
      this.dataList = res.data.records;
      this.listTotal = res.data.total;
    },

    //确定
    async addDataFn() {
      await this.$refs.addForm.validate();
      await getCreditListEdit({ ...this.addForm, ...this.data });
      this.getDataList({
        type: "COMMONLY",
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      });
      this.$message.success("设置成功");
      this.dialogForm = false;
    },
    //取消
    async cancelFn() {
      await this.$refs.addForm.resetFields();
      this.dialogForm = false;
    },
    //记录
    recordFn(id) {
      console.log(id);
      this.recordsListId = id;
      this.showRecord = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.selectStyle {
  width: 100%;
}
.exportClass {
  display: inline-block;
  margin-right: 10px;
}
</style>
