<template>
    <!-- 国网模板 -->
    <div class="app-container">
        <el-form :model="queryParams" ref="login" label-width="0px" class="ms-content">
            <el-row :gutter="20">
                <el-col :span="6">
                    <el-form-item>
                        <el-input v-model="queryParams.name" placeholder="请输入标书名称" clearable> </el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="6">
                    <el-form-item>
                        <el-date-picker
                            clearable
                            type="daterange"
                            style="width: 100%"
                            v-model="timeValue"
                            range-separator="至"
                            value-format="yyyy-MM-dd"
                            end-placeholder="结束日期"
                            start-placeholder="开始日期"
                        />
                    </el-form-item>
                </el-col>

                <el-col :span="12">
                    <el-form-item>
                        <el-button type="primary" @click="getGwTemplateList">查询</el-button>
                        <el-button @click="resetQueryParams">重置</el-button>
                        <el-button type="primary" @click="onHandleAddOrEdit(null, 1)">新增</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <el-table :data="tableList" stripe style="width: 100%">
            <el-table-column label="序号" width="55" type="index" />
            <el-table-column
                :key="index"
                align="center"
                :prop="item.prop"
                :label="item.label"
                show-overflow-tooltip
                v-for="(item, index) in tableColumn"
            />

            <el-table-column label="操作" width="220" align="center">
                <template #default="{ row }">
                    <el-button
                        style="color: #67c23a"
                        icon="el-icon-view"
                        type="text"
                        size="mini"
                        @click="onHandleAddOrEdit(row, 2)"
                    >
                        详情
                    </el-button>
                    <el-button
                        style="color: #d140ff"
                        icon="el-icon-download"
                        type="text"
                        size="mini"
                        @click="onHandleDownload(row)"
                    >
                        下载
                    </el-button>
                    <el-button
                        style="color: #409eff"
                        icon="el-icon-edit"
                        type="text"
                        size="mini"
                        @click="onHandleAddOrEdit(row, 3)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        style="color: #f56c6c"
                        icon="el-icon-delete"
                        type="text"
                        size="mini"
                        @click="onHandleDelete(row)"
                    >
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getGwTemplateList"
        />
        <DialogModel :dialogConfig="dialogConfig" @closeDialogOuter="closeDialogOuter">
            <template slot="outerContent">
                <AddAndEditForm :formType="formType" :queryId="queryId" @closeDialogOuter="closeDialogOuter" />
            </template>
        </DialogModel>
    </div>
</template>

<script>
import DialogModel from '@/components/DialogModel/index'
import AddAndEditForm from './components/addAndEditForm.vue'
import { getGwTemplateListAPI, deleteGwTemplateAPI, downloadGwTemplateByIdAPI } from '@/api/data-create/gw-template'
export default {
    components: {
        DialogModel,
        AddAndEditForm,
    },
    data() {
        return {
            formType: 1, // 1新增 2详情 3编辑
            total: 0,
            queryId: '', // 详情或编辑的id
            dialogConfig: {
                outerTitle: '新增国网模板',
                innerTitle: false,
                outerWidth: '80%',
                outerVisible: false,
                innerVisible: false,
            },
            queryParams: {
                name: '',
                startDate: '',
                endDate: '',
                pageNum: 1,
                pageSize: 10,
            },
            timeValue: [],
            tableList: [],
            tableColumn: [
                { label: '标书名称', prop: 'name' },
                { label: '创建人', prop: 'createUser' },
                { label: '创建时间', prop: 'createTime' },
            ],
        }
    },
    methods: {
        // 关闭
        closeDialogOuter(isRefresh = false) {
            this.dialogConfig.outerVisible = false
            if (isRefresh) {
                this.getGwTemplateList()
            }
        },
        // 删除
        onHandleDelete(row) {
            this.$modal
                .confirm('确定删除该模板吗？')
                .then(async () => {
                    const res = await deleteGwTemplateAPI({ id: row.id })
                    if (res.code == 200) {
                        this.$message.success('删除成功')
                        this.getGwTemplateList()
                    }
                })
                .catch(() => {})
        },
        // 新增或编辑
        onHandleAddOrEdit(row, type) {
            this.formType = type
            if (type === 1) {
                this.dialogConfig.outerTitle = '新增国网模板'
                this.queryId = ''
            } else if (type === 3) {
                this.dialogConfig.outerTitle = '编辑国网模板'
                this.queryId = row.id
            } else if (type === 2) {
                this.dialogConfig.outerTitle = '国网模板详情'
                this.queryId = row.id
            }

            this.dialogConfig.outerVisible = true
        },
        // 下载
        async onHandleDownload(row) {
            // downloadStateGridTemp
            // const res = await downloadGwTemplateByIdAPI({ id: row.id })
            // console.log(res, '下载结果')

            this.downloadNew(
                '/stateGrid/downloadStateGridTemp',
                {
                    id: row.id,
                },
                `国网模板_${row.name}.doc`,
            )
        },

        // 获取国网模板列表列表
        async getGwTemplateList() {
            if (this.timeValue && this.timeValue.length > 0) {
                this.queryParams.startDate = this.timeValue[0]
                this.queryParams.endDate = this.timeValue[1]
            } else {
                this.queryParams.startDate = ''
                this.queryParams.endDate = ''
            }
            const res = await getGwTemplateListAPI(this.queryParams)
            this.tableList = res.rows
            this.total = res.total
        },
        // 重置
        resetQueryParams() {
            this.timeValue = []
            this.queryParams.name = ''
            this.queryParams.pageNum = 1
            this.queryParams.pageSize = 10
            this.getGwTemplateList()
        },
    },
    created() {
        this.getGwTemplateList()
    },
}
</script>
