<template>
    <el-dialog
      :title="dialogTitle"
      v-model="dialogVisible"
      width="640px"
      @close="closeDialog()"
      @open="open"
    >
      <el-form
        ref="formInfo"
        :model="form"
        class="demo-form-inline"
        label-width="100px"
        :rules="rules"
      >
        <el-form-item label="设备名称：" prop="monitorDeviceName">
          <el-input v-model="form.monitorDeviceName"></el-input>
        </el-form-item>
        <el-form-item label="设备编号：" prop="monitorDeviceCode">
          <el-input v-model="form.monitorDeviceCode"></el-input>
        </el-form-item>
        <el-form-item label="回路表记地址:" prop="loopMeterAddress">
          <!-- <el-select
            v-model="form.loopMeterAddress"
            multiple
            filterable
            collapse-tags
            placeholder="请选择"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select> -->
           <el-input-number
              v-model="form.loopMeterAddress"
              controls-position="right"
              :min="0"
              :max="255"
            ></el-input-number>

        </el-form-item>

        <el-form-item label="所属站点：" prop="siteId">
            <el-select
              v-model="form.siteId"
             :disabled="
                deviceNumDataContent != '' &&
                form.siteId != null &&
                form.siteId != ''
                  ? true
                  : false
              "
              placeholder="请选择所属站点"
            >
              <el-option
                v-for="item in siteList"
                :key="item.value"
                :label="item.siteName"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>

        <el-form-item label="额定电压(kV)：" prop="ratedVoltage">
          <el-input v-model="form.ratedVoltage"></el-input>
          <div class="remarksTxt">(数值为线电压)</div>
        </el-form-item>
        <el-form-item label="额定电流(A)：" prop="ratedCurrent">
          <el-input v-model="form.ratedCurrent"></el-input>
        </el-form-item>
        <el-form-item label="电流负载率门限：" prop="currentLoadRate">
          <el-input v-model="form.currentLoadRate"></el-input>
          <div class="remarksTxt">(0-100之间的数字)</div>
        </el-form-item>
        <el-form-item label="设备能力：" prop="qualityAnalysis">
          <el-checkbox v-model="form.qualityAnalysis">电能质量分析</el-checkbox>
          <div class="remarksTxt">(如果未勾选，该设备不参与电能质量分析)</div>
        </el-form-item>

      
        <br />
        <br />
        <br />
        <div style="text-align: right">
          <el-button @click="closeDialog(0)">取消</el-button>
          <el-button type="primary" @click="submitForm()"
            >保存</el-button
          >
        </div>
      </el-form>
    </el-dialog>
</template>

<script>
import { useStore } from 'vuex'
import { defineComponent, ref, watchEffect } from 'vue'
import * as api from '@/api/siteManage/watchDog.js'
import { ElMessage } from 'element-plus'

export default defineComponent({
  name: 'DialogComponent',
  emits: ['closeDialog'],
  props: {
    flag: Boolean,
    dialogTitle: String,
    itemInfo: Object,
  },
  setup(props, context) {
    context
    const store = useStore()
    const dialogVisible = ref(false)
    const formInfo = ref(null)

    const form = ref([])

     const siteList = ref([])

    // open(): Dialog弹窗打开之前做的事
    const open = () => {
      console.log('')
      form.value = props.itemInfo.value;
       siteList.value = store.state.siteList
        console.log('siteList.value')
      console.log(siteList.value)
       console.log('siteList.value')
    }

    const options = [
      {
        value: '选项1',
        label: '站点一',
      },
      {
        value: '选项2',
        label: '站点二',
      },
      {
        value: '选项3',
        label: '站点三',
      },
      {
        value: '选项4',
        label: '站点四',
      },
      {
        value: '选项5',
        label: '站点五',
      },
    ]

    // 关闭弹框
    const closeDialog = () => {
      context.emit('closeDialog', false)
      dialogVisible.value = false
    }

    watchEffect((fn, options) => {
      fn, options
      dialogVisible.value = props.flag
    })

    const roleValid = (rule, value, callback) => {
      rule
      if (value.length === 0) {
        callback(new Error('角色不能为空'))
      } else {
        callback()
      }
    }

    // 保存-修改操作
    const submitForm = () => {
      formInfo.value.validate((valid) => {
        if (valid) {

          form.value.qualityAnalysis =
            form.value.qualityAnalysis == true
              ? (form.value.qualityAnalysis = '是')
              : (form.value.qualityAnalysis = '否')

          if (props.dialogTitle === '新增') {
            api.deviceNewsAdd(form.value).then((requset) => {
              if (requset.status === 'SUCCESS') {
                ElMessage.success({
                  message: '新增成功',
                  type: 'success',
                })
                closeDialog()
              } else {
                ElMessage.error(requset.msg)
              }
            })
          } else {
            api.deviceNewsUpdate(form.value).then((requset) => {
              if (requset.status === 'SUCCESS') {
                ElMessage.success({
                  message: '修改成功',
                  type: 'success',
                })
                closeDialog()
              } else {
                ElMessage.error(requset.msg)
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }

    return {
      closeDialog,
      dialogVisible,
      options,
      roleValid,
      formInfo,
      siteList,
      form,
      open,
      submitForm,
      rules: {
        monitorDeviceName: [
          { required: true, message: "请输入设备名称", trigger: "blur" },
        
        ],
        monitorDeviceCode: [
          { required: true, message: "请输入设备编号", trigger: "blur" },
          
        ],
        loopMeterAddress: [
          { required: true, message: "请选择回路表记地址", trigger: "blur" },
        ],
         siteId: [
          {
            required: true,
            message: '请选择所属站点',
            trigger: 'blur',
          },
        ],
        ratedVoltage: [
          { required: true, message: "请输入额定电压", trigger: "change" },
        ],
        ratedCurrent: [
          { required: true, message: "请输入额定电流", trigger: "blur" },
        ],
        currentLoadRate: [
          { required: true, message: "请输入电流负载率门限", trigger: "blur" },
        ],
      },
    }
  },
})
</script>
 
<style scoped lang="scss">
.el-input,
.el-select {
  width: 240px;
}

// label样式
.el-form-item__label {
  width: 150px;
}
.el-form-item__content {
  margin-left: 150px;
}
</style>