<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading" style="width: 100%">
      <search-table @search="reload"></search-table>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        height="calc(100vh - 375px)"
        full-height="calc(100vh - 116px)"
        tool-class="ele-toolbar-form"
        :selection.sync="selection"
        :page-size="20"
        @columns-change="handleColumnChange"
        :cache-key="cacheKeyUrl"
      > 
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            v-if="$hasPermission('eom:finreceivable:push')"
            size="small"
            type="primary"
            class="ele-btn-icon"
            @click="allPushBtn"
            :disabled="selection?.length === 0"
          >
            批量推送
          </el-button>
        </template>
        <!-- 操作 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            v-if="[2].includes(row.approvalStatus)"
            :underline="false"
            icon="el-icon-plus"
            @click="handleCollection(row, 'update')"
          >
            收款
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-plus"
            @click="sub(row)"
            v-if="[0, 3].includes(row.approvalStatus)"
          >
            提交
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-plus"
            @click="openEdit(row)"
            v-if="canAddInvoice(row)"
          >
            新增发票
          </el-link>
        </template>
        <!-- 收款信息 -->
        <template v-slot:code="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="handleDetail(row, 'view')"
          >
            {{ row.code }}
          </el-link>
        </template>
        <!-- 开票信息 -->
        <template v-slot:invoiceCode="{ row }">
          <el-link
            type="primary"
            :underline="false"
            @click="handleInvoiceDetail(row, 'view')"
          >
            {{ row.invoiceCode }}
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <!--    收款信息-->
    <collection-dialog
      ref="collectionDialogRef"
      v-if="collectionDialogFlag"
      :collection-dialog-flag.sync="collectionDialogFlag"
      @reload="reload"
    ></collection-dialog>
    <detail-dialog
      ref="detailDialogRef"
      v-if="detailDialogFlag"
      :detail-dialog-flag.sync="detailDialogFlag"
    ></detail-dialog>
    <!--开票详情-->
    <invoice-detail-dialog
      ref="invoiceDetailDialogRef"
      v-if="invoiceDetailDialogFlag"
      :detailDialogFlag.sync="invoiceDetailDialogFlag"
    ></invoice-detail-dialog>
    <process-submit-dialog
      :processSubmitDialogFlag.sync="processSubmitDialogFlag"
      isCloseRefresh="false"
      v-if="processSubmitDialogFlag"
      ref="processSubmitDialogRef"
      @reload="reload"
    ></process-submit-dialog>
    <!-- 开票 -->
    <addOrEditDialogNew
      ref="addOrEditDialogNewRef"
      :add-or-edit-dialog-flag.sync="addOrEditDialogNewFlag"
      v-if="addOrEditDialogNewFlag"
      :isEditType="false"
      @reload="reload"
    />
  </div>
</template>

<script>
  import { reviewStatus } from '@/enum/dict';
  import dictMixins from '@/mixins/dictMixins';
  import { finReceivablePageListAPI, receivableBill } from '@/api/financialManage/receivableManage';
  import collectionDialog from './components/collectionDialog.vue';
  import searchTable from './searchTable.vue';
  import detailDialog from './components/detailDialog.vue';
  import invoiceDetailDialog from '../invoiceManage/components/detailDialog.vue';
  import processSubmitDialog from "@/BIZComponents/processSubmitDialog/processSubmitDialog.vue";
  import tabMixins from '@/mixins/tableColumnsMixin';
  import { shippingModeOp, transactionMethodsOp, paymentTypeOp, paymentStatus, invoiceStatusOp } from '@/enum/dict.js';
  import addOrEditDialogNew from '@/views/financialManage/invoiceManage/components/addOrEditDialogNew.vue'

  export default {
    mixins: [dictMixins,tabMixins],
    components: {
      searchTable,
      collectionDialog,
      detailDialog,
      invoiceDetailDialog,
      processSubmitDialog,
      addOrEditDialogNew
    },
    //客户管理数据
    props: {
      contactData: {
        type: Object,
        default: () => {
          return {};
        }
      },
      saleOrderData: {
        type: Object,
        default: () => {
          return {};
        }
      }
    },
    data() {
      return {
        // 加载状态
        loading: false,
        collectionDialogFlag: false,
        detailDialogFlag: false,
        invoiceDetailDialogFlag: false,
        processSubmitDialogFlag: false,
        shippingModeOp, 
        transactionMethodsOp, 
        invoiceStatusOp,
        paymentTypeOp,
        paymentStatus,
        selection: [],
        addOrEditDialogNewFlag: false,
        statusList: [
          {
            label: '未收款',
            value: 0
          },
          {
            label: '部分收款',
            value: 1
          },
          {
            label: '已收全款',
            value: 2
          }
        ],
        cacheKeyUrl:'eos-03850924-receivableManage',
        columnsVersion:1,
      };
    },
    computed: {
      columns() {
        // 当columnsVersion变化时会重新计算,用作更新列配置
        const version = this.columnsVersion;
        return [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center'
          },
          {
            width: 60,
            label: '序号',
            type: 'index',
            columnKey: 'index',
            align: 'center'
          },
          {
            minWidth: 150,
            prop: 'code',
            label: '应收编码',
            align: 'center',
            slot: 'code',
            showOverflowTooltip: true
          },
          {
            minWidth: 160,
            prop: 'receiptPlanOrderNo',
            label: '收款计划编码',
            align: 'center',
            slot: 'receiptPlanOrderNo',
            showOverflowTooltip: true
          },
          {
            minWidth: 160,
            prop: 'saleOrderNo',
            label: '订单编码',
            align: 'center',
            slot: 'saleOrderNo',
            showOverflowTooltip: true
          },
          {
            minWidth: 150,
            prop: 'contractCode',
            label: '合同编码',
            align: 'center',
            slot: 'contractCode',
            showOverflowTooltip: true
          },
          {
            minWidth: 150,
            prop: 'contractNo',
            label: '合同编号',
            align: 'center',
            slot: 'contractNo',
            showOverflowTooltip: true
          },
          {
            minWidth: 150,
            prop: 'contractName',
            label: '合同名称',
            align: 'center',
            slot: 'contractName',
            showOverflowTooltip: true
          },
          {
            minWidth: 150,
            prop: 'invoiceCode',
            label: '关联发票编码',
            slot: 'invoiceCode',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            minWidth: 150,
            prop: 'invoiceNos',
            label: '发票号',
            align: 'center',
            slot: 'invoiceNos',
            showOverflowTooltip: true
          },
          {
            minWidth: 150,
            prop: 'customerNo',
            label: '客户编码',
            align: 'center',
            slot: 'customerNo',
            showOverflowTooltip: true
          },
          {
            minWidth: 150,
            prop: 'contactName',
            label: '客户名称',
            align: 'center',
            slot: 'contactName',
            showOverflowTooltip: true
          },
          {
            minWidth: 130,
            prop: 'paymentTypeName',
            label: '款项类型',
            align: 'center',
            slot: 'paymentTypeName',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'receivableTotalPrice',
            label: '应收金额',
            align: 'center',
            slot: 'receivableTotalPrice',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'receivedTotalPrice',
            label: '已收金额',
            align: 'center',
            slot: 'receivedTotalPrice',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'unreceiveTotalPrice',
            label: '未收金额',
            align: 'center',
            slot: 'unreceiveTotalPrice',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'invoiceAmount',
            label: '已开票金额',
            align: 'center',
            slot: 'invoiceAmount',
            showOverflowTooltip: true
          },
          {
            minWidth: 100,
            prop: 'unInvoiceAmount',
            label: '未开票金额',
            align: 'center',
            slot: 'unInvoiceAmount',
            showOverflowTooltip: true
          },
          {
            minWidth: 140,
            prop: 'receivableDate',
            label: '实际收款日期',
            slot: 'receivableDate',
            align: 'center',
            showOverflowTooltip: true
          },
          
        {
          minWidth: 120,
          prop: 'createUserName',
          label: '创建人',
          align: 'center',
          slot: 'createUserName',
          showOverflowTooltip: true
        },
        {
          minWidth: 120,
          prop: 'createTime',
          label: '创建时间',
          align: 'center',
          slot: 'createTime',
          showOverflowTooltip: true
        },
        {
            minWidth: 120,
            prop: 'accountingSubjectName',
            label: '会计科目',
            align: 'center',
            slot: 'accountingSubjectName',
            showOverflowTooltip: true
          },
          {
            prop: 'transactionMode',
            label: '交易方式',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 200,
            formatter: (row) => {
              return this.transactionMethodsOp.find(item => item.value == row.transactionMode)?.label || '';
            }
          },
          {
            prop: 'invoiceStatus',
            label: '开票状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return this.invoiceStatusOp.find(item => item.value == cellValue)?.label || '';
            }
          },
          {
            minWidth: 100,
            prop: 'status',
            label: '收款状态',
            align: 'center',
            slot: 'status',
            showOverflowTooltip: true,
            formatter: (_row, _column, cellValue) => {
              return this.paymentStatus.find((item) => item.value == cellValue)
                .label;
            }
          },
          {
            minWidth: 130,
            prop: 'remark',
            label: '来源备注',
            align: 'center',
            showOverflowTooltip: true
          },
        {
          prop: 'externalId',
          label: '是否推送',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 120,
          formatter: (_row, _column, cellValue) => {
            return _row.externalId? '已推送' : '未推送';
          }
        },
          {
            minWidth: 100,
            prop: 'approvalStatus',
            label: '审核状态',
            align: 'center',
            slot: 'approvalStatus',
            formatter: (_row, _column, cellValue) => {
              return reviewStatus[_row.approvalStatus];
            }
          },
          
          {
            columnKey: 'action',
            label: '操作',
            width: 150,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true,
            fixed: 'right'
          }
        ];
      },
      canAddInvoice() {
        return function(row) {
          if (!row) return false;
          return [2].includes(row.approvalStatus) && row.transactionMode == 2 && row.invoiceStatus != 2 && row.unInvoiceAmount && +row.unInvoiceAmount > 0;
        };
      }
    },
    created() {},
    methods: {
      allPushBtn() {
        const dataId = this.selection.map((v) => v.id);
        receivableBill(dataId).then((res) => {
          this.$message.success('推送成功！');
          this.reload();
        });
      },
      //新增发票
      openEdit(row) {

        this.addOrEditDialogNewFlag = true;
        this.$nextTick(() => {
          this.$refs.addOrEditDialogNewRef.createInvoice1([row], '1', 5);
        });
        // this.$refs.addOrEditDialogNewRef.$refs.form &&
        //   this.$refs.addOrEditDialogNewRef.$refs.form.clearValidate();
      },
      handleCollection(row = {}, type) {
        this.collectionDialogFlag = true;
        this.$nextTick(() => {
          this.$refs.collectionDialogRef.init(row, type);
        });
      },
      handleDetail(row = {}, type) {
        this.detailDialogFlag = true;
        this.$nextTick(() => {
          this.$refs.detailDialogRef.init(row.id);
        });
      },
      handleInvoiceDetail(row = {}, type) {
        this.invoiceDetailDialogFlag = true;
        this.$nextTick(() => {
          let params = {
            id: row.invoiceId
          };
          this.$refs.invoiceDetailDialogRef.init(params, type);
        });
      },
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        console.log(this.saleOrderData, 'this.saleOrderData');
        if (this.contactData.id) {
          where['contactId'] = this.contactData.id;
        }
        if (this.saleOrderData.id) {
          where['saleOrderCode'] = this.saleOrderData.orderNo;
        }
        return finReceivablePageListAPI({
          pageNum: page,
          size: limit,
          ...where
        });
      },
      sub(res) {
        this.processSubmitDialogFlag = true;
        let params = {
          businessId: res.id,
          businessKey: 'fin_receivable_approve',
          formCreateUserId: res.createUserId,
          variables: {
            businessCode: res.code,
            businessName: res.contactName,
            businessType: '应收'
          }
        };
        this.$nextTick(() => {
          this.$refs.processSubmitDialogRef.init(params);
        });
      },
      /* 刷新表格 */
      reload(where) {
        this.$nextTick(() => {
          this.$refs.table.reload({ page: 1, where });
        });
      }
    }
  };
</script>

<style lang="scss" scoped>
  :deep(.el-link--inner) {
    margin-left: 0px !important;
  }

  .sys-organization-list {
    height: calc(100vh - 264px);
    box-sizing: border-box;
    border-width: 1px;
    border-style: solid;
    overflow: auto;
  }

  .sys-organization-list :deep(.el-tree-node__content) {
    height: 40px;

    & > .el-tree-node__expand-icon {
      margin-left: 10px;
    }
  }
</style>
