<!-- 招商成果 -->
<template>
    <div class="content">
        <div v-loading.fullscreen.lock="loadingFull" />
        <div class="search_box flexS">
            <el-select v-model="params.search_act" placeholder="活动名称" class="select" @change="toSearch()" clearable
                @clear="toSearch()">
                <el-option v-for="item in actList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
            <el-select v-model="params.search_company" placeholder="公司名称" class="select" @change="toSearch()" clearable
                @clear="toSearch()">
                <el-option v-for="item in companyList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
            <el-select v-model="params.search_project" placeholder="项目名称" class="select" @change="toSearch()" clearable
                @clear="toSearch()">
                <el-option v-for="item in projectList" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
            <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>
            <el-button @click="handleEdit(false)" type="primary" style="margin-left:10px;">添加</el-button>
        </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="date" label="时间" align="center" />
                <el-table-column prop="act_id" label="活动名称" align="center">
                    <template slot-scope="{ row }">
                        <span>{{ row.act && row.act.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="company_id" label="企业名称" align="center">
                    <template slot-scope="{ row }">
                        <span>{{ row.company && row.company.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="project_id" label="项目名称" align="center">
                    <template slot-scope="{ row }">
                        <span>{{ row.project && row.project.name }}</span>
                    </template>
                </el-table-column>
                <el-table-column prop="amount" 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="date">
                    <el-date-picker v-model="ruleForm.date" 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="act_id">
                    <el-select v-model="ruleForm.act_id" placeholder="请选择活动" class="select" style="width:300px">
                        <el-option v-for=" item in actList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="企业：" prop="company_id">
                    <el-select v-model="ruleForm.company_id" placeholder="请选择企业" class="select" style="width:300px"
                        @change="changeCompany">
                        <el-option v-for=" item in companyList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="项目：" prop="project_id">
                    <el-select v-model="ruleForm.project_id" placeholder="请选择项目" class="select" style="width:300px">
                        <el-option v-for=" item in comProjectList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="金额：" prop="amount">
                    <el-input placeholder="请输入金额" v-model="ruleForm.amount" style="width:300px" type="number"></el-input>
                </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 { investAchievementList, addInvestAchievement, editInvestAchievement, deleteInvestAchievement, investActAll, importInvestAchievement } from '@/api/invest'
import { projectAll, companyAll, companyProject } from '@/api/company'
import Tinymce from '@/components/Tinymce'
export default {
    data() {
        return {
            options: [], // 顶部下拉框选项
            dialogVisible: false,
            loadingFull: false,
            listData: [],
            listDataTotal: 0,
            companyList: [],
            projectList: [],
            actList: [],
            comProjectList: [],
            id: '', // 修改时的id
            isEdit: false,
            ruleForm: {
                company_id: '',
                project_id: '',
                act_id: '',
                amount: '',
                date: ''
            },
            params: {
                page_index: 1,
                page_size: 10,
                search_act: '',
                search_company: '',
                search_project: ''
            },
            rules: {
                company_id: [
                    { required: true, message: '请选择企业', trigger: 'change' }
                ],
                act_id: [
                    { required: true, message: '请选择活动', trigger: 'change' }
                ],
                date: [
                    { required: true, message: '请选择时间', trigger: 'change' }
                ],
                amount: [
                    { required: true, message: '请输入金额', trigger: 'blur' }
                ]
            }
        }
    },
    mounted() {
        this.getList()
        this.getOptions()
    },
    methods: {
        // 选择企业后项目联动
        changeCompany(id) {
            this.getComPoject(id)
        },
        getComPoject(id) {
            companyProject({ company_id: id }).then(res => {
                const { code, data, msg } = res
                if (code === 200) {
                    this.comProjectList = data
                } else {
                    this.$message.error(msg)
                }
            })
        },
        // 导入
        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
            }
            importInvestAchievement(formData).then(res => {
                if (res.code === 200) {
                    this.$message.success('导入成功！')
                    this.getList()
                } else {
                    this.$message.error(res.message || '导入失败！')
                }
            })
        },
        getOptions() {
            companyAll().then(res => {
                const { code, msg, data } = res
                if (code === 200) {
                    this.companyList = data
                } else {
                    this.$message.error(msg)
                }
            })
            projectAll().then(res => {
                const { code, msg, data } = res
                if (code === 200) {
                    this.projectList = data
                } else {
                    this.$message.error(msg)
                }
            })
            investActAll().then(res => {
                const { code, msg, data } = res
                if (code === 200) {
                    this.actList = data
                } else {
                    this.$message.error(msg)
                }
            })
        },
        // 搜索
        toSearch() {
            this.params.page_index = 1
            this.getList()
        },
        reset() {
            this.ruleForm = {
                company_id: '',
                project_id: '',
                act_id: '',
                amount: '',
                date: ''
            }
        },
        // 编辑或者添加信息
        handleEdit(isEdit, row) {
            this.dialogVisible = true
            this.isEdit = isEdit
            if (!isEdit) { // 添加
                this.reset()
            } else { // 编辑
                this.id = row.id
                const { company_id, project_id, act_id, amount, date } = row
                this.getComPoject(company_id)
                this.ruleForm = {
                    company_id,
                    project_id,
                    act_id,
                    amount,
                    date
                }
            }
        },
        // 获取列表
        async getList() {
            this.loadingFull = true
            try {
                const res = await investAchievementList(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 ? editInvestAchievement : addInvestAchievement
                    const { company_id, project_id, act_id, amount, date } = this.ruleForm
                    const data = this.isEdit ? { company_id, project_id, act_id, amount, date, 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 deleteInvestAchievement({ 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>