<template>
    <div class="pd10">
        <el-form :inline="true" class="demo-form-inline">
            <el-form-item>
                <Area @click="handleNodeClick" ref="area"></Area>
            </el-form-item>

            <!--      <el-form-item  label="工单状态:">
                    <el-select v-model="form.orderStatus" clearable placeholder="请选择工单状态">
                      <el-option
                          v-for="item in workOrderStatusList"
                          :key="item.code"
                          :label="item.name"
                          :value="item.code"
                      ></el-option>
                    </el-select>
                  </el-form-item>-->

            <el-form-item>
                <el-button type="primary" class="ml10" icon="el-icon-search" @click="pageListInit()">查询</el-button>
                <el-button type="primary" icon="el-icon-download" @click="exportEvent">导出</el-button>
            </el-form-item>
            <el-form-item style="float: right;margin-right: 10px">
                <el-button type="primary" icon="el-icon-add" @click="openAdd">新增园区</el-button>
                <el-button type="primary" icon="el-icon-add" @click="deltePark">删除园区</el-button>
                <el-button type="primary" icon="el-icon-upload" @click="uploadGw">上送</el-button>
            </el-form-item>
        </el-form>

        <el-row>
            <el-col :span="24">
                <vxe-table :data="pageData" :height="height" border size="mini" ref="workOrderTable" style="width: 100%"
                    v-loading="isLoading" header-cell-class-name="table_header" @checkbox-all="selectAllChangeEvent"
                    @checkbox-change="handleSelectionChange">
                    <vxe-table-column key="checkboxs" type="checkbox" align="center" width="55"></vxe-table-column>

                    <template v-for="(item, index) in tableHeaderQualitystatistics">
                        <vxe-table-column align="center" :field="item.prop" :title="item.label" :key="index"
                            :min-width="item.width" :formatter="tableDateFilter">
                        </vxe-table-column>
                    </template>

                    <vxe-table-column key="orderStatus" field="orderStatus" title="上送状态" fixed="right" align="center"
                        width="100">
                        <template slot-scope="scope">
                            <el-tag type="primary" size="small" style="color: #3d34fc; background: #ecf5ff"
                                v-if="scope.row.uploadStatus == '0'">
                                未上送
                            </el-tag>
                            <el-tag type="primary" size="small" style="color: #4ba203; background: #f0f9eb"
                                v-if="scope.row.uploadStatus == '1'">
                                已上送
                            </el-tag>

                            <el-tag type="primary" size="small" style="color: #f3a508; background: #f4f4f5"
                                v-if="scope.row.uploadStatus == '2'">
                                待重新上送
                            </el-tag>
                            <el-tag type="primary" size="small" style="color: #fc0303; background: #f4f4f5"
                                v-if="scope.row.uploadStatus == '3'">
                                已删除
                            </el-tag>

                        </template>
                    </vxe-table-column>

                    <vxe-table-column key="operate" field="" show-overflow align="center" title="操作" min-width="200px"
                        fixed="right">
                        <template slot-scope="scope">
                            <el-button type="primary" size="mini" @click="openUpdate(scope.row)">修改</el-button>
                            <el-button type="primary" size="mini" @click="openStation(scope.row)">园区电站管理
                            </el-button>
                        </template>
                    </vxe-table-column>
                </vxe-table>
                <el-pagination background align="right" @size-change="handleSizeChange"
                    @current-change="handleCurrentChange" :current-page="pageNum" :page-sizes="[20, 30, 40, 50, 100]"
                    :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="pageData.length"
                    class="mt10"></el-pagination>
            </el-col>
        </el-row>


        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-form style="height: 300px" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px"
                class="demo-ruleForm pd10">

                <el-form-item label="运维单位:" class="mt10" prop="maintOrg">
                    <Organization @click="handleNodeClick" ref="Organization" :valueTitle="editCheckName"
                        :editCheckCode="editCheckCode"></Organization>
                </el-form-item>


                <el-form-item label="园区名称" class="mt15" prop="parkName">
                    <el-input type="input" v-model="ruleForm.parkName"></el-input>
                </el-form-item>

                <el-form-item label="园区性质" class="mt15" prop="parkNature">
                    <el-select v-model="ruleForm.parkNature" placeholder="请选择园区性质">
                        <el-option v-for="item in parkNatureList" :key="item.gvalue" :label="item.name"
                            :value="item.gvalue"></el-option>
                    </el-select>
                </el-form-item>


                <el-form-item label="园区级别" class="mt15" prop="parkLevel">
                    <el-select v-model="ruleForm.parkLevel" placeholder="请选择园区级别">
                        <el-option v-for="item in pmsParkLevelList" :key="item.gvalue" :label="item.name"
                            :value="item.gvalue"></el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="园区状态" class="mt15" prop="parkStatus">
                    <el-select v-model="ruleForm.parkStatus" placeholder="请选择园区状态">
                        <el-option v-for="item in parkStatusList" :key="item.gvalue" :label="item.name"
                            :value="item.gvalue"></el-option>
                    </el-select>
                </el-form-item>

            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
            </span>
        </el-dialog>


        <el-dialog title="园区电站管理" :visible.sync="dialogVisibleStation" width="100%" height="100%"
            :before-close="handleCloseStation">
            <parkStation ref="parkStationDia" :objId="objId" :cityOrg="cityOrg"></parkStation>
        </el-dialog>


    </div>
</template>

<script>
import Area from '@/views/components/Area/Area.vue'
import Organization from '../../../Process-supervision/Supervisionmanage2/Area/Organization'
import parkStation from './parkStation'
import { pageList, addPark, updatePark, delPark, uploadParkGw } from '@/api/Accountreview/pmsPark/index'
import { dicData } from '@/assets/commjs/dictypeData'


export default {
    components: { Area, Organization, parkStation },
    data() {
        return {
            vh: '',
            height: null,
            isLoading: false,
            form: {
                maintOrg: '',
                type: '',
                orderStatus: ''
            },
            dialogVisible: false,
            dialogVisibleStation: false,
            objId: '',
            cityOrg: '',
            ruleForm: {
                parkName: '',
                parkNature: '',
                parkLevel: '',
                parkStatus: '',
                maintOrg: '',
                maintOrgName: ''
            },

            parkNatureList: [],//园区性质
            pmsParkLevelList: [],//园区级别
            parkStatusList: [{ gvalue: '01', name: '编制' }, { gvalue: '02', name: '发布' }],//园区状态

            rules: {
                maintOrg: [{ required: true, message: '请选择运维单位', trigger: 'blur' }],
                parkName: [{ required: true, message: '请填写园区名称', trigger: 'blur' }],
                parkNature: [{ required: true, message: '请填写园区性质', trigger: 'blur' }],
                parkLevel: [{ required: true, message: '请填写园区等级', trigger: 'blur' }],
                parkStatus: [{ required: true, message: '请选择园区状态', trigger: 'blur' }]
            },

            dialogTitle: '',


            workOrderStatusList: [],



            multipleSelection: [],

            editCheckCode: '',
            editCheckName: '',


            tableHeaderQualitystatistics: [
                { prop: 'provinceName', label: '省公司', width: 180 },
                { prop: 'cityOrgName', label: '市公司', width: 180 },
                { prop: 'maintOrgName', label: '运维单位', width: 180 },

                { prop: 'parkName', label: '园区名称', width: 150 },
                { prop: 'parkNature', label: '园区性质', width: 130 },
                { prop: 'parkLevel', label: '园区级别', width: 130 },


                { prop: 'parkStatus', label: '园区状态', width: 120 },
                { prop: 'createTime', label: '创建时间', width: 180 }
                /*{prop: 'createBy', label: '创建人', width: 130},*/


            ],
            pageData: [],
            pageNum: 1, // 当前页码
            total: 0, // 总条数
            pageSize: 20 // 每页的数据条数
        }
    },
    created() {

        this.parkNatureList = dicData('pms_park_type', [])
        this.pmsParkLevelList = dicData('pms_park_level', [])


        this.form.maintOrg = JSON.parse(window.sessionStorage.getItem('Info')).deptId


    },
    mounted() {
        this.pageListInit()
        this.setHeight()
        window.addEventListener('resize', this.setHeight)
    },
    beforeDestroy() {
        window.removeEventListener('resize', this.setHeight)
    },
    methods: {
        setHeight() {
            this.height = window.sessionStorage.getItem('appheight') - 110
        },


        //获取主网运行指标统计数据
        pageListInit() {
            this.isLoading = true
            this.form.pageNum = this.pageNum
            this.form.pageSize = this.pageSize
            pageList(this.form).then(res => {
                this.pageData = res.data.records
                this.isLoading = false
            })
        },
        // 切换选项
        handleNodeClick(data) {
            // console.log(data);
            this.ruleForm.maintOrg = data.code
            this.ruleForm.maintOrgName = data.name
        },
        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.pageSize = val
            this.pageListInit()
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.pageNum = val
            this.pageListInit()
        },
        //重置
        Reset() {
            this.$refs.area.form.valueTitle = '全国'
            this.$refs.interval.intervald = 1
        },
        //导出
        exportEvent() {
            this.$refs.workOrderTable.exportData({
                filename: 'export', // 文件名字
                sheetName: 'Sheet1',
                type: 'xlsx', //导出文件类型 xlsx 和 csv
                useStyle: true,
                data: this.pageData
            })
        },

        //工单
        openAdd(row) {
            this.dialogTitle = '新增园区台账'
            this.dialogVisible = true
        },

        openUpdate(row) {
            this.dialogTitle = '修改园区台账'
            this.dialogVisible = true
            this.ruleForm = row
            this.editCheckName = row.maintOrgName
            this.editCheckCode = row.maintOrg
        },


        deltePark() {
            if (this.multipleSelection.length == 0) {
                this.$message({
                    showClose: true,
                    message: '请至少选择一条台账删除!!!',
                    type: 'warning'
                })
                return
            }

            this.$confirm('是否确认删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let planIds = []
                this.multipleSelection.forEach(item => {
                    planIds.push(item.objId)
                })

                delPark(planIds).then(res => {
                    if ((res.code == 'A0000')) {
                        this.$message({
                            type: 'success',
                            message: res.message
                        })
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消'
                })
            })

        },


        //电站相关
        openStation(row) {
            this.dialogVisibleStation = true
            this.objId = row.objId
            this.cityOrg = row.cityOrg
        },

        openDown(row) {

        },

        handleClose() {
            this.resetForm('ruleForm')
            this.dialogVisible = false
        },

        handleCloseStation() {
            this.dialogVisibleStation = false
        },

        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {

                    if (this.dialogTitle == '新增园区台账') {
                        addPark(this.ruleForm).then(res => {
                            if (res.code == 'A0000') {
                                this.$message({
                                    message: '新增成功!',
                                    type: 'success'
                                })
                                this.handleClose()
                                this.pageListInit()
                            }

                        })
                    } else {
                        updatePark(this.ruleForm).then(res => {
                            if (res.code == 'A0000') {
                                this.$message({
                                    message: '修改成功!',
                                    type: 'success'
                                })
                                this.handleClose()
                                this.pageListInit()
                            }

                        })
                    }

                } else {
                    console.log('error submit!!')
                    return false
                }
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields()
        },


        //上报网公司按钮触发
        uploadGw() {
            if (this.multipleSelection.length == 0) {
                this.$confirm('未选择默认上送所有未上送的台账?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let planIds = []
                    this.multipleSelection.forEach(item => {
                        planIds.push(item.objId)
                    })

                    uploadParkGw(planIds).then(res => {
                        if ((res.code == 'A0000')) {
                            this.$message({
                                type: 'success',
                                message: res.message
                            })
                        }

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消上送'
                    })
                })
            } else {
                let flag = this.multipleSelection.some(item => item.uploadStatus == '02')
                if (flag) {
                    this.$message({
                        showClose: true,
                        message: '请选择未上送的台账上送!!!',
                        type: 'warning'
                    })
                    return
                }

                this.$confirm('是否确认上送园区台账?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let planIds = []
                    this.multipleSelection.forEach(item => {
                        planIds.push(item.objId)
                    })

                    uploadParkGw(planIds).then(res => {
                        if ((res.code == 'A0000')) {
                            this.$message({
                                type: 'success',
                                message: res.message
                            })
                        }

                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '上送异常'
                    })
                })

            }


        },


        handleSelectionChange(val) {
            this.multipleSelection = val.records
        },
        selectAllChangeEvent(val) {
            this.multipleSelection = val.records
        },

        tableDateFilter(row, column) {
            if (row.column.property == 'parkNature') {
                let title = ''
                const foundItem = dicData('pms_park_type', []).find(item => item.gvalue === row.row.parkNature);
                if (foundItem) {
                    title = foundItem.name;
                }
                return title
            } else if (row.column.property == 'parkLevel') {
                let title = ''
                const foundItem = dicData('pms_park_level', []).find(item => item.gvalue === row.row.parkLevel);
                if (foundItem) {
                    title = foundItem.name;
                }
                return title
            } else if (row.column.property == 'parkStatus') {
                if (row.row.parkStatus == '01') {
                    return "编制";
                } else if (row.row.parkStatus == '02') {
                    return "发布";
                } else {
                    return "/"
                }
            } else {
                return row.row[row.column.property]
            }
        }


    }
}
</script>
<style lang="less" scoped></style>
