<template>
  <template-page
    ref="pageRef"
    :getList="getList"
    :exportList="exportList"
    :operation="operation()"
    :optionsEvensGroup="optionsEvensGroup"
    :columnParsing="columnParsing"
  >
    <Popu v-if="visible">
      <AccountListDetail @close="handleClose" />
    </Popu>
  </template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import add_callback_mixin from '@/components/template/add_callback_mixin.js'
import Popu from '@/components/template/popu.vue'
import {
  getFinanceStandingBookList,
  getFinanceStandingBookListV2,
  getFinanceStandingBookCheck,
  getCustomerList,
  getWalletCustomerList
} from '@/api/finance/account_list'
import AccountListDetail from './components/account_list-detail'
export default {
  components: { TemplatePage, Popu, AccountListDetail },
  mixins: [import_mixin, add_callback_mixin],
  data() {
    return {
      visible: false,
      // 事件组合
      optionsEvensGroup: [
        [
          [
            {
              name: '一键对账',
              click: this.reconciliationFn
            }
          ]
        ],
        [
          [
            {
              name: '记录',
              click: this.recordFn
            }
          ]
        ]
      ],
      // 表格属性
      tableAttributes: {
        // 启用勾选列
        selectColumn: true
      }, // 关闭新增弹窗

      // 表格事件
      tableEvents: {
        'selection-change': this.selectionChange
      },
      recordSelected: []
    }
  },
  methods: {
    // 列表请求函数
    getList(...p) {
      this.recordSelected = []
      return getFinanceStandingBookListV2(...p)
    },
    // 列表导出函数
    exportList: ()=>{},
    // 表格列解析渲染数据更改
    columnParsing(item, defaultData) {
      return defaultData
    },
    // 监听勾选变化
    selectionChange(data) {
      this.recordSelected = data
    },
    operation() {
      return (h, { row, index, column }) => {
        return (
          <div class="operation-btns">
            {/* <el-button
              size="mini"
              type="text"
              onClick={() => {
                this.visible = true
                this.detailsId = row.id
              }}
            >
              查看
            </el-button> */}
          </div>
        )
      }
    },
    handleClose() {
      this.addOff(() => {
        this.visible = false
      })()
    },
    //合计
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
        }
        if (index === 10) {
          let arr = []
          data.forEach(v => {
            if (v.amountType == 'OUT') {
              arr.push(-v.amount)
            } else {
              arr.push(v.amount)
            }
          })

          let a = arr.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] = numToFixed(a)
        }
      })
      return sums
    },
    //改变经销商
    async changeFn(v) {
      this.searchForm.customerWalletId = ''
      let res = await getWalletCustomerList({ customerId: v })
      this.walletList = res.data
    },
    //获取经销商数据
    async getCustomerDataList(data) {
      const res = await getCustomerList(data)
      this.customerList = res.data.records
    },
    //记录
    recordFn() {
      this.visible = true
    },
    //一键对账
    async reconciliationFn() {
      const res = await getFinanceStandingBookList({
        pageSize: -1,
        pageNum: 1
      })
      let arr = res.data.records
      let ids = arr.map(v => v.id)
      console.log(ids)

      await getFinanceStandingBookCheck({ ids: ids.toString() })
      this.$message.success('对账成功')
    },
    seeFN() {
      this.showDetail = false
    }
  }
}
</script>

<style lang="scss" scoped></style>
