<template>
    <div class="list-view">
        <page-title>
            <el-button
                @click="addRow"
                type="primary"
                icon="el-icon-plus"
                :disabled="fetchingData || downloading"
                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>
            <div class="tool-wrapper">
                <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"
        >
            <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="title" label="标题" min-width="90" show-overflow-tooltip> </el-table-column>
            <el-table-column prop="description" label="描述" width="120" align="center" show-overflow-tooltip></el-table-column>

            <el-table-column prop="source" label="平台类型" min-width="100" align="center">
                <template v-slot="{ row }">
                    <el-tag  v-if="row.source">{{platformTypeMap[row.source]}}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="updateType" label="更新类型" min-width="100" align="center">
                <template v-slot="{ row }">
                    <el-tag v-if="row.updateType">{{updateTypeMap[row.updateType]}}</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="version" label="版本" width="90" align="center"></el-table-column>
            
            <el-table-column prop="startUpdate" label="是否更新" width="90" align="center">
                <template v-slot="scope">
                    <el-tag type="primary" v-if="scope.row.startUpdate">是</el-tag>
                    <el-tag type="info"  v-else>否</el-tag>
                </template>

            </el-table-column>
            <el-table-column prop="appPackage" label="app包名" width="90" align="center"></el-table-column>

            <el-table-column prop="os" label="系统版本" min-width="100" align="center"></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="180" fixed="right">
                <template v-slot="scope">
                    <el-button type="primary" size="mini" @click="editRow(scope.row)">编辑</el-button>
                    <el-button type="danger" size="mini" @click="del(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';

export default {
    components: { SortableHeader },
    name: 'AppVersionList',
    mixins: [pageableTable],
    data() {
        return {
            multipleMode: false,
            search: '',
            url: '/appVersion/all',
            downloading: false,
            formData: {
                createdAt: '',
            },
            platformTypeMap:{
                "OFFICIAL":"官方",
                "MARKET":"市场",
            },
            updateTypeMap: {
                "HOTSWAP":"热更新",
                "SOFT_UPDATE":"软更新",
                "STRONG_UPDATE":"强制更新",
            },
        };
    },
    computed: {
        selection() {
            return this.$refs.table.selection.map(i => i.id);
        }
    },
    methods: {
        beforeGetData() {
             return {
                search: this.search,
                query: { del: false, ...this.formData }
            };
        },
        addRow(){
            this.$router.push({
                path: '/appVersionEdit',
                query: {
                   
                }
            });
        },
        editRow(row){
             this.$router.push({
                path: '/appVersionEdit',
                query: {
                    id:row.id
                }
            });
        },
       del(row){
           this.$confirm(`是否删除'${row.title}'?`,'警告', { type: 'error' }).then(() => {
                this.fetchingData = true;
                this.$http.post("/appVersion/del", row, {body:"json"}).then(res => {
                    this.$message.success("删除成功");
                }).finally(() => {
                    this.fetchingData = false;
                    this.initData();
                });

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

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