<template>
  <div class="container">
    <el-form
      :model="list"
      :rules="rules"
      ref="wordDetailForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="单词" prop="word">
        <el-col :span="11">
          <el-input v-model="list.word"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="美式音标" prop="usSoundmark">
        <el-col :span="11">
          <el-input v-model="list.usSoundmark"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item label="英式音标" prop="ukSoundmark">
        <el-col :span="11">
          <el-input v-model="list.ukSoundmark"></el-input>
        </el-col>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm('wordDetailForm')"
          >提交</el-button
        >
        <!-- <el-button @click="resetForm('wordDetailForm')">清空</el-button> -->
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getBaseWordById, updateByPrimaryKeySelective } from "@/api/pandaWords";
import { formatDate } from "@/utils/date";

export default {
  name: "wordDetail",
  data() {
    return {
      id: "",
      list: {},
      rules: {
        word: [{ required: true, message: "必填", trigger: "blur" }]
      }
    };
  },
  methods: {
    getData() {
      getBaseWordById(this.id).then(res => {
        console.log(res);
        this.list = JSON.parse(JSON.stringify(res.data));
        // this.listLoading = false;
        // this.pageNum = res.data.pageNum;
        // this.pageSize = res.data.pageSize;
        // this.total = res.data.total;
      });
    },
    submitForm(formName) {
      for (const key in this.list) {
        if (this.list[key].length == 0) {
          this.list[key] = undefined;
        }
        // console.log(key, "---", this.list[key]);
      }

      this.$refs[formName].validate(valid => {
        if (valid) {
          this.$confirm("是否确认提交?", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(() => {
            updateByPrimaryKeySelective(this.list).then(res => {
              this.$message({
                type: "success",
                message: "提交成功!"
              });
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  filters: {
    formatDateTime(time) {
      if (time == null || time === "") {
        return "";
      }
      let date = new Date(time);
      return formatDate(date, "yyyy-MM-dd hh:mm:ss");
      // return formatDate(date, "yyyy-MM-dd");
    }
  },
  mounted() {
    this.id = this.$route.query.id;
    this.getData();
  }
};
</script>

<style lang="scss" scoped>
.container {
  padding: 30rem;
}
</style>
