<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" @submit.native.prevent :inline="true" v-show="showSearch" label-width="80px">
            <el-form-item label="内容类型" prop="contentType">
                <el-radio-group v-model="queryParams.contentType">
                    <el-radio-button label="">全部</el-radio-button>
                    <el-radio-button label="10620">文本</el-radio-button>
                    <el-radio-button label="10610">图文</el-radio-button>
                    <el-radio-button label="10640">图片</el-radio-button>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="关键字" prop="keyWords">
                <el-input v-model="queryParams.keyWords" placeholder="请输入关键字" clearable @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" @click="handleAdd">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" :disabled="multiple" @click="handleDelete">删除</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" fit highlight-current-row border stripe :data="replyList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="编号" align="center" prop="replyId" />
            <el-table-column label="关键字" align="center" prop="keyWords" :show-overflow-tooltip="true" />
            <el-table-column label="关键词类型" align="center" prop="keyWordsType">
                <template slot-scope="scope">
                    <el-tag :type="scope.row.keyWordsType === '10510' ? '' : 'success'">
                        <dict-tag :options="dict.type.wx_key_words_type" :value="scope.row.keyWordsType" />
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" />
            <!--                    <el-table-column label="回复类型" align="center" prop="replyType" >-->
            <!--                        <template slot-scope="scope">-->
            <!--                          <el-tag type="success">-->
            <!--                            <dict-tag :options="dict.type.wx_reply_type" :value="scope.row.replyType"/>-->
            <!--                          </el-tag>-->
            <!--                        </template>-->
            <!--                    </el-table-column>-->
            <el-table-column label="内容类型" align="center" prop="contentType">
                <template slot-scope="scope">
                    <el-tag :type="scope.row.contentType === '10610' ? '' : 'success'">
                        <dict-tag :options="dict.type.wx_content_type" :value="scope.row.contentType" />
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="状态" align="center" prop="status" width="100">
                <template slot-scope="scope">
                    <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
                </template>
            </el-table-column>
            <el-table-column label="访问次数" align="center" prop="lookNumber" />
            <el-table-column label="更新时间" align="center" prop="updateTime" :show-overflow-tooltip="true" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
                    <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
        <el-dialog :title="title" :visible.sync="open" direction="rtl" size="50%" append-to-body @opened="onDialogOpened" destroy-on-close>
            <Add_reply ref="AddReply" @closeDialog="closeDialog" @refresh="refresh" />
        </el-dialog>
    </div>
</template>

<script>
import { listReply, getReply, delReply, addReply, updateReply } from '@/api/wxManage/wxreply'
import Add_reply from '@/views/wxManage/keyWordsReply/add_reply.vue'
export default {
    name: 'Reply',
    components: { Add_reply },
    dicts: ['wx_key_words_type', 'wx_reply_type', 'wx_content_type'],
    data() {
        return {
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 微信内容管理表格数据
            replyList: [],
            // 弹出层标题
            title: '',
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                keyWords: null,
                keyWordsType: null,
                isUseKeyWords: null,
                title: null,
                author: null,
                synopsis: null,
                pictureUrl: null,
                picTextDetailContent: null,
                picTextUrl: null,
                replyType: '10730',
                contentType: '',
                lookNumber: null,
                status: null,
                createById: null,
                updateById: null
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                keyWords: [{ required: true, message: '关键字不能为空', trigger: 'blur' }],
                keyWordsType: [{ required: true, message: '关键词类型不能为空', trigger: 'change' }],
                isUseKeyWords: [{ required: true, message: '是否启用关键词 0正常，1禁用不能为空', trigger: 'blur' }],
                replyType: [{ required: true, message: '回复类型不能为空', trigger: 'change' }],
                contentType: [{ required: true, message: '内容类型不能为空', trigger: 'change' }]
            }
        }
    },
    created() {
        this.getList()
    },
    methods: {
        /** 查询微信内容管理列表 */
        getList() {
            this.loading = true
            listReply(this.queryParams).then((response) => {
                this.replyList = response.rows
                this.total = response.total
                this.loading = false
            })
        },
        // 取消按钮
        cancel() {
            this.open = false
            this.reset()
        },
        // 表单重置
        reset() {
            this.form = {
                replyId: null,
                keyWords: null,
                keyWordsType: null,
                isUseKeyWords: null,
                title: null,
                author: null,
                synopsis: null,
                pictureUrl: null,
                picTextDetailContent: null,
                picTextUrl: null,
                replyType: null,
                contentType: null,
                lookNumber: null,
                createById: null,
                createBy: null,
                createTime: null,
                updateById: null,
                updateBy: null,
                updateTime: null,
                remark: null
            }
            this.resetForm('form')
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm('queryForm')
            this.handleQuery()
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map((item) => item.replyId)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset()
            this.open = true
            this.title = '添加微信内容管理'
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset()
            const replyId = row.replyId || this.ids
            getReply(replyId).then((response) => {
                this.form = response.data
                this.open = true
                this.title = '修改微信内容管理'
            })
        },
        handleStatusChange(row) {
            let text = row.status === '0' ? '启用' : '停用'
            this.$modal
                .confirm('确认要"' + text + '"' + row.keyWords + ' 关键字吗？')
                .then(function () {
                    const data = {
                        replyId: row.replyId,
                        status: row.status
                    }
                    return updateReply(data)
                })
                .then(() => {
                    this.$modal.msgSuccess(text + '成功')
                })
                .catch(function () {
                    row.status = row.status === '0' ? '1' : '0'
                })
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs['form'].validate((valid) => {
                if (valid) {
                    if (this.form.replyId != null) {
                        updateReply(this.form).then((response) => {
                            this.$modal.msgSuccess('修改成功')
                            this.open = false
                            this.getList()
                        })
                    } else {
                        addReply(this.form).then((response) => {
                            this.$modal.msgSuccess('新增成功')
                            this.open = false
                            this.getList()
                        })
                    }
                }
            })
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const replyIds = row.replyId || this.ids
            this.$modal
                .confirm('是否确认删除微信内容管理编号为"' + replyIds + '"的数据项？')
                .then(function () {
                    return delReply(replyIds)
                })
                .then(() => {
                    this.getList()
                    this.$modal.msgSuccess('删除成功')
                })
                .catch(() => {})
        },
        onDialogOpened() {
            if (this.form.replyId !== undefined && this.form.replyId !== null) {
                this.$refs.AddReply.setInitData(this.form.replyId)
            } else {
                this.$refs.AddReply.setInitData()
            }
        },
        closeDialog() {
            this.open = false
        },
        refresh() {
            this.getList()
        },
        checkClose(done) {
            this.$confirm('是否关闭表单，关闭后数据将丢失？')
                .then(function () {
                    done()
                })
                .then(() => {})
                .catch(() => {})
        }
    }
}
</script>
