<!--
 * @Title: 
 * @Description: 通知公告列表
 * @Author: swp
 * @Date: 2022-08-24 10:49:21
 * @LastEditors: 
 * @LastEditTime: 2022-08-24 10:49:21
-->
<template>
    <div>

        <basic-container>
            <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" ref="crud" @row-del="rowDel"
                v-model="form" :permission="permissionList" @row-update="rowUpdate" @row-save="rowSave"
                :before-open="beforeOpen" @search-change="searchChange" @search-reset="searchReset"
                @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
                @refresh-change="refreshChange" @on-load="onLoad">
                <template slot-scope="{ row, index }" slot="menu">
                    <el-button type="text" size="small" icon="el-icon-document" @click="openAttachViewDialog(row)">查看附件
                    </el-button>
                </template>
            </avue-crud>
        </basic-container>
        <el-drawer title="附件信息" :visible.sync="attachViewDialog" v-if="attachViewDialog" :size="drawerSize"
            :direction="direction" :close-on-click-modal="false" append-to-body>
            <noticeattach :id="attachId" ref="noticeattach"></noticeattach>
        </el-drawer>
    </div>
</template>

<script>
import { getPage, remove, update, add } from "@/api/notice/notice.js";
import noticeattach from "./noticeattach.vue";
import { mapGetters } from "vuex";
let action = "/api/galaxy-business/notice/putFile";
export default {
    components: {
        noticeattach
    },
    name: 'noticelist',
    data() {
        return {
            drawerSize: '50%',
            attachViewDialog: false,
            attachId: '',
            form: {},
            query: {},
            loading: true,
            page: {
                pageSize: 10,
                currentPage: 1,
                total: 0
            },
            selectionList: [],
            option: {
                height: 'auto',
                calcHeight: 80,
                dialogWidth: 950,
                tip: false,
                searchShow: true,
                searchMenuSpan: 6,
                border: true,
                index: true,
                viewBtn: true,
                delBtn: true,
                editBtn: false,
                addBtn: true,
                selection: true,
                dialogClickModal: false,
                printBtn: false,
                excelBtn: false,
                filterBtn: false,
                refreshBtn: true,
                columnBtn: false,
                column: [
                    {
                        label: "通知标题",
                        prop: "title",
                        span: 24,
                        row: true,
                        search: true,
                        rules: [{
                            required: true,
                            message: "请输入通知标题",
                            trigger: "blur"
                        }]
                    },
                    {
                        label: "通知内容",
                        prop: "content",
                        component: 'AvueUeditor',
                        options: {
                            action: '/api/galaxy-resource/oss/endpoint/put-file',
                            props: {
                                res: "data",
                                url: "link",
                            }
                        },
                        minRows: 4,
                        span: 24,
                        overHidden: true
                    },
                    {
                        label: "通知日期",
                        prop: "releaseTime",
                        type: "date",
                        format: "yyyy-MM-dd hh:mm:ss",
                        valueFormat: "yyyy-MM-dd hh:mm:ss",
                        sapn: 12,
                        rules: [{
                            required: true,
                            message: "请输入通知日期",
                            trigger: "click"
                        }]
                    },
                    {
                        label: "是否公开",
                        prop: "publicState",
                        span: 12,
                        html: true,
                        formatter: (val) => {
                            if (val.publicState == 0) {
                                return '<b style="color:blue">内部</b>'
                            } else {
                                return '<b style="color:orangered">公开</b>'
                            }
                        },
                        value: 0,
                        type: "radio",
                        dicData: [{
                            label: '内部',
                            value: 0,
                        }, {
                            label: '公开',
                            value: 1
                        }]
                    },
                    {
                        label: "发布单位",
                        prop: "releaseDeptName",
                        span: 24,
                        viewDisplay: true,
                        addDisplay: false,
                    },
                    {
                        label: '附件',
                        prop: 'attach',
                        type: 'upload',
                        dataType: 'object',
                        loadText: '附件上传中，请稍等',
                        span: 24,
                        hide: true,
                        props: {
                            label: 'name',
                            value: 'url'
                        },
                        propsHttp: {
                            url: 'name',
                            name: 'originalName',
                            home: this.website.ossServer,
                            res: 'data',
                        },
                        action: action,
                        addDisplay: true,
                        editDisplay: false,
                        viewDisplay: false,
                    },
                    {
                        label: '附件',
                        prop: 'attachText',
                        span: 24,
                        addDisplay: false,
                        editDisplay: false,
                        viewDisplay: false,
                        overHidden: true
                    },

                ]
            },
            data: []
        };
    },
    computed: {
        ...mapGetters(["permission"]),
        permissionList() {
            console.log(JSON.stringify(this.permission))
            return {
                addBtn: this.vaildData(this.permission.notice_btn_add, false),
                viewBtn: this.vaildData(this.permission.notice_btn_view, false),
                delBtn: this.vaildData(this.permission.notice_btn_delete, false),
                editBtn: false
            };
        },
        ids() {
            let ids = [];
            this.selectionList.forEach(ele => {
                ids.push(ele.id);
            });
            return ids.join(",");
        }
    },
    methods: {
        openAttachViewDialog(row) {
            this.attachId = row.id;
            this.attachViewDialog = true;
            this.$nextTick(() => {
                this.$refs["noticeattach"].init();
            });
        },
        rowSave(row, done, loading) {
            add(row).then(() => {
                this.onLoad(this.page);
                this.$message({
                    type: "success",
                    message: "操作成功!"
                });
                done();
            }, error => {
                window.console.log(error);
                loading();
            });
        },
        rowUpdate(row, index, done, loading) {
            update(row).then(() => {
                this.onLoad(this.page);
                this.$message({
                    type: "success",
                    message: "操作成功!"
                });
                done();
            }, error => {
                window.console.log(error);
                loading();
            });
        },
        rowDel(row) {
            this.$confirm("确定将选择数据删除?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    return remove(row.id);
                })
                .then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                });
        },
        searchReset() {
            this.query = {};
            this.onLoad(this.page);
        },
        searchChange(params, done) {
            this.query = params;
            this.page.currentPage = 1;
            this.onLoad(this.page, params);
            done();
        },
        selectionChange(list) {
            this.selectionList = list;
        },
        selectionClear() {
            this.selectionList = [];
            this.$refs.crud.toggleSelection();
        },
        handleDelete() {
            if (this.selectionList.length === 0) {
                this.$message.warning("请选择至少一条数据");
                return;
            }
            this.$confirm("确定将选择数据删除?", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            })
                .then(() => {
                    return remove(this.ids);
                })
                .then(() => {
                    this.onLoad(this.page);
                    this.$message({
                        type: "success",
                        message: "操作成功!"
                    });
                    this.$refs.crud.toggleSelection();
                });
        },
        beforeOpen(done, type) {
            if (["edit", "view"].includes(type)) {
                // getNotice(this.form.id).then(res => {
                //     this.form = res.data.data;
                // });
            }
            done();
        },
        currentChange(currentPage) {
            this.page.currentPage = currentPage;
        },
        sizeChange(pageSize) {
            this.page.pageSize = pageSize;
        },
        refreshChange() {
            this.onLoad(this.page, this.query);
        },
        onLoad(page, params = {}) {
            this.loading = true;
            getPage(page.currentPage, page.pageSize, params).then(res => {
                const data = res.data.data;
                this.page.total = data.total;
                this.data = data.records;
                this.loading = false;
                this.selectionClear();
            });
        }
    }
};
</script>

<style></style>
