<!-- 招商活动 -->
<template>
    <div class="content">
        <div v-loading.fullscreen.lock="loadingFull" />
        <div class="search_box flexS">
            <el-input v-model="params.search" clearable placeholder="请输入活动名称" prefix-icon="el-icon-search"
                @keyup.enter.native="toSearch()" @clear="toSearch()" style="width:320px;" />
            <el-button type="primary" icon="el-icon-search" :loading="loadingFull" @click="toSearch()">搜索</el-button>
            <el-button @click="handleEdit(false)" type="primary">添加</el-button>
            <el-upload class="upload-demo" action="" :http-request="uploadFile" :multiple="false" :show-file-list="false">
                <el-button type="primary" style="margin:0 10px;">导入</el-button>
            </el-upload>
        </div>
        <div class="table_box">
            <el-table ref="list" :data="listData" align="center" :header-cell-style="{ background: '#f8fbfc' }"
                class="tableBorder" border stripe>
                <el-table-column prop="name" label="名称" align="center" />
                <el-table-column prop="address" label="地址" align="center" />
                <el-table-column prop="start_time" label="开始时间" align="center" />
                <el-table-column prop="end_time" label="结束时间" align="center" />
                <el-table-column label="操作" align="center" width="280">
                    <template slot-scope="{row}">
                        <el-button type="text" size="mini" @click="handleEdit(true, row)">编辑</el-button>
                        <el-button type="text" size="mini" @click="handleDelete(row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <el-drawer :title="isEdit ? '修改招商活动信息' : '添加招商活动信息'" :visible.sync="dialogVisible" size="45%">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
                <el-form-item label="名称：" prop="name">
                    <el-input v-model="ruleForm.name" placeholder="请输入活动名称" style="width:300px" />
                </el-form-item>
                <el-form-item label="活动地址：" prop="address">
                    <el-input v-model="ruleForm.address" placeholder="请输入活动名称" style="width:300px" />
                </el-form-item>
                <el-form-item label="开始时间：" prop="start_time">
                    <el-date-picker v-model="ruleForm.start_time" type="datetime" placeholder="选择日期时间" style="width:300px"
                        value-format="yyyy-MM-dd" format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间：" prop="end_time">
                    <el-date-picker v-model="ruleForm.end_time" type="datetime" placeholder="选择日期时间" style="width:300px"
                        value-format="yyyy-MM-dd" format="yyyy-MM-dd">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="简介：" prop="introduction">
                    <textarea name="" id="" cols="50" rows="10" v-model="ruleForm.introduction"></textarea>
                </el-form-item>
                <el-form-item>
                    <el-button @click="dialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="submitForm('ruleForm')">{{ !isEdit ? '确 定' : '保存' }}</el-button>
                </el-form-item>
            </el-form>

        </el-drawer>
        <div class="paging_box">
            <pagination :total="listDataTotal" :limit="params.page_size" :page="params.page_index"
                @pagination="changePagination" @update:limit="changePagination" />
        </div>
    </div>
</template>
<script>
import { investActList, investActAll, addInvestAct, editInvestAct, deleteInvestAct ,importInvestAct} from '@/api/invest'
import Tinymce from '@/components/Tinymce'
export default {
    data() {
        return {
            options: [], // 顶部下拉框选项
            dialogVisible: false,
            loadingFull: false,
            listData: [],
            listDataTotal: 0,
            id: '', // 修改时的id
            isEdit: false,
            ruleForm: {
                name: '',
                start_time: '',
                end_time: '',
                address: '',
                introduction: ''
            },
            params: {
                page_index: 1,
                page_size: 10,
                search: '',
            },
            rules: {
                name: [
                    { required: true, message: '请输入活动标题', trigger: 'blur' }
                ],
                start_time: [
                    { required: true, message: '请选择开始时间', trigger: 'change' }
                ],
                end_time: [
                    { required: true, message: '请选择结束时间', trigger: 'change' }
                ],
                introduction: [
                    { required: true, message: '请输入活动简介', trigger: 'change' }
                ],
                address: [
                    { required: true, message: '请输入活动地址', trigger: 'blur' }
                ]
            }
        }
    },
    mounted() {
        this.getList()
    },
    methods: {
        // 导入
        uploadFile(params) {
            const file = params.file
            var formData = new FormData()
            formData.append('file', file)
            const isExcel = file.name.endsWith('.xlsx') || file.name.endsWith('.xls')
            console.log(file.name, 'file')
            if (!isExcel) {
                this.$message.error('仅支持上传 .xlsx 或 .xls 格式的文件!')
                return
            }
            importInvestAct(formData).then(res => {
                if (res.code === 200) {
                    this.$message.success('导入成功！')
                    this.getList()
                } else {
                    this.$message.error(res.message || '导入失败！')
                }
            })
        },
        // 搜索
        toSearch() {
            this.params.page_index = 1
            this.getList()
        },
        reset() {
            this.ruleForm = {
                name: '',
                start_time: '',
                end_time: '',
                address: '',
                introduction: ''
            }
        },
        // 编辑或者添加人才信息
        handleEdit(isEdit, row) {
            this.dialogVisible = true
            this.isEdit = isEdit
            if (!isEdit) { // 添加
                this.reset()
            } else { // 编辑
                this.id = row.id
                const { name, start_time, end_time, address, introduction } = row
                this.ruleForm = {
                    name,
                    start_time,
                    end_time,
                    address,
                    introduction
                }
            }
        },
        // 获取列表
        async getList() {
            this.loadingFull = true
            try {
                const res = await investActList(this.params)
                const { message, code, data } = res
                if (code === 200) {
                    const { list, total } = data
                    this.listData = list
                    this.listDataTotal = total
                } else {
                    this.$message.error(message)
                }
            } catch (e) {
                console.log(`获取列表失败:${e}`)
                this.loadingFull = false
            }
            this.loadingFull = false
        },
        // 添加/编辑事件
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    const http = this.isEdit ? editInvestAct : addInvestAct
                    const { name, start_time, end_time, address, introduction } = this.ruleForm
                    const data = this.isEdit ? { name, start_time, end_time, address, introduction, id: this.id } : this.ruleForm
                    http(data).then(res => {
                        if (res.code === 200) {
                            this.$message.success(this.isEdit ? '修改成功！' : '添加成功!')
                            this.getList()
                            this.dialogVisible = false
                        } else {
                            this.$message.error(this.isEdit ? '修改失败！' : '添加失败!')
                            this.$message.error(res.msg)
                        }
                    })
                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
        // 删除确认
        handleDelete(row) {
            const { id } = row
            this.$confirm('确认要删除该活动吗?', '删除确认', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.deleteEvent(id)
            })
        },
        // 删除事件
        async deleteEvent(id) {
            const res = await deleteInvestAct({ id })
            try {
                const { message, code } = res
                if (code === 200) {
                    this.$message.success('删除成功！')
                    this.getList()
                } else {
                    this.$message.error(message)
                }
            } catch (e) {
                this.$message.error(e.message)
            }
        },
        // 分页页码变化
        changePagination(data) {
            const { page, limit } = data
            this.params.page_size = limit
            this.params.page_index = page
            this.getList()
        }
    }

}
</script>
<style lang="scss" scoped>
.el-input,
textarea {
    width: 350px;
}
</style>