<template>
  <transition name="dialog-fade">
    <el-dialog
      v-if="showDialog"
      :title="dialogTitle"
      class="dialog-component"
      v-model="showDialog"
      width="640px"
      @close="closeDialog(0)"
    >
      <el-form
        ref="formInfo"
        :model="formInfo"
        class="demo-form-inline"
        label-width="100px"
        :rules="rules"
      >
        <el-form-item label="站点名称：" prop="watchName" label-width="150px">
          <el-input v-model="formInfo.watchName"></el-input>
        </el-form-item>

        <el-form-item
          label="开始停电时间："
          prop="watchCode"
          label-width="150px"
        >
          <!-- <el-date-picker
            v-model="startTime"
            type="datetime"
            placeholder="选择日期时间"
            :picker-options="pickerBeginDate"
            clearable
            style="width:240px"
          ></el-date-picker> -->

          <el-date-picker
            v-model="startTime"
            type="datetime"
            placeholder="Select date and time"
          ></el-date-picker>
        </el-form-item>

        <el-form-item
          label="结束停电时间:"
          prop="routeAddress"
          label-width="150px"
        >
          <!-- <el-date-picker
            v-model="endTime"
          
            type="datetime"
            placeholder="选择日期时间"
            :picker-options="pickerEndDate"
            clearable
            style="width:240px"
          ></el-date-picker>
 -->

          <el-date-picker
            v-model="endTime"
            :disabledDate="pickerOptions"
            type="datetime"
            placeholder="Select date and time"
          ></el-date-picker>
        </el-form-item>

        <el-form-item
          label="提交时间："
          prop="ratedVoltage"
          label-width="150px"
        >
          <el-input v-model="formInfo.ratedVoltage"></el-input>
        </el-form-item>
        <el-form-item
          label="提交人："
          prop="stationAddress"
          label-width="150px"
        >
          <el-input v-model="formInfo.ratedCurrent"></el-input>
        </el-form-item>
        <el-form-item label="状态：" prop="fzlMx" label-width="150px">
          <el-input v-model="formInfo.fzlMx"></el-input>
        </el-form-item>

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

<script>
export default {
  name: 'DialogComponent',
  props: {
    dialogTitle: {
      type: String,
      default: '新增',
    },
    itemInfo: {
      type: Object,
      default: function () {
        return {}
      },
    },
  },
  data() {
    //限制不能选之后的日期
    this.pickerBeginDate = {
      disabledDate: (time) => {
        alert(2)
        let endTime = this.endTime
        if (endTime) {
          return time.getTime() > new Date(endTime).getTime()
        }
      },
    }
    // /限制不能选之前的时间
    this.pickerEndDate = {
      disabledDate: (time) => {
        let beginTime = this.beginTime
        if (beginTime) {
          return time.getTime() < new Date(beginTime).getTime()
        }
      },
    }

    return {
      startTime: '',
      endTime: '',

      checked: true,
      showDialog: false,
      formInfo: JSON.parse(JSON.stringify(this.itemInfo)),
      options: [
        {
          value: '选项1',
          label: '站点一',
        },
        {
          value: '选项2',
          label: '站点二',
        },
        {
          value: '选项3',
          label: '站点三',
        },
        {
          value: '选项4',
          label: '站点四',
        },
        {
          value: '选项5',
          label: '站点五',
        },
      ],

      rules: {
        watchName: [
          // required  是否为必填项， trigger:表单验证的触发时机，失去焦点进行验证
          { required: true, message: '请输入站点名称', trigger: 'blur' },
          {
            min: 3,
            max: 6,
            message: '用户名长度在 3 到 6 个字符',
            trigger: 'blur',
          },
        ],
        beginTime: [
          { required: true, message: '请输入开始停电时间', trigger: 'blur' },
          {
            min: 3,
            max: 6,
            message: '用户名长度在 3 到 6 个字符',
            trigger: 'blur',
          },
        ],
        endTime: [
          { required: true, message: '请选则结束停电时间', trigger: 'change' },
        ],
        ratedVoltage: [
          { required: true, message: '请选择站点列表', trigger: 'change' },
        ],
        ratedCurrent: [
          { required: true, message: '请输入已选站点个数', trigger: 'blur' },
          { trigger: 'blur' },
        ],
        fzlMx: [
          { required: true, message: '请输入挂载设备个数', trigger: 'blur' },
          { trigger: 'blur' },
        ],
      },
    }
  },
  methods: {
    pickerOptions(){
      disabledDate(time) {
        time
        return false //禁止选择以前的时间
        // return time.getTime() > Date.now();//禁止选择以后的时间
      },
    },
    roleValid(rule, value, callback) {
      if (value.length === 0) {
        callback(new Error('角色不能为空'))
      } else {
        callback()
      }
    },

    // onSelectedDrug(event) {
    //   this.routeAddress = event;
    //   console.log(this.routeAddress);
    // },
    // 保存操作
    submitForm(formName) {
      const params = Object.assign(this.formInfo, {})
      params
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // 走保存请求
          this.$message({
            message: '操作成功！',
            type: 'success',
          })
          this.closeDialog(1)
        } else {
          return false
        }
      })
    },
    // 关闭弹框
    closeDialog(flag) {
      this.$refs['formInfo'].resetFields()
      this.showDialog = false
      this.$emit('closeDialog', flag)
    },
  },
}
</script>
 
<style scoped lang="scss">
.el-input,
.el-select,
.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 240px;
}

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