<template>
  <!-- 管理员配置 -->
  <el-dialog
    :title="dialogTitle"
    v-model="cloneDialog"
    @close="close"
    @open="open"
  >
    <div class="dialogOne">
      <el-form
        :model="form"
        :rules="rules"
        ref="ruleForm"
        label-width="150px"
        class="demo-ruleForm"
      >
        <el-form-item label="管理员姓名:" prop="nickName">
          <el-input
            v-model="form.nickName"
            placeholder="请输入管理员姓名"
            clearable
          />
        </el-form-item>
        <el-form-item label="管理员账号:" prop="userName">
          <el-input
            v-model="form.userName"
            placeholder="请输入管理员账号"
            clearable
          />
        </el-form-item>
        <el-form-item label="管理员邮箱:" prop="email">
          <el-input
            v-model="form.email"
            placeholder="请输入管理员邮箱"
            clearable
          />
        </el-form-item>
        <el-form-item label="管理员手机号:" prop="phonenumber">
          <el-input
            v-model="form.phonenumber"
            placeholder="请输入管理员手机号"
            clearable
          />
        </el-form-item>
        <el-form-item label="管理员密码:" prop="password" v-if="dialogTitle != '修改'">
          <el-input
            v-model="form.password"
            placeholder="请输入管理员密码"
            clearable
          />
        </el-form-item>
        <el-form-item label="状态:" prop="status">
          <el-select v-model="form.status" placeholder="请选择">
            <el-option label="正常" :value="0"></el-option>
            <el-option label="停用" :value="1"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="cloneDialog = false">取消</el-button>
        <el-button type="primary" @click="submit()">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { useStore } from 'vuex'
import { defineComponent, ref, watchEffect } from 'vue'
import * as api from '@/api/tenantManage/adminConfig.js'
import { ElMessage } from 'element-plus'
import { validatorPhone, isEmail } from '@/utils'

export default defineComponent({
  name: 'powerEquip',
  components: {},
  emits: ['closeNo', 'select'],
  props: {
    show_dialog2: Boolean,
    DialogArray: Object,
    dialogTitle: String,
    itemInfo: {
      type: Object,
      default: function () {
        return {}
      },
    },
  },
  setup(props, context) {
    const store = useStore()
    store

    const form = ref({})

    const cloneDialog = ref(false) //管理员配置弹窗控制
    const ruleForm = ref(null)

    const submit = () => {
      ruleForm.value.validate((valid) => {
        if (valid) {
          if (props.dialogTitle === '修改') {
            api.updateserData(form.value).then((requset) => {
              if (requset.status === 'SUCCESS') {
                ElMessage.success({
                  message: '修改成功',
                  type: 'success',
                })
                close()
              } else {
                ElMessage.error(requset.msg)
              }
            })
          } else {
            api.addUserData(form.value).then((requset) => {
              if (requset.status === 'SUCCESS') {
                ElMessage.success({
                  message: '配置成功',
                  type: 'success',
                })
                close()
              } else {
                ElMessage.error(requset.msg)
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }

    //open
    const open = () => {
      form.value = props.itemInfo
      console.log('form.value')
      console.log(form.value)

    }

    // close(): Dialog弹窗关闭之后做的事
    const close = () => {
      resetForm()

      context.emit('closeNo', false)
      cloneDialog.value = false
    }
    function resetForm() {
      ruleForm.value.resetFields()
    }

    watchEffect((fn, options) => {
      fn, options
      cloneDialog.value = props.show_dialog2
    })

    return {
      store,
      open,
      close,
      submit,
      form,
      cloneDialog,

      ruleForm,
      resetForm,
      rules: {
        nickName: [
          {
            required: true,
            message: '管理员姓名不能为空',
            trigger: 'blur',
          },
        ],
        userName: [
          {
            required: true,
            message: '管理员账号不能为空',
            trigger: 'blur',
          },
        ],
        email: [
          {
            required: true,
            message: '管理员邮箱不能为空',
            trigger: 'blur',
          },
          {
            validator: isEmail,
            message: '输入的邮箱地址不正确',
            trigger: 'blur',
          },
        ],
        phonenumber: [
          {
            required: true,
            message: '管理员手机号不能为空',
            trigger: 'blur',
          },
          {
            validator: validatorPhone,
            message: '输入的电话号码不正确',
            trigger: 'blur',
          },
        ],
        password: [
          {
            required: true,
            message: '密码不能为空',
            trigger: 'blur',
          },
        ],
        status: [
          {
            required: true,
            message: '转态不能为空',
            trigger: 'blur',
          },
        ],
      },
    }
  },
})
</script>