<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="作者姓名" prop="authorName">
      <el-input v-model="dataForm.authorName" placeholder="作者姓名"></el-input>
    </el-form-item>
      <el-form-item label="性别" prop="sex">
        <!-- <el-input v-model="dataForm.sex" placeholder="性别"></el-input> -->
        <el-radio v-model="dataForm.sex" :label=1>男</el-radio>
        <el-radio v-model="dataForm.sex" :label=0>女</el-radio>
      </el-form-item>
    <el-form-item label="年龄" prop="age">
      <el-input v-model="dataForm.age" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="简介" prop="introduction">
      <el-input v-model="dataForm.introduction" placeholder="简介"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="tel">
      <el-input v-model="dataForm.tel" placeholder="电话"></el-input>
    </el-form-item>
    <!-- <el-form-item label="住址" prop="address">
      <el-input v-model="dataForm.address" placeholder="住址"></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,
          authorName: '',
          sex: '',
          age: '',
          introduction: '',
          tel: '',
          address: '',
          createTime: '',
          updateTime: '',
          sort: '',
          delFlag: ''
        },
        dataRule: {
          authorName: [
            { required: true, message: '作者姓名不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          age: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          introduction: [
            { required: true, message: '简介不能为空', trigger: 'blur' }
          ],
          tel: [
            { required: true, message: '电话不能为空', trigger: 'blur' }
          ],
          // address: [
          //   { required: true, message: '住址不能为空', 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/author/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.authorName = data.author.authorName
                this.dataForm.sex = data.author.sex
                this.dataForm.age = data.author.age
                this.dataForm.introduction = data.author.introduction
                this.dataForm.tel = data.author.tel
                this.dataForm.address = data.author.address
                this.dataForm.createTime = data.author.createTime
                this.dataForm.updateTime = data.author.updateTime
                this.dataForm.sort = data.author.sort
                this.dataForm.delFlag = data.author.delFlag
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/book/author/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'authorName': this.dataForm.authorName,
                'sex': this.dataForm.sex,
                'age': this.dataForm.age,
                'introduction': this.dataForm.introduction,
                'tel': this.dataForm.tel,
                'address': this.dataForm.address,
                '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>
