<template>
  <div class="parentBlock">
    <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="platformAreaName">
          <el-input v-model="form.platformAreaName"></el-input>
        </el-form-item>
        <el-form-item label="台区编号：" prop="platformAreaCode">
          <el-input v-model="form.platformAreaCode"></el-input>
        </el-form-item>
        <el-form-item label="台区地址：" prop="platformAreaAddress">
          <el-input v-model="form.platformAreaAddress"></el-input>
        </el-form-item>
        <!-- <el-form-item label="站点列表:" prop="siteList">
          <el-select
            v-model="form.siteList"
            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-form-item> -->

        <el-form-item
          label="已勾选："
          prop="siteCount"
          v-if="dialogTitle == '编辑'"
        >
          <el-input
            v-model="form.siteCount"
            style="width: 200px"
            disabled
          ></el-input>
          个站点
        </el-form-item>
        <el-form-item
          label="共挂载："
          prop="deviceCount"
          v-if="dialogTitle == '编辑'"
        >
          <el-input
            v-model="form.deviceCount"
            style="width: 200px"
            disabled
          ></el-input>
          个设备
        </el-form-item>
        <br />
        <br />
        <br />
        <div style="text-align: right">
          <el-button @click="closeDialog()">取消</el-button>
          <el-button type="primary" @click="submitForm()">保存</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { defineComponent, ref, watchEffect } from 'vue'
import * as api from '@/api/stationManage/index.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 dialogVisible = ref(false)
    const formInfo = ref(null)

    const form = ref([])

    // open(): Dialog弹窗打开之前做的事
    const open = () => {
      console.log('')
      form.value = props.itemInfo.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) {
          if (props.dialogTitle === '新增') {
            api.platformAreaAdd(form.value).then((requset) => {
              if (requset.status === 'SUCCESS') {
                ElMessage.success({
                  message: '新增成功',
                  type: 'success',
                })
                closeDialog()
              } else {
                ElMessage.error(requset.msg)
              }
            })
          } else {
            api.platformAreaUpdate(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,
      form,
      open,
      submitForm,
      rules: {
        platformAreaName: [
          // required  是否为必填项， trigger:表单验证的触发时机，失去焦点进行验证
          { required: true, message: '请输入台区名称', trigger: 'blur' },
          {
            min: 3,
            max: 6,
            message: '用户名长度在 3 到 6 个字符',
            trigger: 'blur',
          },
        ],
        platformAreaCode: [
          { required: true, message: '请输入台区编号', trigger: 'blur' },
          {
            min: 3,
            max: 6,
            message: '用户名长度在 3 到 6 个字符',
            trigger: 'blur',
          },
        ],
        platformAreaAddress: [
          { required: true, message: '请输入台区地址', trigger: 'blur' },
          {
            min: 3,
            max: 6,
            message: '用户名长度在 3 到 6 个字符',
            trigger: 'blur',
          },
        ],
        // siteList: [
        //   { required: true, message: '请选择站点列表', trigger: 'change' },
        // ],
        // siteCount: [
        //   { required: true, message: '请输入已选站点个数', trigger: 'blur' },
        //   { trigger: 'blur' },
        // ],
        // deviceCount: [
        //   { required: true, message: '请输入挂载设备个数', trigger: 'blur' },
        //   { trigger: 'blur' },
        // ],
      },
    }
  },
})
</script>
 
<style scoped lang="scss">
.el-input,
.el-select {
  width: 240px;
}
</style>