<template>
    <div class="list-view">
        <page-title>
            <el-button
                @click="download('/collection/excelSecond')"
                icon="el-icon-upload2"
                :loading="downloading"
                :disabled="fetchingData"
                class="filter-item"
            >
                导出
            </el-button>
        </page-title>
        <div class="filters-container">
            <el-form :model="formData" :inline="true" @keyup.enter.native="initData">
                <el-form-item label="创建时间">
                    <created-at-picker v-model="formData.createdAt" @input="initData" name="创建"></created-at-picker>
                </el-form-item>
                <el-form-item label="资产状态">
                    <el-select v-model="formData.assetStatus" @input="initData" clearable="">
                        <el-option
                            v-for="(value, key) in assetStatusMap"
                            :label="value"
                            :value="key"
                            :key="key"
                        ></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="发布用户">
                    <user-search  v-model="formData.userId" @input="initData" ></user-search>
                </el-form-item>
                <el-form-item label="官方藏品">
                     <collection-search  v-model="formData.officialCollectionId"  @input="initData" ></collection-search>
                </el-form-item>

                <el-form-item label="藏品名称">
                    <el-input v-model="formData.name" placeholder="藏品名称" clearable=""></el-input>
                </el-form-item>

                

            </el-form>
            <div class="tool-wrapper">
                <div class="tool-right">
                    <el-button
                        type="success"
                        @click="batchCancelConsignment(multipleSelection)"
                        :disabled="fetchingData"
                        class="filter-item"
                    >
                        批量下架
                    </el-button>
                    <el-tag>总金额({{ totalAmount || 0 }}￥)</el-tag>
                    &nbsp;&nbsp;
                    <el-tag>寄售笔数({{ totalNum || 0 }})</el-tag>
                    &nbsp;&nbsp;
                    <el-tag>平均寄售价格({{ avgAmount || 0 }}￥)</el-tag>
                </div>
                <el-input
                    placeholder="搜索..."
                    v-model="search"
                    clearable
                    class="filter-item search"
                    @keyup.enter.native="initData"
                >
                    <el-button @click="initData" slot="append" icon="el-icon-search"> </el-button>
                </el-input>
            </div>
        </div>
        <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"
            v-loading="fetchingData"
            @selection-change="handleSelectionChange"
        >
            <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="name" label="藏品名称" min-width="140" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="pic" label="图片" width="90" align="center">
                <template v-slot="{ row }">
                    <el-image
                        style="width: 30px; height: 30px"
                        :src="row.pic[0].thumb || row.pic[0].url"
                        fit="cover"
                        :preview-src-list="row.pic.map(i => i.thumb || i.url)"
                    ></el-image>
                </template>
            </el-table-column>
            <el-table-column prop="price" label="价格" width="90"> 
                <template v-slot:header="{ column }">
                    <sortable-header :column="column" :current-sort="sort" @changeSort="changeSort"> </sortable-header>
                </template>
            </el-table-column>
            <el-table-column prop="number" label="资产编号" width="120"> 
                 <template v-slot:header="{ column }">
                    <sortable-header :column="column" :current-sort="sort" @changeSort="changeSort"> </sortable-header>
                </template>
            </el-table-column>
            <el-table-column prop="assetStatus" label="资产状态" width="90" align="center">
                 <template v-slot="scope">
                    <el-tag :type="assetStatusMap[scope.row.assetStatus].type" v-if="assetStatusMap[scope.row.assetStatus]">{{
                        assetStatusMap[scope.row.assetStatus]
                    }}</el-tag>
                </template>
            </el-table-column>

            <el-table-column prop="assetId" label="资产ID" width="90"> </el-table-column>
            <el-table-column prop="userId" label="发布用户ID" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="phone" label="用户手机号" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="nickname" label="用户昵称" min-width="100" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="officialCollectionId" label="官方藏品ID" min-width="80" > </el-table-column>
            <el-table-column prop="createdAt" label="寄售时间" width="150">
                <template v-slot:header="{ column }">
                    <sortable-header :column="column" :current-sort="sort" @changeSort="changeSort"> </sortable-header>
                </template>
            </el-table-column>
            <el-table-column label="操作" min-width="120" fixed="right">
                <template v-slot="scope">
                    <el-button type="primary" size="mini" @click="batchCancelConsignment([scope.row])">下架</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination-wrapper">
            <el-pagination
                background
                @size-change="onSizeChange"
                @current-change="onCurrentChange"
                :current-page="page"
                :page-sizes="[10, 20, 30, 40, 50]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="totalElements"
            >
            </el-pagination>
        </div>
    </div>
</template>
<script>
import pageableTable from '@/mixins/pageableTable';
import SortableHeader from '../components/SortableHeader.vue';

import {assetStatusMap} from '@/utils/constant.js';

export default {
    components: { SortableHeader },
    name: 'CollectionSecondList',
    mixins: [pageableTable],
    data() {
        return {
            multipleMode: true,
            search: '',
            url: '/collection/allSecondCollection',
            downloading: false,
            typeOptions: [
                { label: '默认', value: 'DEFAULT' },
                { label: '盲盒', value: 'BLIND_BOX' },
                { label: '拍卖', value: 'AUCTION' }
            ],
            sourceOptions: [
                { label: '官方', value: 'OFFICIAL' },
                { label: '用户铸造', value: 'USER' },
                { label: '转让', value: 'TRANSFER' }
            ],
            formData: {
                officialCollectionId:"",
                createdAt: '',
                userId: '',
                name:"",
                assetStatus: ""
            },
            multipleSelection:[],
            assetStatusMap,
            totalAmount:0,
            totalNum:0,
            avgAmount:0
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        typeFormatter(row, column, cellValue, index) {
            let selectedOption = this.typeOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        sourceFormatter(row, column, cellValue, index) {
            let selectedOption = this.sourceOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        beforeGetData() {
             return {
                search: this.search,
                query: { del: false, ...this.formData }
            };
        },
         afterGetData(res) {
            //获取总金额
            this.totalAmount = res.totalAmount || 0;
            this.totalNum = res.totalElements || 0;
            if(this.totalAmount > 0 && this.totalNum > 0){
                this.avgAmount = Math.ceil(this.totalAmount / this.totalNum * 100) / 100;
            }else{
                this.avgAmount = 0;
            }
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        batchCancelConsignment(dataList){
            if(dataList == null || dataList.length == 0){
                this.$message.error("请选择一个需要下架的藏品");
                return;
            }

            this.$confirm("是否下架指定的" + dataList.length + "个藏品",'警告', { type: 'error' }).then(() => {
                this.fetchingData = true;
                this.$http.post("/collection/batchCancelConsignment", {dataList}, {body:"json"}).then(res => {
                    let count = res.count;
                    this.$message.success("成功下架" + count + "个藏品");
                }).finally(() => {
                    this.fetchingData = false;
                    this.initData();
                });

            });


        }
    }
};
</script>
<style lang="less" scoped>
.tool-wrapper {
    display: block;
    overflow: hidden;

    .tool-right {
        float: left;
    }
}
</style>
