<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="图书id" prop="bookId">
      <el-input v-model="dataForm.bookId" placeholder="图书id"></el-input>
    </el-form-item>
    <el-form-item label="章节id" prop="chapterId">
      <el-input v-model="dataForm.chapterId" placeholder="章节id"></el-input>
    </el-form-item>
    <el-form-item label="章节名称" prop="chapterName">
      <el-input v-model="dataForm.chapterName" placeholder="章节名称"></el-input>
    </el-form-item>
    <el-form-item label="章节内容id" prop="contentId">
      <el-input v-model="dataForm.contentId" placeholder="章节内容id"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="" prop="sort">
      <el-input v-model="dataForm.sort" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="delFlag">
      <el-input v-model="dataForm.delFlag" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userId: '',
          bookId: '',
          chapterId: '',
          chapterName: '',
          contentId: '',
          createTime: '',
          updateTime: '',
          sort: '',
          delFlag: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          bookId: [
            { required: true, message: '图书id不能为空', trigger: 'blur' }
          ],
          chapterId: [
            { required: true, message: '章节id不能为空', trigger: 'blur' }
          ],
          chapterName: [
            { required: true, message: '章节名称不能为空', trigger: 'blur' }
          ],
          contentId: [
            { required: true, message: '章节内容id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/book/bookreadrate/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.bookReadRate.userId
                this.dataForm.bookId = data.bookReadRate.bookId
                this.dataForm.chapterId = data.bookReadRate.chapterId
                this.dataForm.chapterName = data.bookReadRate.chapterName
                this.dataForm.contentId = data.bookReadRate.contentId
                this.dataForm.createTime = data.bookReadRate.createTime
                this.dataForm.updateTime = data.bookReadRate.updateTime
                this.dataForm.sort = data.bookReadRate.sort
                this.dataForm.delFlag = data.bookReadRate.delFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/book/bookreadrate/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'bookId': this.dataForm.bookId,
                'chapterId': this.dataForm.chapterId,
                'chapterName': this.dataForm.chapterName,
                'contentId': this.dataForm.contentId,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'sort': this.dataForm.sort,
                'delFlag': this.dataForm.delFlag
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
