<template>
  <div class="drawer_info">
    <div class="drawer_title">{{ dataForm.id ? "修改" : "新增" }}</div>
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRule"
      @keyup.enter.native="validateForm('dataForm')"
      class="drawer_list"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item prop="title" label="标题">
            <el-input
              v-model="dataForm.title"
              maxlength="20"
              placeholder="请输入标题"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="content" label="内容">
            <el-input
              v-model="dataForm.content"
              maxlength="20"
              placeholder="请输入内容"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="workDate" label="日期">
             <el-date-picker
            v-model="dataForm.workDate"
            type="date"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          >
          </el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="save_btn">
      <el-button type="primary" @click="validateForm('dataForm')"
        >提交</el-button
      >
      <el-button plain @click="resetForm('dataForm')">重置</el-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      id: "",
      menuList: [],
      dataForm: {
        id: "",
        title: "",
        content:"",
        workDate: "",
      },
    };
  },
  computed: {
    dataRule() {
      return {
        title: [
          {
            required: true,
            message: "请输入标题",
            trigger: "blur",
          },
        ],
        content: [
          {
            required: true,
            message: "请输入内容",
            trigger: "blur",
          },
        ],
        workDate:[
          {
            required: true,
            message: "请选择日期",
            trigger: "blur",
          },
        ]
      };
    },
  },
  created() {
    //this.getMenuList();
  },
  methods: {
    init() {
      this.resetForm("dataForm");
      this.$nextTick(() => {
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/work/workprogram/${this.dataForm.id}`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg);
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data,
        };
      });
    },
    //表单重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dataForm.id = this.id;
    },
    //表单验证
    validateForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submitForm();
        } else {
          return false;
        }
      });
    },
    // 表单提交
    submitForm() {
      this.$http[!this.dataForm.id ? "post" : "put"](
        "/work/workprogram",
        this.dataForm
      ).then((res) => {
        if (res.data.code == 0) {
          this.$message({
            type: "success",
            message: "保存成功",
          });
          this.$emit("refreshDataList");
        } else {
          this.$message({
            type: "error",
            message: res.data.msg,
          });
        }
      });
    },
  },
};
</script>
<style lang="scss">
.customWidth.alert {
  width: 50%;
  .el-dialog__title {
    border-left: 4px solid #ddd;
    padding-left: 8px;
    border-color: #5776e6;
  }
}
</style>