<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:finadvance: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:finadvance:update')"
                type="primary"
                :underline="false"
                @click="openEdit('edit', row)"
              >
                修改
              </el-link>
              <el-link
                v-if="$hasPermission('eom:finadvance:submit')"
                type="primary"
                :underline="false"
                @click="handleSubmit(row)"
              >
                提交
              </el-link>
              <el-popconfirm
                v-if="$hasPermission('eom:finadvance: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:finadvance: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="handleCollect(row)"
              >
                收款
              </el-link>
              <el-link
                v-if="$hasPermission('eom:finadvance:complete') && row.status == 0"
                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>
    <collect-dialog ref="collectDialogRef" @done="reload"></collect-dialog>
  </div>
</template>

<script>
  import searchForm from './components/searchForm.vue';
  import addDialog from './components/addDialog.vue';
  import collectDialog from './components/collectDialog.vue';
  import tabMixins from '@/mixins/tableColumnsMixin';
  import { paymentStatus, reviewStatus } from '@/enum/dict';
  import {
    advanceReceiptPageList,
    deleteAdvanceReceipt,submit,approve, withdraw, completeInfo
  } from '@/api/financialManage/advanceReceipt';
  
  export default {
    mixins: [tabMixins],
    components: {
      searchForm,
      addDialog,
      collectDialog
    },
    data() {
      return {
        selection: [],
        loading: 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: '预收款项编码',
            slot: 'code',
            align: 'center',
            sortable: true,
            showOverflowTooltip: true,
            minWidth: 180
          },
          {
            prop: 'contactName',
            label: '客户名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 160
          },
          {
            prop: 'advanceDate',
            label: '预收日期',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 120
          },
          {
            prop: 'advanceTotalPrice',
            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: 'uncollectedAdvanceAmount',
            label: '预收款未收金额',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'uncollectedToReceivableAmount',
            label: '未收转应收金额',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 140
          },
          {
            prop: 'collectionStatus',
            label: '收款状态',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
            formatter: (_row, _column, cellValue) => {
              return (
                paymentStatus.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: 'sourceType',
            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: 'collectionConditionName',
            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: 260,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true,
            fixed: 'right'
          }
        ],
        cacheKeyUrl: 'eos-advanceReceipt-list'
      };
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where }) {
        return advanceReceiptPageList({
          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) {
        deleteAdvanceReceipt(delData).then(() => {
          this.$message.success('删除成功！');
          this.reload();
        });
      },

      // 提交
      handleSubmit(row) {
        this.$confirm('确定要提交该预收款吗？', '提示', {
          type: 'warning'
        }).then(() => {
          submit({
            id: row.id
          }).then((res) => {
            // TODO: 调用提交接口
            this.$message.success('提交成功');
            this.reload();
          });
        }).catch(() => {
          // this.$message.info('已取消提交');
        });;
      },

      // 收款
      handleCollect(row) {
        this.$refs.collectDialogRef.open(row);
      },

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

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

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