<template>
  <div class="app-container">
    <div v-if="showDetail == 1">
      <!-- 筛选条件 -->
      <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="billType">
                <!-- <el-input v-model="searchForm.billType" placeholder="请输入"></el-input> -->
                <el-select v-model="searchForm.billType" class="selectStyle" placeholder="请选择" filterable>
                  <el-option label="收款单" value="1"> </el-option>
                  <el-option label="付款单" value="3"> </el-option>
                  <el-option label="退款单" value="2"> </el-option>

                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="单据编号" prop="billNo">
                <el-input v-model="searchForm.billNo" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="往来单位" prop="wlCompanyType">
                <!-- <el-input v-model="searchForm.wlCompanyType" placeholder="请输入"></el-input> -->
                <el-select class="selectStyle" v-model="searchForm.wlCompanyType" placeholder="请选择" filterable>
                  <el-option v-for="(v, i) in customerList" :key="i" :label="v.name" :value="v.name">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="付款单位" prop="customerName">
                <!-- <el-input v-model="searchForm.customerName" placeholder="请输入"></el-input> -->
                <el-select class="selectStyle" v-model="searchForm.customerName" placeholder="请选择" filterable>
                  <el-option v-for="(v, i) in customerList" :key="i" :label="v.name" :value="v.name">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="结算方式" prop="settleType">
                <el-input v-model="searchForm.settleType" placeholder="请输入"></el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="收款用途" prop="">
                <el-input v-model="searchForm." placeholder="请输入"></el-input>
              </el-form-item>
            </el-col> -->
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="开始时间" prop="startTime">
                <el-date-picker class="selectStyle" v-model="searchForm.startTime" type="datetime" placeholder="选择日期时间"
                  default-time="00:00:00" value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="结束时间" prop="endTime">
                <el-date-picker class="selectStyle" v-model="searchForm.endTime" type="datetime" placeholder="选择日期时间"
                  default-time="00:00:00" value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
              </el-form-item>
            </el-col>

            <el-col :xs="24" :sm="12" :lg="6">
              <el-form-item label="" class="fr">
                <el-button size="mini" @click="cancelFn">清空</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="fl">
          <!-- <el-button type="primary" size="mini" @click="toPrint" :disabled="tableSelection.length < 1">打印发货单 -->
            <!-- </el-button> -->
        </div>
        <div class="fr">
          
          <ExportButton :exUrl="'/k3/receipt/export'" :exParams="exParams" class="exportClass" />
        </div>
      </div>
      <!-- 列表 -->
      <div class="mymain-container">
        <div class="table">
          <el-table ref="table" v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit
            highlight-current-row stripe show-summary :summary-method="getSummaries" @select="handleSelect" @select-all="handleSelectAll">
            <el-table-column align="center" type="selection" width="55"></el-table-column>

            <el-table-column align="left" label="单据类型" prop="billType" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{
                    scope.row.billType == 1
                      ? "收款单"
                      : scope.row.billType == 2
                        ? "退款单"
                        : scope.row.billType == 3 ? '付款单' : ''
                }}
              </template>
            </el-table-column>
            <el-table-column align="left" label="单据编码" prop="billNo" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.billNo" />
                <span>{{ scope.row.billNo }}</span>
              </template>
            </el-table-column>
            <el-table-column align="left" label="业务日期" prop="theTime" min-width="160" show-overflow-tooltip>
            </el-table-column>
            <!-- <el-table-column
              align="left"
              label="币别"
              prop="bibie"
              min-width="160"
              show-overflow-tooltip
            ></el-table-column> -->
            <el-table-column align="left" label="单据状态" prop="status" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                <el-tag size="mini" v-show="scope.row.status == 'A'">保存</el-tag>
                <el-tag size="mini" v-show="scope.row.status == 'B'" type="info">审核中</el-tag>
                <el-tag size="mini" v-show="scope.row.status == 'C'" type="success">已审核</el-tag>
                <el-tag size="mini" v-show="scope.row.status == 'D'" type="warning">重新审核</el-tag>
              </template>
            </el-table-column>
            <el-table-column align="left" label="钱包" prop="walletName" min-width="160" show-overflow-tooltip>
            </el-table-column>
            <el-table-column align="right" label="表头·应收金额" prop="" min-width="160" show-overflow-tooltip>
            </el-table-column>
            <el-table-column align="left" label="往来单位" prop="wlCompanyName" min-width="260" show-overflow-tooltip>
              <template slot-scope="scope">
                <CopyButton :copyText="scope.row.wlCompanyName" />
                <span>{{ scope.row.wlCompanyName }}</span>
              </template>
            </el-table-column>
            <el-table-column align="right" label="表头·实收金额" prop="amount" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ (scope.row.billType == 3 || scope.row.billType == 2) ? -scope.row.amount : scope.row.amount |
                    numToFixed
                }}
              </template>
            </el-table-column>
            <el-table-column align="right" label="收款金额" prop="" min-width="160" show-overflow-tooltip></el-table-column>
            <!-- <el-table-column
              align="center"
              label="结算方式"
              prop="settleType"
              min-width="160"
              show-overflow-tooltip
            ></el-table-column> -->
            <!-- <el-table-column
              align="center"
              label="收款用途"
              prop=""
              min-width="160"
              show-overflow-tooltip
            ></el-table-column> -->
            <el-table-column align="left" label="付款单位" prop="customerName" min-width="260" 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="right" label="表体·实收金额" prop="amount" min-width="160" show-overflow-tooltip>
              <template slot-scope="scope">
                {{ (scope.row.billType == 3 || scope.row.billType == 2) ? -scope.row.amount : scope.row.amount |
                    numToFixed
                }}
              </template>
            </el-table-column>
            <el-table-column align="left" label="勾对" prop="" min-width="160" show-overflow-tooltip></el-table-column>

            <el-table-column align="left" label="备注" prop="remark" min-width="160" show-overflow-tooltip>
            </el-table-column>
            <el-table-column align="center" label="操作" min-width="160" show-overflow-tooltip fixed="right">
              <template slot-scope="scope">
                <el-button type="text" class="textColor" v-if="
                  $checkBtnRole('examine', $route.meta.roles) &&
                  scope.row.status == 'C'
                " @click="NoExamineFn(scope.row.id)">弃审</el-button>
                <el-button type="text" class="textColor" @click="seeFn(scope.row.id)">查看</el-button>
                <!-- <el-button type="text" class="textColor" @click="toPrint(scope.row)">打印</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>
    <ReceiptPrint :listItem="queryItem" v-else-if="showDetail == 2" @backListFormDetail="backList" />
    <ReceiptListDetail :detailId="detailId" v-else />
  </div>
</template>

<script>
import { getCustomerList } from "@/api/finance/wallet";
import {
  getK3ReceiptList,
  getK3ReceiptAbandon,
} from "@/api/finance/receipt_list";
import ReceiptListDetail from "./components/receipt_list-detail";
import ReceiptPrint from '@/views/finance/components/receipt_print.vue'

import { numToFixed } from "@/filters";
export default {
  components: {
    ReceiptPrint,
    ReceiptListDetail,
  },
  data() {
    return {
      customerList: [],
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [], // 列表数据
      searchForm: {
        billType: "",
        billNo: "",
        wlCompanyType: "",
        customerName: "",
        settleType: "",
        startTime: "",
        endTime: "",
      }, //搜索表单
      listLoading: false, // 列表加载loading
      showDetail: 1,
      detailId: null,
      queryItem:{},
      tableSelection:[]
    };
  },
  computed: {
    exParams() {
      return {
        ...this.searchForm,
      };
    },
  },
  created() {
    this.getDataList({ pageSize: this.pageSize, pageNum: this.currentPage });
    this.getCustomerDataList();
  },
  methods: {
    //获取经销商列表
    async getCustomerDataList() {
      let res = await getCustomerList({
        pageNum: 1,
        pageSize: -1,
      });
      this.customerList = res.data.records;
    },
    //合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];

      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
        }

        if (index === 7) {
          let arr = [];
          data.forEach((v) => {
            if (v.billType == 2 || v.billType == 3) {
              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);
          sums[10] = numToFixed(a);
        }
      });
      return sums;
    },
    //弃审
    async NoExamineFn(id) {
      await getK3ReceiptAbandon({ id });
      this.$message.success("弃审成功");
      this.getDataList({ pageSize: this.pageSize, pageNum: this.currentPage });
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val;
      this.currentPage = 1;
      this.getDataList({ pageNum: this.currentPage, pageSize: this.pageSize });
    },
    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getDataList({ pageNum: this.currentPage, pageSize: this.pageSize });
    },
    //清空
    cancelFn() {
      this.$refs.searchForm.resetFields();
    },
    //搜索
    searchFn() {
      this.getDataList({
        ...this.searchForm,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      });
    },
    async getDataList(data) {
      const res = await getK3ReceiptList(data);
      // console.log(res);
      // res.data.records.forEach((item) => {
      //   item.sums1 = [];
      //   item.sums2 = ["amount"];
      // });
      this.dataList = res.data.records;
      this.listTotal = res.data.total;
    },
    seeFn(id) {
      this.detailId = id;
      this.showDetail = 3;
    },
    handleSelect(selection, row) {
      this.$refs.table.toggleRowSelection(row);
      this.dataList.forEach((item) => {
        if (item.id === row.id) {
          this.$refs.table.toggleRowSelection(item);
        }
      });
      this.tableSelection = this.$refs.table.selection;
    },

    handleSelectAll(selection) {
      this.tableSelection = this.$refs.table.selection;
    },
    backList() {
      this.queryItem = {};
      this.isShowPrint = false;
    },

    // 点击打印
    toPrint(row) {
      this.queryItem = row;
      this.showDetail = 2;
    },
  },
};
</script>

<style lang="scss" scoped>
.selectStyle {
  width: 100%;
}
</style>
