<template>
  <el-dialog
    class="addLineDialog"
    :title="lineDialogTitle"
    :visible.sync="lineDialog"
    width="420px"
  >
    <el-form
      label-position="left"
      ref="formLineInfo"
      label-width="100px"
      :rules="rules"
      :model="lineForm"
    >
      <el-form-item label="名称：" prop="name">
        <el-input
          v-model="lineForm.name"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="开始时间：" prop="startTime">
        <el-input
          v-model.number="lineForm.startTime"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="结束时间：" prop="endTime">
        <el-input
          v-model.number="lineForm.endTime"
          autocomplete="off"
        ></el-input>
      </el-form-item>
      <el-form-item label="间隔：" prop="span">
        <el-input v-model="lineForm.span" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注：" prop="remark">
        <el-input v-model="lineForm.remark" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog(0)">取 消</el-button>
      <el-button type="primary" @click="submitForm()">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { addLineJoggle, updateLineJoggle } from "@/utils/api/index";
export default {
  props: {
    lineDialog: {
      type: Boolean,
    },
    lineDialogTitle: {
      type: String,
      default: "新增",
    },
    formItem: {
      type: Object,
      default: function () {
        return {};
      },
    },
  },

  data() {
    return {
      lineForm: {},
      rules: {
        name: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
        startTime: [
          { required: true, message: "请选择DY等级", trigger: "blur" },
          { type: "number", message: "DY等级数字值" },
        ],
        endTime: [
          { required: true, message: "请选择DY等级", trigger: "blur" },
          { type: "number", message: "DY等级数字值" },
        ],
        span: [
          {
            required: true,
            message: "请输入线路名称",
            trigger: "blur",
          },
        ],
        remark: [
          {
            required: true,
            message: "请输入线路名称",
            trigger: "blur",
          },
        ],
      },
      //表单数组，对象
      formArr: [],
    };
  },
  methods: {
    // 保存确定操作
    submitForm() {
      this.$refs.formLineInfo.validate((valid) => {
        if (valid) {
          let formObj = {
            name: this.lineForm.name,
            startTime: this.lineForm.startTime,
            endTime: this.lineForm.endTime,
            span: this.lineForm.span,
            remark: this.lineForm.remark
          };
          this.formArr.push(formObj);
          console.log(this.formArr);

          // 走保存请求
          if (this.lineDialogTitle == "新增") {
            addLineJoggle({ list: this.formArr })
              .then((res) => {
                console.log(res);
                this.$emit("lineDialogClose", 1); //关闭弹窗
                this.$message.success("添加成功");
              })
              .catch((err) => {
                console.log(err); //代码错误、请求失败捕获
                this.$message.error("添加失败");
              });
          } else if (this.lineDialogTitle == "修改") {
            let changeformObj = {
              name: this.lineForm.name,
              startTime: this.lineForm.startTime,
              endTime: this.lineForm.endTime,
              span: this.lineForm.span,
              remark: this.lineForm.remark
            };
            updateLineJoggle(changeformObj)
              .then((res) => {
                console.log(res);
                this.$emit("lineDialogClose", 1); //关闭弹窗
                this.$message.success("修改成功");
              })
              .catch((err) => {
                console.log(err); //代码错误、请求失败捕获
                this.$message.error("修改失败");
              });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    //取消关闭弹窗
    closeDialog(flag) {
      this.$refs.formLineInfo.resetFields();
      this.$emit("lineDialogClose", flag);
    },
  },
  mounted() {
    console.log("打印传过来的对象", this.formItem);
    console.log("打印传过来的对象", this.formItem.bsIdentifier);
    this.lineForm = JSON.parse(JSON.stringify(this.formItem));
  },
  watch: {
    formItem: {
      handler(newVal, oldVal) {
        // 调用函数
        this.$nextTick(() => {
          this.lineForm = JSON.parse(JSON.stringify(this.formItem));
        });
      },
      immediate: true,
      deep: true,
    },
  },
};
</script>
