<template>
    <div>
        <div>
            <el-dialog v-model="dialogVisible" :title="titles" width="30%" @close="closes">
                <!--  -->
                <el-form ref="ruleFormRef" :model="ruleForm" label-width="120px" class="demo-ruleForm" status-icon>
                    <el-form-item label="名称" prop="name">
                        <el-input v-model="ruleForm.name" :disabled="disNum" />
                    </el-form-item>
                    <el-form-item label="电压等级">
                        <el-select v-model="ruleForm.volId" :disabled="disNum" placeholder="请选择电压等级">
                            <template v-for="(item, index) in selectList">
                                <el-option :label="item.name" :value="item.id" />
                            </template>
                        </el-select>
                    </el-form-item>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="cancels">取消</el-button>
                        <el-button type="primary" @click="sureClick">
                            确定
                        </el-button>
                    </span>
                </template>
            </el-dialog>
        </div>
    </div>
</template>

<script>
import { ref, onMounted, toRefs, watch } from 'vue';
import { ElMessage } from 'element-plus';
import litLine from '@/api/litLine';
import systemRow from '@/api/systemRow';
export default {
    props: {
        lookModal: {
            type: Boolean,
            required: true
        },
        needList: {
            type: Object,
            required: true
        },
        listNum: {
            type: Number,
            required: true
        },
        search: {
            type: Function,
            required: true
        }
    },
    setup(props, { emit }) {
        let dialogVisible = ref(false)
        let result = props.lookModal
        let newList = props.needList
        let careNum = props.listNum
        let titles = ref("查看")
        let selectList = ref([])
        let editId = ref("")
        let disNum = ref(false)
        let { cancels, sureClick } = toRefs(props)
        let ruleForm = ref({
            name: "",
            volId: "",
        })
        function searchFlashLel() {
            systemRow.getChildren({ code: "voltage_level" }).then(res => {
                console.log(res, 'vvv');
                selectList.value = res.data
            })
        }
        function lineList() {
            ruleForm.value.name = newList.name
            ruleForm.value.volId = newList.vol_level_id
            editId.value = newList.id
            if (careNum == 0) {
                titles.value = '查看'
                disNum.value = true
            } else {
                titles.value = '编辑'
                disNum.value = false
            }
            dialogVisible.value = result
        }
        function canBack() {
            dialogVisible.value = false
            emit("lookBack", dialogVisible.value)
        }
        function sureBack() {
            if (careNum == 0) {
                dialogVisible.value = false
                emit("lookBack", dialogVisible.value)
            } else {
                litLine.addLine({
                    id: editId.value - 0,
                    name: ruleForm.value.name,
                    vol_id: ruleForm.value.volId
                }).then(res => {
                    if (res.code == 0) {
                        dialogVisible.value = false
                        emit("lookBack", dialogVisible.value)
                        ElMessage({
                            type: "success",
                            message: "修改成功",
                            duration: 1500
                        })
                        props.search()
                    }
                })
            }
        }
        onMounted(() => {
            lineList()
            searchFlashLel()
        })
        return {
            dialogVisible,
            ruleForm,
            cancels: canBack,
            sureClick: sureBack,
            closes: canBack,
            lineList,
            titles,
            editId,
            disNum,
            searchFlashLel,
            selectList,
        }
    }
}
</script>

<style lang="scss" scoped></style>