<template>
    <div>
        <el-form :inline="true" :model="ruleForm" class="demo-form-inline">
            <el-form-item label="信息查询:">
                <el-input v-model.trim="ruleForm.searchValue" placeholder="请输入查询信息" clearable></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-download" class="pms">PMS获取数据</el-button>
                <!-- <el-button type="primary" icon="el-icon-upload2" class="ml10" @click="upload">PMS数据上传</el-button> -->
                <el-button type="primary" class="ml10" icon="el-icon-search" @click="pagMonitor">查询</el-button>
                <!-- <el-button type="primary" icon="el-icon-refresh-right" @click="Reset"
          >重置</el-button
        > -->
                <el-button type="primary" icon="el-icon-download" @click="exportEvent">导出</el-button>
            </el-form-item>
        </el-form>

        <template>
            <span>
                <el-button type="primary" size="mini" class="ml10" icon="el-icon-plus" @click="addFn">新增</el-button>
                <el-button type="primary" size="mini" class="ml10" icon="el-icon-edit" @click="modifyFn">
                    修改
                </el-button>
                <el-button type="primary" size="mini" class="ml10" icon="el-icon-delete" @click="deleteFn">
                    删除
                </el-button>
            </span>
            <vxe-table stripe :data="UserData" :height="height" border :row-config="{ isCurrent: true, isHover: true }"
                size="mini" ref="TractionStationInformation" style="width: 100%" v-loading="isLoading"
                header-cell-class-name="table_header" @checkbox-all="handleSelectionChange"
                @checkbox-change="handleSelectionChange" class="mt10" :cell-style="cellStyle">
                <vxe-table-column align="center" type="checkbox" width="55"></vxe-table-column>

                <vxe-table-column v-for="(item, index) in tableHeaderMonitor" align="center" :field="item.prop"
                    :title="item.label" :min-width="item.width" :key="index" :formatter="formFilter"
                    :show-overflow="true"></vxe-table-column>
            </vxe-table>
            <el-pagination background align="right" @size-change="handleSizeChange"
                @current-change="handleCurrentChange" :current-page="ruleForm.pageNum"
                :page-sizes="[20, 30, 40, 50, 100]" :page-size="ruleForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper" :total="total" class="mt10"></el-pagination>
        </template>

        <!-- <PMS_Data
      :PMSobtain="PMSobtain"
      :title="title"
      @Close="Close"
      :Data="UserData"
      :Name="tableHeaderMonitor"
    ></PMS_Data> -->

        <!-- 新增/修改 -->
        <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="addinformation"
            :before-close="handleClose" width="50%" height="80%">
            <el-form :inline="true" :model="form" label-width="170px" class="form" :rules="rules" ref="rulesform">
                <el-form-item label="牵引站编号:" prop="id" class="mt10">
                    <el-input v-model="form.id" placeholder="请输入牵引站编号" clearable :disabled="disabled"></el-input>
                </el-form-item>
                <el-form-item label="牵引站名称:" prop="name" class="mt10">
                    <el-input v-model="form.name" clearable placeholder="请输入牵引站名称"></el-input>
                </el-form-item>
                <el-form-item prop="orgName" class="mt10" label="组织机构名称:" :rules="{
                    required: true,
                    message: '请输入组织机构名称',
                    trigger: 'change'
                }">
                    <Organization @click="handleNodeClick" ref="Organization" :valueTitle="valueTitle"
                        :editCheckCode="editCheckCode"></Organization>
                </el-form-item>
                <el-form-item prop="operationName" class="mt10" label="运维单位名称:" :rules="{
                    required: true,
                    message: '请输入运维单位名称',
                    trigger: 'change'
                }">
                    <Operation @click="handleNodeClick1" ref="Operation" :valueTitle="valueTitle1"
                        :editCheckCode="editCheckCode1"></Operation>
                </el-form-item>
                <el-form-item label="供电变电站名称:" prop="powerId" class="mt10">
                    <el-select v-model="form.powerId" clearable placeholder="请选择供电变电站名称" @change="changeFn3" filterable>
                        <el-option v-for="item in statationName" :key="item.powerId"
                            :label="item.orgName + '_' + item.powerName" :value="item.powerId"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="供电变电站电压等级:" class="mt10">
                    <el-select v-model="form.powerVoltageLevel" clearable placeholder="请选择供电变电站电压等级">
                        <el-option v-for="item in powerVoltageLevelList" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="供电电压等级:" class="mt10">
                    <el-select v-model="form.voltageLevel" clearable placeholder="请选择供电电压等级">
                        <el-option v-for="item in voltageLevelList" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否专线供电:" prop="ifSpecial" class="mt10">
                    <el-select v-model="form.ifSpecial" clearable placeholder="请选择是否专线供电">
                        <el-option v-for="item in whether1" :key="item.id" :label="item.label"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="铁路:" prop="railwayLineId" class="mt10">
                    <el-select v-model="form.railwayLineId" clearable filterable placeholder="请选择是否专线供电">
                        <el-option v-for="item in railwayList" :key="item.code" :label="item.name"
                            :value="item.code"></el-option>
                    </el-select>
                    <!-- <el-input v-model="form.railwayLineName" clearable placeholder="请输入铁路线路名称"></el-input> -->
                </el-form-item>
                <!-- <el-form-item label="铁路线路编号:" class="mt10">
                    <el-input
                        v-model="form.railwayLineId"
                        maxlength="18"
                        clearable
                        placeholder="请输入铁路线路编号"
                    ></el-input>
                </el-form-item> -->
                <!-- <el-form-item label="铁路类型:" class="mt10">
                    <el-select v-model="form.railwayType" clearable placeholder="请选择是否是上送网公司监测点">
                        <el-option label="高铁" value="1"></el-option>
                        <el-option label="普铁" value="2"></el-option>
                    </el-select>
                </el-form-item> -->
                <el-form-item label="牵引站变压器接线方式:" prop="connetGroupWay" class="mt10">
                    <el-select v-model="form.connetGroupWay" clearable placeholder="请选择牵引站变压器接线方式">
                        <el-option v-for="item in connetGroupWayList" :key="item.id" :label="item.name"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="牵引站变电容量:" prop="ratedCapacity" class="mt10">
                    <el-input v-model="form.ratedCapacity" maxlength="10" clearable placeholder="请输入牵引站变电容量"></el-input>
                </el-form-item>

                <el-form-item label="创建日期:" prop="createdDate" class="mt10">
                    <el-date-picker v-model="form.createdDate" value-format="yyyy-MM-dd" type="date"
                        placeholder="选择日期"></el-date-picker>
                </el-form-item>
                <el-form-item label="用户标签:" prop="userTag" class="mt10">
                    <el-input v-model="form.userTag" clearable placeholder="请输入用户标签"></el-input>
                </el-form-item>
                <el-form-item label="是否是上送网公司监测点:" prop="isUpToGrid" class="mt10">
                    <el-select v-model="form.isUpToGrid" clearable placeholder="请选择是否是上送网公司监测点">
                        <el-option v-for="item in whether" :key="item.id" :label="item.label"
                            :value="item.id"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleClose">取 消</el-button>
                <el-button v-if="add" type="primary" @click="addinformationFn">确 定</el-button>
                <el-button v-if="modify" type="primary" @click="modifyinformationFn">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import PMS_Data from '@/views/components/Accountinformation/PMS_Data'
import Organization from './Area/Organization.vue'
import Operation from './Area/Operation.vue'
import {
    getstatationStatList,
    getTractionStationPageList,
    addTractionStation,
    updateTractionStation,
    deleteTractionStation
} from '@/api/hbaccountoperation/TractionStationInformation'
import { deptTree } from '@/api/admin/dept'
import { dicData } from '@/assets/commjs/dictypeData'
export default {
    components: { PMS_Data, Organization, Operation },
    data() {
        return {
            vh: '',
            title: '',
            add: false,
            modify: false,
            disabled: false,

            // 查询数据
            ruleForm: {
                searchValue: '',
                pageNum: 1,
                pageSize: 20
            },
            total: 0,
            form: {
                connetGroupWay: '',
                createdDate: '',
                id: '',
                ifSpecial: 0,
                isUpToGrid: 0,
                railwayType: '',
                name: '',
                operationId: '',
                operationName: '',
                orgId: '',
                orgName: '',
                powerId: '',
                powerName: '',
                powerVoltageLevel: '',
                railwayLineId: '',
                railwayLineName: '',
                ratedCapacity: 0,
                userTag: '',
                voltageLevel: ''
            },
            rules: {
                id: [{ required: true, message: '请输入牵引站编号', trigger: 'blur' }],
                name: [{ required: true, message: '请输入牵引站名称', trigger: 'blur' }],
                powerId: [
                    {
                        required: true,
                        message: '请选择供电变电站名称',
                        trigger: 'change'
                    }
                ],
                railwayLineId: [
                    { required: true, message: '请输入铁路线路编号', trigger: 'change' },
                    {
                        pattern: /^[0-9a-z]*$/,
                        message: '只可以输入数字和小写字母',
                        trigger: 'blur'
                    }
                ],
                railwayLineName: [{ required: true, message: '请输入铁路线路名称', trigger: 'blur' }],
                ratedCapacity: [
                    { required: true, message: '请输入牵引站变电容量', trigger: 'blur' },
                    { pattern: /^(0\.[1-9]\d*|[1-9]\d*(\.\d+)?)$/, message: '只可以输入数字', trigger: 'blur' }
                ],
                voltageLevel: [{ required: true, message: '请选择供电电压等级', trigger: 'change' }],
                connetGroupWay: [
                    {
                        required: true,
                        message: '请选择牵引站变压器接线方式',
                        trigger: 'change'
                    }
                ],
                powerVoltageLevel: [
                    {
                        required: true,
                        message: '请选择供电变电站电压等级',
                        trigger: 'change'
                    }
                ],
                ifSpecial: [{ required: true, message: '请选择是否专线供电', trigger: 'change' }],
                createdDate: [{ required: true, message: '选择日期', trigger: 'change' }],
                // userTag: [{ required: true, message: '请输入用户标签', trigger: 'blur' }],
                isUpToGrid: [
                    {
                        required: true,
                        message: '请选择是否是上送网公司监测点',
                        trigger: 'change'
                    }
                ]
            },
            railwayList: [],
            height: null,
            height1: null,
            isLoading: false,
            disabled: true,
            multipleSelection: [],
            //台区台账表头
            tableHeaderMonitor: [
                { prop: 'id', label: '牵引站编号', width: 100 },
                { prop: 'name', label: '牵引站名称', width: 140 },
                { prop: 'orgName', label: '组织机构名称', width: 150 },
                { prop: 'operationName', label: '运维单位名称', width: 150 },
                { prop: 'voltageLevel', label: '供电电压等级', width: 120 },
                { prop: 'railwayLineId', label: '铁路线路编号', width: 120 },
                { prop: 'railwayLineName', label: '铁路线路名称', width: 120 },
                { prop: 'railwayType', label: '铁路类型', width: 120 },
                { prop: 'connetGroupWay', label: '牵引站变压器接线方式', width: 180 },
                { prop: 'ratedCapacity', label: '牵引站变电容量', width: 160 },
                { prop: 'powerId', label: '供电变电站编号', width: 160 },
                { prop: 'powerName', label: '供电变电站名称', width: 180 },
                { prop: 'powerVoltageLevel', label: '供电变电站电压等级', width: 160 },
                { prop: 'ifSpecial', label: '是否专线供电', width: 120 },
                { prop: 'createdDate', label: '创建日期', width: 120 },
                { prop: 'userTag', label: '用户标签', width: 120 },

                { prop: 'isUpToGrid', label: '是否是上送网公司监测点', width: 190 }
                // { prop: "status", label: "数据状态", width:100 },
            ],
            whether: [
                {
                    id: 0,
                    value: '选项1',
                    label: '否'
                },
                {
                    id: 1,
                    value: '选项2',
                    label: '是'
                }
            ],
            whether1: [
                {
                    id: 0,
                    value: '选项1',
                    label: '否'
                },
                {
                    id: 1,
                    value: '选项2',
                    label: '是'
                }
            ],
            editCheckCode: '',
            valueTitle: '',
            editCheckCode1: '',
            valueTitle1: '',
            UserData: [],
            device: '',
            organization: [], //组织机构名称
            operation: [], //运维单位名称
            statationName: [], //供电变电站名称
            voltageLevelList: [], //供电电压等级
            connetGroupWayList: [], //牵引站变压器接线方式
            powerVoltageLevelList: [], //供电变电站电压等级
            PMSobtain: false,
            information: false, //修改台区信息
            addinformation: false //新增台区信息
        }
    },
    created() {
        this.getclassificationData()
    },

    mounted() {
        this.setHeight()
        window.addEventListener('resize', this.setHeight)
        this.Monitor()
    },
    beforeDestroy() {
        window.removeEventListener('resize', this.setHeight)
    },
    computed: {},
    methods: {
        setHeight() {
            this.device = window.devicePixelRatio
            this.height = window.sessionStorage.getItem('appheight') - 205
            this.height1 = window.sessionStorage.getItem('appheight') - 285
        },
        // 切换选项
        handleNodeClick(data) {
            // console.log(data);
            if (data.code != null) {
                let form = {
                    deptId: data.id
                }
                // 变电站名称
                getstatationStatList(form).then(res => {
                    this.statationName = res.data
                })
            }
            this.form.orgId = data.code
            this.form.orgName = data.name
        },
        handleNodeClick1(data) {
            // console.log(data);
            this.form.operationId = data.code
            this.form.operationName = data.name
        },
        changeFn3(val) {
            // console.log(val);
            this.statationName.forEach(item => {
                if (item.powerId == val) {
                    this.form.powerName = item.powerName
                }
            })
        },
        //获取类型
        getclassificationData() {
            //供电电压等级/供电变电站电压等级
            var code = 'Dev_Voltage'
            this.voltageLevelList = dicData(code, [])
            this.powerVoltageLevelList = dicData(code, [])
            this.railwayList = dicData('Railway_Number', [])
            console.log('🚀 ~ getclassificationData ~ this.railwayList:', this.railwayList)
            //牵引站变压器接线方式
            var code1 = 'Connet_Group_Way'
            this.connetGroupWayList = dicData(code1, [])
        },

        //PMS获取数据
        // PMSobtainFn() {
        //   this.PMSobtain = true;
        //   this.title = "线路表";
        // },
        pagMonitor() {
            this.ruleForm.pageNum = 1
            this.ruleForm.pageSize = 20
            this.Monitor()
        },
        //查询用户用电信息
        Monitor() {
            this.isLoading = true
            getTractionStationPageList(this.ruleForm).then(res => {
                this.isLoading = false
                this.UserData = res.data.records
                this.total = res.data.total
            })
        },
        // 数据过滤
        formFilter(row, column) {
            if (row.column.property == 'voltageLevel') {
                let title = ''
                this.voltageLevelList.forEach(item => {
                    if (item.value == row.row.voltageLevel) {
                        title = item.label
                    }
                })
                return title
            } else if (row.column.property == 'connetGroupWay') {
                let title = ''
                this.connetGroupWayList.forEach(item => {
                    if (item.value == row.row.connetGroupWay) {
                        title = item.label
                    }
                })
                return title
            } else if (row.column.property == 'powerVoltageLevel') {
                let title = ''
                this.powerVoltageLevelList.forEach(item => {
                    if (item.value == row.row.powerVoltageLevel) {
                        title = item.label
                    }
                })
                return title
            } else if (row.column.property == 'railwayType') {
                return row.row.railwayType == 1 ? '高铁' : row.row.railwayType == 2 ? '普铁' : ''
            } else {
                return row.row[row.column.property]
            }
        },

        //数据上传
        upload() {
            if (this.multipleSelection.length == 0) {
                this.$message({
                    showClose: true,
                    message: '请选择台账!!!',
                    type: 'warning'
                })
                return
            }
            this.$message({
                showClose: true,
                message: '上传成功！！！',
                type: 'success'
            })
        },
        // 重置
        Reset() {
            // 查询数据
            this.ruleForm.searchValue = ''
        },

        //获取数据
        getData() {
            // deptTree().then(res=>{
            //   if(res.code=='A0000'){
            //     this.operation=res.data[0].children[0].children
            //     this.organization=res.data[0].children[0].children
            //   }
            // })
            // getstatationStatList().then(res=>{
            //   // console.log(res);
            //   this.statationName=res.data
            // });
        },
        //新增
        addFn() {
            this.disabled = false
            this.modify = false
            this.add = true
            this.title = '新增牵引站信息'
            this.addinformation = true
            this.getData()
        },
        //新增台区确定
        addinformationFn() {
            // this.railwayList.filter(item => item.code == this.form.railwayLineId)
            this.$refs.rulesform.validate(value => {
                // console.log(value);
                if (value == true) {
                    this.$confirm('是否确认新增?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    })
                        .then(() => {
                            this.form.railwayLineName = this.railwayList.filter(
                                item => item.code == this.form.railwayLineId
                            )[0].name
                            this.form.railwayType = this.railwayList.filter(
                                item => item.code == this.form.railwayLineId
                            )[0].gvalue
                            addTractionStation(this.form).then(res => {
                                if (res.code == 'A0000') {
                                    this.$message({
                                        message: res.message,
                                        type: 'success'
                                    })
                                }
                                this.addinformation = false
                                this.Monitor()
                            })
                        })
                        .catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消新增'
                            })
                        })
                }
            })
        },
        handleClose() {
            this.form = {
                connetGroupWay: '',
                createdDate: '',
                id: '',
                ifSpecial: 0,
                isUpToGrid: 0,
                railwayType: '',
                name: '',
                operationId: '',
                operationName: '',
                orgId: '',
                orgName: '',
                powerId: '',
                powerName: '',
                powerVoltageLevel: '',
                railwayLineId: '',
                railwayLineName: '',
                ratedCapacity: 0,
                userTag: '',
                voltageLevel: ''
            }
            this.valueTitle = ''
            this.valueTitle1 = ''
            this.$refs['rulesform'].resetFields()
            this.addinformation = false
            if (this.title == '新增牵引站信息') {
                this.$refs.Operation.form.valueTitle = ''
                this.$refs.Organization.form.valueTitle = ''
            }
        },
        //修改数据
        modifyFn() {
            this.getData()
            this.disabled = true
            this.modify = true
            this.add = false
            this.title = '修改牵引站信息'
            if (this.multipleSelection.length != 1) {
                this.$message({
                    showClose: true,
                    message: '请选择台账!!!',
                    type: 'warning'
                })
                return
            }
            this.addinformation = true
            // console.log(this.multipleSelection[0],'===============');
            this.form = JSON.parse(JSON.stringify(this.multipleSelection[0]))
            this.form.powerId = this.form.orgName + '_' + this.form.powerName
            this.valueTitle = this.multipleSelection[0].orgName
            this.editCheckCode = this.multipleSelection[0].orgId
            this.valueTitle1 = this.multipleSelection[0].operationName
            this.editCheckCode1 = this.multipleSelection[0].operationId
            //处理下拉框回显数据
            let form = {
                deptCode: this.multipleSelection[0].orgId
            }
            // 变电站名称
            getstatationStatList(form).then(res => {
                this.statationName = res.data
            })
            if (this.form.ifSpecial == '是') {
                this.form.ifSpecial = 1
            } else if (this.form.ifSpecial == '否') {
                this.form.ifSpecial = 0
            }
            if (this.form.isUpToGrid == '是') {
                this.form.isUpToGrid = 1
            } else if (this.form.isUpToGrid == '否') {
                this.form.isUpToGrid = 0
            }
        },
        //修改台区信息确定
        modifyinformationFn() {
            this.$refs.rulesform.validate(value => {
                // console.log(value);
                if (value == true) {
                    this.$confirm('是否确认修改?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    })
                        .then(() => {
                            this.form.railwayLineName = this.railwayList.filter(
                                item => item.code == this.form.railwayLineId
                            )[0].name
                            this.form.railwayType = this.railwayList.filter(
                                item => item.code == this.form.railwayLineId
                            )[0].gvalue
                            updateTractionStation(this.form).then(res => {
                                if (res.code == 'A0000') {
                                    this.$message({
                                        message: res.message,
                                        type: 'success'
                                    })
                                }
                                this.addinformation = false
                                this.Monitor()
                            })
                        })
                        .catch(() => {
                            this.$message({
                                type: 'info',
                                message: '已取消修改'
                            })
                        })
                }
            })
        },
        // 删除
        deleteFn() {
            if (this.multipleSelection.length == 0) {
                this.$message({
                    showClose: true,
                    message: '请选择台账!!!',
                    type: 'warning'
                })
                return
            }
            this.$confirm('是否确认删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
                .then(() => {
                    let id = []
                    this.multipleSelection.forEach(item => {
                        id.push(item.id)
                    })

                    deleteTractionStation(id).then(res => {
                        if ((res.code == 'A0000')) {
                            this.$message({
                                type: 'success',
                                message: res.message
                            })
                        }
                        this.Monitor()
                    })
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    })
                })
        },

        //关闭PMS数据弹框
        // Close() {
        //   this.PMSobtain = false;
        // },

        // 表格多选
        handleSelectionChange(row) {
            this.multipleSelection = row.records
            // console.log(this.multipleSelection);
        },

        //每页条数改变时触发 选择一页显示多少行
        handleSizeChange(val) {
            this.ruleForm.pageSize = val
            this.Monitor()
        },
        //当前页改变时触发 跳转其他页
        handleCurrentChange(val) {
            this.ruleForm.pageNum = val
            this.Monitor()
        },
        cellStyle(row) {
            if (row.row.ifSpecial == 0 || row.row.isUpToGrid == 0) {
                row.row.ifSpecial = '否'
                row.row.isUpToGrid = '否'
            } else if (row.row.ifSpecial == 1 || row.row.isUpToGrid == 1) {
                row.row.ifSpecial = '是'
                row.row.isUpToGrid = '是'
            }
        },
        //导出
        exportEvent() {
            getTractionStationPageList({
                searchValue: this.ruleForm.searchValue,
                pageNum: 1,
                pageSize: this.total
            }).then(res => {
                this.$refs.TractionStationInformation.exportData({
                    filename: '牵引站信息', // 文件名字
                    sheetName: 'Sheet1',
                    type: 'xlsx', //导出文件类型 xlsx 和 csv
                    useStyle: true,
                    data: res.data.records, // 数据源 // 过滤那个字段导出
                    columnFilterMethod: function (column, $columnIndex) {
                        return !(column.$columnIndex === 0)
                    }
                })
            })
        }
    }
}
</script>
<style lang="less" scoped>
@import url('../../../styles/comStyle.less');

::v-deep .vxe-table .cell {
    text-align: center;
}

::v-deep .el-tabs--border-card>.el-tabs__content {
    padding: 10px;
}

::v-deep .form {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    .el-form-item {
        display: flex;
        width: 48%;

        .el-form-item__content {
            flex: 1 !important;

            .el-select {
                width: 100%;
            }

            .el-date-editor {
                width: 100%;
            }
        }
    }
}

.pms {
    margin-left: 10px;
}

.obtain {
    margin-left: 1220px;
}
</style>
