<template>
    <div class="list-view">
        <el-table
            :data="tableData"
            row-key="id"
            ref="table"
            header-row-class-name="table-header-row"
            header-cell-class-name="table-header-cell"
            row-class-name="table-row"
            cell-class-name="table-cell"
            :height="tableHeight"
        >
            <el-table-column v-if="multipleMode" align="center" type="selection" width="50"> </el-table-column>
            <!-- <el-table-column prop="id" label="ID" width="100"> </el-table-column> -->
            <!-- <el-table-column prop="patentId" label="专利id"> </el-table-column> -->
            <el-table-column prop="feeType" label="费用种别" min-width="80px"> </el-table-column>
            <el-table-column prop="feeName" label="费用种别" min-width="80px"> </el-table-column>
            <el-table-column prop="status" label="支付状态" min-width="80px">
                <template slot-scope="{ row }">
                    <el-tag :type="row.status === 'UNPAID' ? 'warning' : ''">{{ statusFormatter(row.status) }}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="patentPartnerId" label="专利权人" min-width="120px"> </el-table-column>
            <el-table-column prop="payPartnerId" label="支付对象" min-width="120px"> </el-table-column>
            <el-table-column prop="amount" label="金额" min-width="120px"> </el-table-column>
            <el-table-column prop="currencyMaintenanceId" label="币种" min-width="100px"> </el-table-column>
            <el-table-column prop="paymentDeadline" label="支付期限" min-width="80px"> </el-table-column>
            <el-table-column prop="remark" label="账单"> </el-table-column>
            <el-table-column prop="billImg" label="账单图片">
                <template slot-scope="{ row }">
                    <el-image
                        style="width: 36px; height: 36px;"
                        :src="row.billImg"
                        fit="cover"
                        :preview-src-list="[row.billImg]"
                    ></el-image>
                </template>
            </el-table-column>
            <el-table-column prop="paymentDate" label="支付日期" min-width="80px">
                <template slot-scope="{ row }">
                    <span v-if="row.status === 'UNPAID'" class="warning">未支付</span>

                    <span v-else>{{ row.paymentDate }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="invoiceReturn" label="是否发票回传">
                <template slot-scope="{ row }">
                    <el-tag v-if="row.invoiceReturn" :type="row.invoiceReturn ? '' : 'info'">{{
                        row.invoiceReturn
                    }}</el-tag>
                </template>
            </el-table-column>
            <!-- <el-table-column prop="userId" label="userId"> </el-table-column> -->
            <el-table-column label="操作" align="center" fixed="right" min-width="150px">
                <template>
                    <el-button class="" type="primary" size="mini" plain>支付费用</el-button>
                    <el-button type="danger" size="mini" plain>编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>
<script>
import { mapState } from 'vuex';
import pageableTable from '@/mixins/pageableTable';

export default {
    name: 'FeeList',
    mixins: [pageableTable],
    props: {
        logoPatentInfo: {
            type: Object,
            default: () => {
                return {};
            }
        }
    },
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/fee/all',
            downloading: false,
            statusOptions: [
                { label: '已支付', value: 'PAID' },
                { label: '未支付', value: 'UNPAID' }
            ],
            pageSize: 100
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        init() {
            this.getData();
            this.$nextTick(() => {
                setTimeout(() => {
                    this.tableHeight = document.querySelector('.el-table').getBoundingClientRect().height + 20;
                }, 100);
            });
        },
        statusFormatter(cellValue) {
            let selectedOption = this.statusOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        beforeGetData() {
            return {
                query: {
                    patentId: this.logoPatentInfo.id
                }
            };
        },
        toggleMultipleMode(multipleMode) {
            this.multipleMode = multipleMode;
            if (!multipleMode) {
                this.$refs.table.clearSelection();
            }
        },
        addRow() {
            this.$refs.feeAdd.show = true;
        },
        editRow(row) {
            this.$router.push({
                path: '/feeEdit',
                query: {
                    id: row.id
                }
            });
        },
        download() {
            this.downloading = true;
            this.$axios
                .get('/fee/excel', {
                    responseType: 'blob',
                    params: { size: 10000 }
                })
                .then(res => {
                    console.log(res);
                    this.downloading = false;
                    const downloadUrl = window.URL.createObjectURL(new Blob([res.data]));
                    const link = document.createElement('a');
                    link.href = downloadUrl;
                    link.setAttribute('download', res.headers['content-disposition'].split('filename=')[1]);
                    document.body.appendChild(link);
                    link.click();
                    link.remove();
                })
                .catch(e => {
                    console.log(e);
                    this.downloading = false;
                    this.$message.error(e.error);
                });
        },
        operation1() {
            this.$notify({
                title: '提示',
                message: this.selection
            });
        },
        operation2() {
            this.$message('操作2');
        },
        deleteRow(row) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/fee/del/${row.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.getData();
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        this.$message.error(e.error);
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped>
.list-view {
    flex-grow: 1;
    padding: 20px 0;
}
</style>
