<template>
  <Dialog
    :is-dialog-exist="isDialog"
    :title="dialogTitle"
    :dialog-style="dialogStyle"
    @close-dialog="handleClose"
  >
    <el-container>
      <el-main>
        <el-form
          ref="formData"
          :model="formData"
          label-width="auto"
        >
          <el-form-item label="电子书ID">
            <el-input
              v-model="formData.book_id"
              placeholder="电子书ID"
              :disabled="true"
            />
          </el-form-item>
          <el-form-item label="书名">
            <el-input
              v-model="formData.book_name"
              placeholder="请输入"
            />
          </el-form-item>
          <el-form-item label="书源">
            <el-input
              v-model="formData.source_name"
              placeholder="书源"
              :disabled="true"
            />
          </el-form-item>
          <el-form-item label="分类">
            <el-select
              v-model="formData.book_type"
              placeholder="请输入"
              style="width: 100%"
            >
              <el-option
                v-for="(item, index) in categoryList"
                :key="index"
                :label="item.category_name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="简介">
            <el-input
              v-model="formData.book_intro"
              type="textarea"
              :rows="5"
              resize="false"
              placeholder="简介"
            />
          </el-form-item>
        </el-form>
      </el-main>
      <el-footer>
        <el-button
          type="primary"
          @click="_postRequest()"
        >
          确认
        </el-button>
        <el-button @click="handleClose">
          取消
        </el-button>
      </el-footer>
    </el-container>
  </Dialog>
</template>

<script>
import Dialog from '@/components/Dialog/index'
import { updateBooks, booksCategory } from '@/api/books-management'
export default {
  components: { Dialog },
  props: {
    isDialog: {
      type: Boolean,
      default: false
    },
    selectItem: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      // dialog样式
      dialogStyle: {
        '--dialog-width': '1000px',
        '--dialog-height': '510px',
        '--title-height': '51px'
      },
      // 弹窗标题
      dialogTitle: '编辑书籍',
      formData: {
        id: '',
        book_id: '',
        book_name: '',
        book_intro: '',
        book_type: '',
        source_name: '',
        source_platform: ''
      },
      categoryList: []
    }
  },
  watch: {
    isDialog(val) {
      if (val) {
        this.formData = this.selectItem
        // 书源分类请求
        booksCategory({ source_platform: this.formData.source_platform }).then(resp => {
          if (parseInt(resp.error_code) === 0) {
            this.categoryList = resp.data
          } else {
            this.$message.error(resp.message)
          }
        })
      }
    }
  },
  methods: {
    _postRequest() {
      const params = {
        name: this.formData.book_name,
        category_id: this.formData.book_type,
        desc: this.formData.book_intro
      }
      updateBooks(Number(this.formData.id), params).then(resp => {
        if (parseInt(resp.error_code) === 0) {
          this.$message.success('操作成功！')
          this.$emit('handle-success')
          this.handleClose()
        } else {
          this.$message.error(resp.message)
        }
      })
    },
    handleClose() {
      this.$emit('handle-close')
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-container{
    width: 100%;
    height: 100%;
    .el-main{
      width: 100%;
      height: 90%;
      padding: 2% 15% 0;
    }
    .el-footer{
      width: 100%;
      height: 10%;
      display: flex;
      justify-content: center;
      align-items: center;
      box-shadow: 0 5px 10px #000000;
    }
  }
</style>
