<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 disabled v-model="list.word"></el-input>
        </el-col>
      </el-form-item>
            <el-form-item label="英式音标" prop="intro">
        <el-col :span="11">
          <el-input v-model="list.ukSoundmark"></el-input>
        </el-col>
      </el-form-item>
            <el-form-item label="美式音标" prop="intro">
        <el-col :span="11">
          <el-input v-model="list.usSoundmark"></el-input>
        </el-col>
      </el-form-item>
          <el-form-item label="等级" prop="level">
        <el-col :span="11">
          <el-select v-model="list.level" placeholder="请选择" disabled>
            <el-option
              v-for="item in levelList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
        <el-form-item label="简明释义" prop="simpleIntro">
        <el-col :span="11">
          <el-input v-model="list.simpleIntro"></el-input>
        </el-col>
      </el-form-item>


      <el-form-item label="释义" prop="intro">
        <el-col :span="11">
          <el-input v-model="list.intro"></el-input>
        </el-col>
      </el-form-item>


      <el-form-item
        v-for="(item, index) in list.exampleMap"
        :key="index"
        :label="'例句' + (index + 1)"
        prop="exampleMap"
        class="item"
      >
        <el-col :span="11">
          <el-input v-model="item.sContent"></el-input>
        </el-col>
        <el-col class="line" :span="1">-</el-col>
        <el-col :span="8">
          <el-input v-model="item.sCn"></el-input>
        </el-col>
        <el-button
          @click="handleDel(index)"
          type="danger"
          size="mini"
          icon="el-icon-delete"
          circle
        ></el-button>
      </el-form-item>

      <el-form-item label="" prop="">
        <el-button @click="handleAdd" size="mini">添加例句</el-button>
      </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 {
  getGradeWordById,
  updateByPrimaryKeySelectiveGrade
} from "@/api/pandaWords";
import { formatDate } from "@/utils/date";

export default {
  name: "wordGradeDetail",
  data() {
    return {
      id: "",
      list: {},

      levelList: [
        {
          value: "0",
          label: "学前"
        },
        {
          value: "1",
          label: "小学"
        },
        {
          value: "2",
          label: "初中"
        },
        {
          value: "3",
          label: "高中"
        },
        {
          value: "4",
          label: "大学"
        },
        {
          value: "5",
          label: "成人"
        }
      ],
      rules: {
        word: [{ required: true, message: "必填", trigger: "blur" }],
        // intro: [{ required: true, message: "必填", trigger: "blur" }],
        simpleIntro: [{ required: true, message: "必填", trigger: "blur" }],
        level: [{ required: true, message: "必填", trigger: "change" }]
      }
    };
  },
  methods: {
    getData() {
      getGradeWordById(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;
      });
    },
    handleAdd() {
      this.list.exampleMap.push({
        sContent: "",
        sCn: ""
      });
    },
    handleDel(index) {
      this.list.exampleMap.splice(index, 1);
    },
    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(() => {
            updateByPrimaryKeySelectiveGrade(this.list).then(res => {
              this.$message({
                type: "success",
                message: "提交成功!"
              });
            });
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });

      // 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>
// .item {
//   display: flex;
//   flex-direction: column;
// }
.container {
  padding: 30rem;
}
</style>
