<template>
    <div class="main-content">
       <div class="area-form">
       </div>
       <div class="button-tab">
            <el-button size="default" type="primary" @click="goAdd">添加奖励规则</el-button>
       </div>
       <div class="area-table">
           <el-table :data="tableData.data" size="default" border
               :header-cell-style="{background: '#EEE'}">
               <el-table-column prop="title" label="规则名" width="360"></el-table-column>
               <el-table-column prop="isDefault" :formatter="statusFormatter" label="是否默认"></el-table-column>
               <el-table-column prop="creatAt" label="创建时间"></el-table-column>
               <el-table-column prop="updateAt" label="更新时间"></el-table-column>
               <el-table-column prop="action" label="操作" fixed="right" width="240">
                   <template #default="scope">
                        <el-link type="primary" @click="handlerEdit(scope.row.id)">编辑</el-link>
                        <el-link type="primary" @click="setDefault(scope.row.id)" style="margin-left: 8px" v-if="scope.row.isDefault == null">设为默认</el-link>
                        <el-link type="danger" @click="singleDel(scope.row.id)" style="margin-left: 8px" v-if="scope.row.isDefault == null">删除</el-link>
                   </template>
               </el-table-column>
           </el-table>
            <el-pagination
                v-model:currentPage="pagination.current"
                v-model:page-size="pagination.pageSize"
                :page-sizes="[10, 20, 30, 40]"
                background
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.total"
            />
       </div>
       <el-drawer v-model="drawerVisible">
            <template #header>
                <span style="font-size: 16px;font-weight: bold">{{drawerData.activeType==1?'查看部门':'查看员工'}}</span>
            </template>
            <template #footer>
                <el-button type="primary" @click="drawerVisible = false" size="default">确定</el-button>
            </template>
       </el-drawer>
    </div>
</template>

<script>
import {defineComponent, reactive, ref, watch, getCurrentInstance} from 'vue'
import {statusFormatter} from './config.js'
export default defineComponent({
    name: 'v-questList',
    setup() {
        const form = reactive({
            type: undefined,
            series: undefined,
            tag: ''
        });
        const tableData = reactive({
            data: [],
        });
        const pagination = reactive({
            total: 0,
            current: 1,
            pageSize: 10
        });
        const setForm = reactive({
            name: ''
        });
        const drawerVisible = ref(false);
        const { proxy } = getCurrentInstance();
        const drawerData = reactive({
            activeId: undefined, //抽屉数据的当前id
            activeType: undefined, //抽屉数据的当前类型 1,部门 2,员工
            data: [],
        });
        return {
            form,setForm,proxy,
            tableData,
            pagination,
            statusFormatter,
            drawerVisible,drawerData
        }
    },
    mounted() {
        this.getData();
        watch(() => (this.pagination.pageSize), () => {
            this.getData()
        });
        watch(() => (this.pagination.current), () => {
            this.getData()
        })
    },
    methods: {
        getData() {
            let obj = Object.assign({pageNum: this.pagination.current,pageSize: this.pagination.pageSize})
            this.proxy.$post("/rewardRuleTemplate/list", obj).then(res => {
                const data = res.data;
                this.tableData.data = data;
            }).catch(() => {});
        },
        addSet() {

        },
        handlerEdit(value) {

        },
        goAdd() {
            this.$router.push({name: 'AddReward'})
        },
        //设为默认
        setDefault(id) {
            this.$alert(
                '是否设为默认?', //文字
                '设为默认', //标题
                {type: 'info'}
            ).then(() => {
                this.proxy.$post("/referenceRuleTemplate/setDefault", {id: id}).then(res => {
                    this.getData();
                    this.$message.success(res.data)
                }).catch(() => {});
            }).catch(() => {})
        },
        //删除
        singleDel(id) {
            this.$alert(
                '是否删除?', //文字
                '删除', //标题
                {type: 'warning'}
            ).then(() => {
                this.proxy.$post("/referenceRuleTemplate/del", {id: id}).then(res => {
                    this.getData();
                    this.$message.success(res.data)
                }).catch(() => {});
            }).catch(() => {})
        }
    }
})
</script>

<style lang="less" scoped>
.area-table {
    margin-top: 16px
}
</style>