<template>
    <div>
        <el-dialog :close-on-click-modal="false"
                   :title="title"
                   :visible.sync="dialogVisible"
                   :before-close="handleClose"

                   width="30%"
        >
            <el-form
                ref="formName"
                :rules="rules"
                label-width="150px"
                :model="form"
                :disabled="disabled"
            >
                <el-form-item label="监测点类别:" prop="monitorSort">
                    <el-select
                        disabled
                        v-model.trim="form.monitorSort"
                        placeholder="请选择监测点类别"
                    >
                        <el-option
                            v-for="item in devIdList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                            :disabled="item.disabled"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item
                    label="监测点状态:"
                    prop="monitorState"
                    v-if="activeName != 1"
                >
                    <el-select
                        clearable
                        v-model.trim="form.monitorState"
                        placeholder="请选择监测点状态"
                    >
                        <el-option
                            v-for="item in voltageIdList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item
                    label="监测点类型:"
                    v-if="activeName == 3"
                    prop="ifPowerUser"
                >
                    <el-select
                        clearable
                        :disabled="title == '修改配网'"
                        v-model.trim="form.ifPowerUser"
                        @change="changeType"
                        placeholder="请选择监测点类型"
                    >
                        <el-option
                            v-for="item in scaleList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="监测点:" prop="monitorId">
                    <el-select
                        filterable
                        clearable
                        :disabled="title == '修改配网'"
                        v-model.trim="form.monitorId"
                        @change="changeMonitorId"
                        placeholder="输入关键字进行过滤"
                    >
                        <el-option
                            v-for="item in IDList"
                            :key="item.id"
                            :label="
                activeName == 3
                  ? item.platformName + ' __ ' + item.name
                  : activeName == 2
                  ? item.orgName + ' __ ' + item.name
                  : item.powerrName + ' __ ' + item.name
              "
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item
                    label="所属终端:"
                    prop="terminalId"
                    v-if="activeName != 1"
                >
                    <el-select
                        filterable
                        clearable
                        v-model="form.terminalId"
                        placeholder="输入关键字进行过滤"
                    >
                        <el-option
                            v-for="item in Terminal"
                            :key="item.id"
                            :label="item.powerrName + ' __ ' + item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item
                    label="监测终端接线方式:"
                    prop="terminalWiringMethod"
                    v-if="activeName != 1"
                >
                    <el-select
                        filterable
                        clearable
                        v-model.trim="form.terminalWiringMethod"
                        placeholder="请选择监测终端接线方式"
                    >
                        <el-option
                            v-for="item in modeList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item
                    label="统计间隔:"
                    prop="statisticalInterval"
                    v-if="activeName != 1"
                >
                    <el-select
                        filterable
                        clearable
                        v-model.trim="form.statisticalInterval"
                        placeholder="请选择统计间隔"
                    >
                        <el-option
                            v-for="item in [{id:1,name:'1分钟'},{id:3,name:'3分钟'},{id:5,name:'5分钟'},{id:10,name:'10分钟'}]"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        >
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="pt变化值:" prop="pt1" v-if="activeName != 1">
                    <div style="display: flex; justify-content: space-between">
                        <el-input
                            v-model="form.pt1"
                            style="margin-right: 10px"
                            placeholder="请输入pt1"
                        ></el-input>
                        <el-input
                            v-model="form.pt2"
                            style="margin-left: 10px"
                            placeholder="请输入pt2"
                        ></el-input>
                    </div>
                </el-form-item>
                <!-- <el-form-item label="pt2:" prop="pt2" v-if="activeName != 1">

                </el-form-item> -->
                <el-form-item label="ct变化值:" prop="ct1" v-if="activeName != 1">
                    <div style="display: flex; justify-content: space-between">
                        <el-input
                            v-model="form.ct1"
                            style="margin-right: 10px"
                            placeholder="请输入ct1"
                        ></el-input>
                        <el-input
                            v-model="form.ct2"
                            style="margin-left: 10px"
                            placeholder="请输入ct2"
                        ></el-input>
                    </div>
                </el-form-item>
                <!-- <el-form-item label="ct2:" prop="ct2" v-if="activeName != 1">

                </el-form-item> -->
            </el-form>
            <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="handleClose"
        >取 消</el-button
        >
        <el-button type="primary" size="small" @click="confirm"
        >确 定</el-button
        >
      </span>
        </el-dialog>
    </div>
</template>
<script>
import {
    getMonitorListByParam,
    addDistributionMonitor,
    getPowerDistributionList,
    getPowerGenerationUserList,
    getPowerClientList,
    getTerminalSelectList,
    getDistributionMonitorById,
    updateDistributionMonitor
} from '@/api/AccountOperation'
import { dicData } from '@/assets/commjs/dictypeData'

export default {
    components: {},
    props: {
        dialogVisible: {
            type: Boolean, //类型
            required: true //是否必须传入
        },
        title: { type: String, required: true },
        activeName: { type: String, required: true },
        orgId: { type: String, required: true },
        multipleSelection: { type: Array, required: false }
    },
    data() {
        return {
            form: {
                monitorId: '',
                monitorSort: '',
                monitorState: '',
                ifPowerUser: 0,
                terminalId: '',
                terminalWiringMethod: '',
                statisticalInterval: 1,
                pt1: '',
                pt2: '',
                ct1: '',
                ct2: ''
            },
            scaleList: [
                {
                    id: 0,
                    name: '用电用户'
                },
                {
                    id: 1,
                    name: '发电用户'
                }
            ],
            devIdList: [], //1所属终端
            voltageIdList: [], //1所属母线
            IDList: [], //1所属母线
            ptTypeList: [], //监测终端接线方式
            modeList: [], //接线方式
            Terminal: [], //所属终端
            tfTypeList: [], //互感器类型
            groundTypeList: [], //接地方式
            // 表格检验
            rules: {
                monitorId: [
                    { required: true, message: '请选择监测点', trigger: 'change' }
                ],
                monitorSort: [
                    { required: true, message: '请选择监测点类别', trigger: 'change' }
                ],
                monitorState: [
                    { required: true, message: '请选择监测点状态', trigger: 'change' }
                ],
                ifPowerUser: [
                    { required: true, message: '请选择监测点类型', trigger: 'change' }
                ],
                terminalId: [
                    { required: true, message: '请选择所属终端', trigger: 'change' }
                ],
                terminalWiringMethod: [
                    {
                        required: true,
                        message: '请选择监测终端接线方式',
                        trigger: 'change'
                    }
                ],
                statisticalInterval: [
                    {
                        required: true,
                        message: '请选择监测点统计间隔',
                        trigger: 'change'
                    }
                ],
                pt1: [{ required: true, message: '请输入pt1', trigger: 'blur' }],
                pt2: [{ required: true, message: '请输入pt2', trigger: 'blur' }],
                ct1: [{ required: true, message: '请输入ct1', trigger: 'blur' }],
                ct2: [{ required: true, message: '请输入ct2', trigger: 'blur' }]
            },
            disabled: false
        }
    },
    created() {
        getTerminalSelectList({
            /*deptCode: val,*/
        }).then((res) => {
            this.Terminal = res.data
        })
        if (this.activeName == 1) {
            this.form.monitorSort = '78a96acb276a5fe9d6eff737fdf1973f'
            getMonitorListByParam({ orgId: this.orgId }).then((res) => {
                this.IDList = res.data
            })
        } else if (this.activeName == 2) {
            this.form.monitorSort = 'a5696acb276a5fe9d6eff74fdf1973f'
            getPowerDistributionList({ orgId: this.orgId }).then((res) => {
                this.IDList = res.data
            })
        } else if (this.activeName == 3) {
            this.form.monitorSort = '35696acb276a5fe9d6eff74fdf1973f'
            getPowerClientList({ orgId: this.orgId }).then((res) => {
                this.IDList = res.data
            })
        }
        this.dictypeData()
        if (this.title == '修改配网') {
            getDistributionMonitorById({
                id: this.multipleSelection[0],
                monitorSort: this.form.monitorSort
            }).then((res) => {
                this.form = res.data
                // this.IDList = res.data;
            })
        }
    },
    mounted() {
    },

    methods: {
        changeMonitorId(e) {
            let val = ''
            this.IDList.forEach((item) => {
                if (item.id == this.form.monitorId) {
                    val = item.orgId
                }
            })
            this.form.terminalId = ''
        },
        // 字典表
        dictypeData() {
            //所属母线

            this.voltageIdList = dicData('Line_State', [])

            //所属终端
            this.devIdList = dicData('Line_Sort', [])
            // 接线方式
            this.modeList = dicData('Dev_Connect', [])
            // 所属终端
            // this.Terminal = dicData("Line_Sort", []);
        },
        // 确认按钮
        confirm() {
            this.$refs['formName'].validate((valid) => {
                if (valid) {
                    if (this.title == '修改配网') {
                        updateDistributionMonitor(this.form).then((res) => {
                            if (res.code == 'A0000') {
                                this.$message({
                                    message: '修改成功!',
                                    type: 'success'
                                })
                                this.$emit('success')
                            }
                        })
                    } else {
                        addDistributionMonitor(this.form).then((res) => {
                            if (res.code == 'A0000') {
                                this.$message({
                                    message: '新增成功!',
                                    type: 'success'
                                })
                                this.$emit('success')
                            }
                        })
                    }
                } else {
                    // this.$message({
                    //   message: "警告哦，这是一条警告消息",
                    //   type: "warning"
                    // });
                    return false
                }
            })
        },
        changeType() {
            if (this.form.ifPowerUser == 0) {
                this.form.monitorId = ''
                getPowerClientList({ orgId: this.orgId }).then((res) => {
                    this.IDList = res.data
                })
            } else {
                this.form.monitorId = ''
                getPowerGenerationUserList({ orgId: this.orgId }).then((res) => {
                    this.IDList = res.data
                })
            }
        },
        // 关闭
        handleClose() {
            this.$emit('monitorClose')
        }
    },

    computed: {},

    watch: {}
}
</script>
<style lang="less" scoped>
::v-deep .el-dialog {
    margin-top: 0px !important;
}
</style>
