<!-- 企业产值 -->
<template>
    <div class="content">
        <div v-loading.fullscreen.lock="loadingFull" />
        <div class="search_box flexS">
            <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-date-picker v-model="params.search_date" type="datetime" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                placeholder="选择日期时间" style="width:300px" @change="toSearch()">
            </el-date-picker>
            <el-button @click="handleEdit(false)" type="primary" style="margin-right:10px;">添加</el-button>
            <el-upload class="upload-demo" action="" :http-request="uploadFile" :multiple="false" :show-file-list="false">
                <el-button type="primary">导入</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="date" label="日期" align="center" />
                <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="output" label="企业产值" align="center" />
                <el-table-column prop="num" label="研发人员数数量" align="center" />
                <el-table-column prop="expend" label="研究经费支出" align="center" />

                <el-table-column prop="industrial_output" 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" format="yyyy-MM-dd" value-format="yyyy-MM-dd"
                        placeholder="选择日期时间" style="width:300px">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="企业产值：" prop="output">
                    <el-input v-model="ruleForm.output" placeholder="请输入项目产值" style="width:300px" type="number" />
                </el-form-item>
                <el-form-item label="工业产值：" prop="industrial_output">
                    <el-input v-model="ruleForm.industrial_output" placeholder="请输入项目产值" style="width:300px"
                        type="number" />
                </el-form-item>
                <el-form-item label="研究经费支出：" prop="expend">
                    <el-input v-model="ruleForm.expend" placeholder="请输入研究经费支出" style="width:300px" type="number" />
                </el-form-item>
                <el-form-item label="研发人员数量：" prop="num">
                    <el-input v-model="ruleForm.num" placeholder="请输入研发人员数量" style="width:300px" type="number" />
                </el-form-item>
                <el-form-item label="所属企业:" prop="company_id">
                    <el-select v-model="ruleForm.company_id" placeholder="请选择所属企业" style="width:300px">
                        <el-option v-for="item in companyList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </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 { infoList, addInfo, editInfo, deleteInfo, importInfo, companyAll } from '@/api/company'
export default {
    data() {
        return {
            options: [], // 顶部下拉框选项
            categoryList: [], // 类型列表
            dialogVisible: false,
            loadingFull: false,
            listData: [],
            listDataTotal: 0,
            companyList: [], //企业列表
            id: '',// 当前修改产值的id
            isEdit: false,
            ruleForm: {
                date: '',
                company_id: '',
                output: '',
                expend: '',
                num: '',
                industrial_output: ''
            },
            params: {
                page_index: 1,
                page_size: 10,
                search_date: '',
                search_company: ''
            },
            rules: {
                date: [
                    { required: true, message: '请选择时间', trigger: 'change' }
                ],
                output: [
                    { required: true, message: '请输入企业产值', trigger: 'blur' }
                ],
                num: [
                    { required: true, message: '请输入研发人员数量', trigger: 'blur' }
                ],
                expend: [
                    { required: true, message: '请输入研究经费支出', trigger: 'blur' }
                ],
                industrial_output: [
                    { required: true, message: '请输入工业产值', trigger: 'blur' }
                ],
                company_id: [
                    { required: true, message: '请选择所属企业', trigger: 'change' }
                ]
            }
        }
    },
    mounted() {
        this.getList()
        this.getCompany()
    },
    methods: {
        // 获取企业列表
        async getCompany() {
            try {
                const res = await companyAll()
                const { msg, code, data } = res
                if (code === 200) {
                    this.companyList = data
                } else {
                    this.$message.error(msg)
                }
            } catch (e) {
                console.log(`获取列表失败:${e}`)
            }
        },
        // 修改类型
        editItem(fid, isEdit, info, idx) {
            this.addItem(fid, isEdit, info, idx)
        },
        // 搜索
        toSearch() {
            this.params.page_index = 1
            this.getList()
        },
        // 导入
        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
            }
            importInfo(formData).then(res => {
                if (res.code === 200) {
                    this.$message.success('导入成功！')
                    this.getList()
                } else {
                    this.$message.error(res.message || '导入失败！')
                }
            })
        },
        reset() {
            this.ruleForm = {
                date: '',
                company_id: '',
                output: '',
                expend: '',
                num: '',
                industrial_output: ''
            }
        },
        // 编辑或者添加人才信息
        handleEdit(isEdit, row) {
            this.dialogVisible = true
            this.isEdit = isEdit
            if (!isEdit) { // 添加
                this.reset()
            } else { // 编辑
                this.id = row.id
                const { date, company_id, output, num, expend, industrial_output } = row
                this.ruleForm = {
                    date,
                    company_id,
                    output,
                    expend,
                    num,
                    industrial_output
                }
            }
        },
        // 获取列表
        async getList() {
            this.loadingFull = true
            try {
                const res = await infoList(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 ? editInfo : addInfo
                    const { date, company_id, output, num, expend, industrial_output } = this.ruleForm
                    const data = this.isEdit ? { date, company_id, output, expend, num, industrial_output, 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 deleteInfo({ 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>