<template>
  <div class="app-container">
    <div v-if="showReconciliation">
      <!-- 筛选条件 -->
      <div>
        <Collapse :screen-form="searchForm" :is-show="false">
          <template #right_btn>
            <el-button size="mini" @click="clearFn">清空</el-button>
            <el-button size="mini" type="primary" @click="searchFn">搜索</el-button>
          </template>
          <template #search>
            <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="type">
                    <el-select v-model="searchForm.type" class="selectStyle" placeholder="请选择" filterable clearable>
                      <el-option v-for="(v, i) in walletType" :key="i" :label="v.label" :value="v.value" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :lg="6">
                  <el-form-item label="月份" prop="month">
                    <el-date-picker v-model="searchForm.month" style="width: 100%" type="month" placeholder="选择月" />
                  </el-form-item>
                </el-col>
                <el-col :xs="24" :sm="12" :lg="6">
                  <el-form-item label="对账状态" prop="isReconciliation">
                    <el-select
                      v-model="searchForm.isReconciliation"
                      class="selectStyle"
                      placeholder="请选择对账状态"
                      filterable
                      clearable
                    >
                      <el-option v-for="(v, i) in statusList" :key="i" :label="v.label" :value="v.value" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
          </template>
        </Collapse>
      </div>
      <!-- 按钮 -->
      <div class="btn-group clearfix">
        <div class="fl">
          <el-button :disabled="dataList.length == 0" type="primary" size="mini" @click="reconciliationFn"
            >一键对账</el-button
          >
          <!--          <el-button type="primary" size="mini" @click="recordFn">记录</el-button>-->
        </div>
        <!--        <div class="fr">-->
        <!--          <ExportButton :ex-url="'/finance/standing/book/export'" :ex-params="exParams" />-->
        <!--        </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 align="left" label="序号" type="index" width="80" show-overflow-tooltip />-->
            <el-table-column align="left" label="月份" prop="month" min-width="100" show-overflow-tooltip />
            <el-table-column
              align="left"
              label="对账状态"
              prop="isReconciliation"
              min-width="100"
              show-overflow-tooltip
            >
              <template slot-scope="scope">
                <el-tag v-if="scope.row.isReconciliation == false" size="mini" type="danger">未对账</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="left" label="经销商编码" prop="customerCode" min-width="100" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copy-text="scope.row.customerCode" />
                <span>{{ scope.row.customerCode }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="经销商名称" prop="customerName" min-width="260" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copy-text="scope.row.customerName" />
                <span>{{ scope.row.customerName }}</span>
              </template>
            </el-table-column>

            <el-table-column align="left" label="预付货款" prop="preFee" min-width="160" show-overflow-tooltip />
            <el-table-column align="left" label="订单占款" prop="orderFee" min-width="160" show-overflow-tooltip />
            <el-table-column align="left" label="支付货款" prop="payFee" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ scope.row.billNo }}
              </template>
            </el-table-column>
            <el-table-column align="left" label="押金" prop="depositFee" min-width="160" show-overflow-tooltip />
            <el-table-column align="left" label="工程押金" prop="projectFee" min-width="160" show-overflow-tooltip />
          </el-table>
        </div>
        <!-- 分页 -->
        <div class="fr">
          <el-pagination
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <AccountListDetail v-else />
  </div>
</template>

<script>
import {
  getCustomerList,
  getFeconciliateList,
  getFinanceStandingBookCheck,
  getWalletCustomerList
} from '@/api/finance/account_list'
import AccountListDetail from '../components/account_list-detail'

export default {
  components: {
    AccountListDetail
  },
  data() {
    return {
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [], // 列表数据
      searchForm: {
        customerId: '',
        customerWalletId: '',
        billNo: '',
        startTime: '',
        endTime: '',
        type: null,
        month: '',
        isReconciliation: null
      }, // 搜索表单
      listLoading: false, // 列表加载loading
      showReconciliation: true,
      statusList: [
        {
          value: true,
          label: '是'
        },
        {
          value: false,
          label: '否'
        }
      ],
      walletType: [
        {
          value: 'COMMONLY',
          label: '普通钱包'
        },
        {
          value: 'REBATE',
          label: '返利钱包'
        }
      ],
      isCollapse: true
    }
  },
  computed: {
    exParams() {
      return {
        ...this.searchForm,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
        isReconciliation: false
      }
    }
  },
  created() {
    this.getDataList({
      pageSize: this.pageSize,
      pageNum: this.currentPage,
      ...this.searchForm
    })
    this.getCustomerDataList({
      pageSize: -1,
      pageNum: 1
    })
  },
  methods: {
    // 清空
    clearFn() {
      this.$refs.searchForm.resetFields()
      this.currentPage = 1
      this.getDataList({
        ...this.searchForm,
        pageSize: this.pageSize,
        pageNum: this.currentPage
      })
    },
    // 搜索
    searchFn() {
      this.getDataList({
        ...this.searchForm,
        pageSize: this.pageSize,
        pageNum: this.currentPage
      })
    },
    // 改变经销商
    async changeFn(v) {
      this.searchForm.customerWalletId = ''
      const res = await getWalletCustomerList({ customerId: v })
      this.walletList = res.data
    },
    // 获取经销商数据
    async getCustomerDataList(data) {
      const res = await getCustomerList(data)
      this.customerList = res.data.records
    },
    // 记录
    recordFn() {
      this.showReconciliation = false
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getDataList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        isReconciliation: false
      })
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList({
        pageNum: this.currentPage,
        pageSize: this.pageSize,
        isReconciliation: false
      })
    },
    // 获取列表
    async getDataList(data) {
      const res = await getFeconciliateList(data)
      res.data.records.forEach(item => {
        item.sums1 = []
        item.sums2 = ['amount']
      })
      this.dataList = res.data.records
      this.listTotal = res.data.total
    },
    // 一键对账
    async reconciliationFn() {
      const res = await getFeconciliateList({
        pageSize: -1,
        pageNum: 1
      })
      const arr = res.data.records
      const 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>
.selectStyle {
  width: 100%;
}
</style>
