<template>
  <el-dialog
    class="addLineDialog"
    :title="title"
    :visible.sync="isShow"
    :close-on-click-modal="false"
    width="40%"
  >
    <el-form
      label-position="left"
      ref="formInfo"
      label-width="100px"
      :rules="rules"
      :model="formdata"
    >
      <el-form-item label="名称：" prop="name">
        <el-input
          v-model="formdata.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="时间：" prop="time">
        <el-time-picker
          is-range
          v-model="formdata.time"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          value-format="HH:mm:ss">
        </el-time-picker>
      </el-form-item>
      <el-form-item label="间隔：" prop="span">
        <!-- <el-input v-model="formdata.span" autocomplete="off" type="number"></el-input> -->
        <el-input-number v-model="formdata.span"  :min="1" ></el-input-number>
      </el-form-item>
      <el-form-item label="备注：" prop="remark">
        <el-input v-model="formdata.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { addScheduleRulel, updateScheduleRulel } from "@/utils/api/index";
export default {
  props: {
    title:String
  },
  data() {
    return {
      isShow: false,
      formdata: {},
      rules: {
        name: [
          { required: true, message: "请输入名称", trigger: "blur" },
        ],
        time: [
          { required: true, message: "请选择时间", trigger: "blur" },
        ],
        span: [
          { required: true, message: "请输入间隔", trigger: "blur", },
        ],
        remark: [
          { required: true, message: "请输入备注", trigger: "blur", },
        ],
      }
    };
  },
  methods: {
    //判断
    getdataform(val){
      console.log(val)
      if (val==null) {
        return this.formdata = {}
      } 
      this.formdata = val
      this.$set(this.formdata,'time',[val.startTime, val.endTime])
    },
    // 保存确定操作
    submitForm() {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          this.formdata.startTime=this.formdata.time[0]
          this.formdata.endTime=this.formdata.time[1]
          delete this.formdata.time
          if (this.title == "新增") {
            let formArr=[]
            formArr.push(this.formdata)
            addScheduleRulel({ list: formArr })
              .then((res) => {
                this.isShow = false
                this.$message.success("添加成功");
                this.$parent.deviceList()
              })
              .catch((err) => {
                this.$message.error("添加失败");
              });
          } else {
            updateScheduleRulel(this.formdata)
              .then((res) => {
                this.isShow = false
                this.$message.success("修改成功");
                this.$parent.deviceList()
              })
              .catch((err) => {
                this.$message.error("修改失败");
              });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    display() {
      this.isShow = true
    },
    hide() {
      this.isShow = false
    }

  },
  mounted() {
    
  },
};
</script>
