<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <div class="ele-border-lighter form-content" v-loading="loading">
        <search-form @search="reload"> </search-form>

        <!-- 数据表格 -->
        <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:finprepay:save')"
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit('add', {})"
            >
              新增
            </el-button>
          </template>

          <template v-slot:code="{ row }">
            <el-link type="primary" :underline="false" @click="openDetail(row)">
              {{ row.code }}
            </el-link>
          </template>

          <!-- 操作列 -->
          <template v-slot:action="{ row }">
            <template v-if="row.approvalStatus === 0">
              <el-link
                v-if="$hasPermission('eom:finprepay:update')"
                type="primary"
                :underline="false"
                @click="openEdit('edit', row)"
              >
                修改
              </el-link>
              <el-link
                v-if="$hasPermission('eom:finprepay:submit')"
                type="primary"
                :underline="false"
                @click="handleSubmit(row)"
              >
                提交
              </el-link>
              <el-popconfirm
                v-if="$hasPermission('eom:finprepay:delete')"
                class="ele-action"
                title="确定要删除此信息吗？"
                @confirm="remove([row.id])"
              >
                <template v-slot:reference>
                  <el-link type="danger" :underline="false">删除</el-link>
                </template>
              </el-popconfirm>
            </template>
            <template v-else-if="row.approvalStatus === 1">
              <el-link
                v-if="$hasPermission('eom:finprepay:withdraw')"
                type="warning"
                :underline="false"
                @click="handleRevoke(row)"
              >
                撤销
              </el-link>
            </template>
            <template v-else-if="row.approvalStatus === 2">
              <el-link
                type="primary"
                :underline="false"
                @click="handlePay(row)"
              >
                付款
              </el-link>
              <el-link
                v-if="$hasPermission('eom:finprepay:complete')"
                type="success"
                :underline="false"
                @click="handleComplete(row)"
              >
                完结
              </el-link>
            </template>
            <template v-else>
              <span>--</span>
            </template>
          </template>
        </ele-pro-table>
      </div>
    </el-card>

    <add-dialog ref="addDialogRef" @done="reload"></add-dialog>
    <pay-dialog ref="payDialogRef" @done="reload"></pay-dialog>
    <detail-dialog ref="detailDialogRef"></detail-dialog>
    <process-submit-dialog
      :isNotNeedProcess="false"
      :processSubmitDialogFlag.sync="processSubmitDialogFlag"
      isCloseRefresh="false"
      v-if="processSubmitDialogFlag"
      ref="processSubmitDialogRef"
      @reload="reload"
    ></process-submit-dialog>
  </div>
</template>

<script>
  import searchForm from './components/searchForm.vue';
  import addDialog from './components/addDialog.vue';
  import payDialog from './components/payDialog.vue';
  import detailDialog from './components/detailDialog.vue';
  import tabMixins from '@/mixins/tableColumnsMixin';
  import { paymentType, reviewStatus } from '@/enum/dict';
  import {
    prepaymentPageList,
    deletePrepayment,
    getPrepaymentInfo,
    withdrawPrepayment,
    completeInfo
  } from '@/api/financialManage/prepayment';
  import processSubmitDialog from '@/BIZComponents/processSubmitDialog/processSubmitDialog.vue';

  export default {
    mixins: [tabMixins],
    components: {
      searchForm,
      addDialog,
      payDialog,
      detailDialog,
      processSubmitDialog
    },
    data() {
      return {
        selection: [],
        loading: false,
        processSubmitDialogFlag: false,
        columns: [
          {
            width: 45,
            type: 'selection',
            columnKey: 'selection',
            align: 'center'
          },
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            prop: 'code',
            label: '预付款项编码',
            align: 'center',
            sortable: true,
            slot: 'code',
            showOverflowTooltip: true,
            minWidth: 180
          },
          {
            prop: 'contactName',
            label: '供应商名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 160
          },
          {
            prop: 'prepayDate',
            label: '预付日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'prepayTotalPrice',
            label: '预付金额',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'confirmedTotalPrice',
            label: '预付款确付金额',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'reconciledAmount',
            label: '已对账确付金额',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'unreconciledAmount',
            label: '未对账确付金额',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'unpaidAdvanceAmount',
            label: '预付款未付金额',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'unpaidToPayableAmount',
            label: '未付转应付金额',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'paymentStatus',
            label: '付款状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return paymentType.find(item => item.value == cellValue)?.label || '';
            }
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              if (cellValue === 0) return '未完结';
              if (cellValue === 1) return '已完结';
              if (cellValue === 2) return '已完结转应收';
              return '';
            }
          },
          {
            prop: 'approvalStatus',
            label: '审核状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return reviewStatus[_row.approvalStatus];
            }
          },
          {
            prop: 'accountingSubjectName',
            label: '会计科目',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'sourceTyp',
            label: '来源类型',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              if (cellValue === 2) return '合同';
              if (cellValue === 3) return '采购订单';
              return '';
            }
          },
          {
            prop: 'sourceCode',
            label: '来源编码',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'fundTypeName',
            label: '款项类型',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'paymentConditionName',
            label: '付款条件',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'createUserName',
            label: '创建人',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 240,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true,
            fixed: 'right'
          }
        ],
        cacheKeyUrl: 'eos-prepayment-list'
      };
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return prepaymentPageList({
          pageNum: page,
          size: limit,
          ...where
        });
      },

      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where });
      },

      // 新增编辑
      openEdit(type, row) {
        this.$refs.addDialogRef.open(type, row);
      },

      // 查看详情
      openDetail(row) {
        this.$refs.addDialogRef.open('detail', row);
      },

      // 删除
      remove(delData) {
        deletePrepayment(delData).then(() => {
          this.$message.success('删除成功！');
          this.reload();
        });
      },

      // 提交
      async handleSubmit(res) {
        // this.$confirm('确定要提交该预付款吗？', '提示', { type: 'warning' }).then(() => {
        //   // TODO: 调用提交接口
        //   this.$message.success('提交成功');
        //   this.reload();
        // });
        const data = await getPrepaymentInfo(res.id);

        this.processSubmitDialogFlag = true;
        let key = 'fin_prepay_approve';
        this.$nextTick(() => {
          let params = {
            businessId: data.id,
            businessKey: key,
            formCreateUserId: data.createUserId,
            variables: {
              businessCode: data.code,
              fundType: data.fundTypeName,
              sourceType: data.sourceType,
              businessType: '预付款项'
            }
          };
          this.$refs.processSubmitDialogRef.init(params);
        });
      
      },

      // 付款
      handlePay(row) {
        this.$refs.payDialogRef.open(row);
      },

      // 撤销
      handleRevoke(row) {
        this.$confirm('确定要撤销该预付款吗？', '提示', { type: 'warning' }).then(() => {
          // TODO: 调用撤销接口
          withdrawPrepayment({
            id: row.id
          }).then(() => {
            this.$message.success('撤销成功');
            this.reload();
          });
        })
      },

      // 完结
      handleComplete(row) {
        this.$confirm('确定要完结该预付款吗？', '提示', { type: 'warning' }).then(() => {
          completeInfo({
            id: row.id
          }).then(() => {
          // TODO: 调用完结接口
          this.$message.success('完结成功');
          this.reload();
          });
        });
      }
    }
  };
</script>

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