<template>
    <div class="app-container">
        <div class="searchPanel">
            <el-form :inline="true" v-show="showSearch" class="searchPanelForm">
                <el-form-item label="反馈时间:" prop="postName" style="width: 330px">
                    <el-date-picker v-model="dateRange" @change="handleDateChange" value-format="YYYY-MM-DD"
                        type="daterange" range-separator="-" start-placeholder="开始日期"
                        end-placeholder="结束日期"></el-date-picker>
                </el-form-item>
            </el-form>
        </div>
        <el-card class="mt20">
            <el-row :gutter="10" class="my_row">
                <el-col :span="12">
                    <el-form :inline="true" class="searchInputForm">
                        <el-form-item label="">
                            <el-input v-model="queryParams.feedbackUser" placeholder="请输入反馈人" :prefix-icon="Search"
                                style="width: 300px;" />
                        </el-form-item>
                    </el-form>
                </el-col>
                <el-col :span="12" style="text-align: right;">
                    <el-button type="primary" class="primaryBtn" @click="handleQuery">查询</el-button>
                    <el-button type="primary" class="primaryBtn" @click="resetQuery">重置</el-button>
                </el-col>
            </el-row>

            <el-table v-loading="loading" :data="feedbackList" height="calc(100vh - 308px)">
                <el-table-column label="序号" align="center" width="80">
                    <template #default="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column label="反馈内容" align="left" prop="content" />
                <el-table-column label="反馈人" align="center" prop="feedbackUser" width="150" />
                <el-table-column label="反馈时间" align="center" prop="createTime" width="210">
                    <template #default="scope">
                        <span>{{ parseTime(scope.row.createTime) }}</span>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="130" align="center" class-name="small-padding fixed-width">
                    <template #default="scope">
                        <!-- <el-button link type="primary" @click="handleUpdate(scope.row)"
                            v-hasPermi="['problemFeedback:edit']">修改</el-button> -->
                        <el-button link type="primary" @click="handleDelete(scope.row)"
                            v-hasPermi="['problemFeedback:remove']">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <pagination :total="total" v-model:page="queryParams.pageIndex" v-model:limit="queryParams.pageSize"
                @pagination="getBusProblemFeedbackPage" />
        </el-card>

        <!-- 添加反馈对话框 -->
        <el-dialog title="修改问题反馈" v-model="open" width="800px" align-center class="my_dialog" append-to-body
            :destroy-on-close="true" :close-on-click-modal="false">
            <el-form ref="feedBackRef" :model="form" :rules="rules" label-width="120px" label-position="top"
                class="myFeedBackForm">
                <el-form-item label="反馈人" prop="feedbackUser">
                    <el-input v-model="form.feedbackUser" placeholder="请输入反馈人姓名" />
                </el-form-item>
                <el-form-item label="输入反馈内容" prop="content">
                    <el-input v-model="form.content" style="width: 100%" :rows="12" type="textarea"
                        placeholder="欢迎提出您在使用过程中遇到的问题或宝贵建议" />
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button class="my-cancel-btn" @click="cancel">取 消</el-button>
                    <el-button class="my-confirm-btn" type="primary" @click="submitForm">确 定</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Post">
import { onMounted, ref } from 'vue';
import { Search } from '@element-plus/icons-vue'
import { busProblemFeedbackPage, updateBusProblemFeedback, getBusProblemFeedback, deleteBusProblemFeedback } from "@/api/problemFeedback"
import { useBackgroundStore } from '@/store/modules/background'
import otherbg from '@/assets/images/otherbg.png'
const bgStore = useBackgroundStore()

const { proxy } = getCurrentInstance()

const feedbackList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const dateRange = ref([])
const data = reactive({
    form: {},
    queryParams: {
        pageIndex: 1,
        pageSize: 10,
        feedbackUser: undefined,
        startDate: undefined,
        endDate: undefined
    },
    rules: {
        content: [{ required: true, message: "反馈内容不能为空", trigger: "blur" }]
    }
})

const { queryParams, form, rules } = toRefs(data)
// 处理日期变化
const handleDateChange = (val) => {
    dateRange.value = val;
    queryParams.value.startDate = val[0]
    queryParams.value.endDate = val[1]
};
/** 查询反馈问题列表 */
const getBusProblemFeedbackPage = () => {
    loading.value = true
    busProblemFeedbackPage(queryParams.value).then(response => {
        feedbackList.value = response.data.list
        total.value = response.data.total
        loading.value = false
    })
}

/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageIndex = 1
    getBusProblemFeedbackPage()
}

/** 重置按钮操作 */
const resetQuery = () => {
    queryParams.value = {
        pageIndex: 1,
        pageSize: 10,
        feedbackUser: undefined,
        startDate: undefined,
        endDate: undefined
    }
    handleQuery()
}

// 修改问题
const handleUpdate = (row) => {
    form.value = {
        feedbackUser: undefined,
        content: undefined
    }
    getBusProblemFeedback(row.id).then(res => {
        form.value = res.data
        open.value = true
    })
}


const cancel = () => {
    open.value = false
    form.value = {}
}
const submitForm = () => {
    proxy.$refs["feedBackRef"].validate(valid => {
        if (valid) {
            updateBusProblemFeedback(form.value).then(response => {
                proxy.$modal.msgSuccess("修改成功")
                getBusProblemFeedbackPage()
                open.value = false
                form.value = {}
                proxy.resetForm("feedBackRef")
            })
        }
    })
}

/** 删除按钮操作 */
const handleDelete = (row) => {
    proxy.$modal.confirm('是否确认删除该问题吗？').then(function () {
        return deleteBusProblemFeedback(row.id)
    }).then(() => {
        getBusProblemFeedbackPage()
        proxy.$modal.msgSuccess("删除成功")
    }).catch(() => { })
}

// 初始化
onMounted(() => {
    bgStore.setBgImage(otherbg)
    getBusProblemFeedbackPage()
}); 
</script>
