<template>
  <div class="subPage edit-book" :style="{ padding: '20px' }">
    <el-form :model="formData" :rules="rules" ref="ruleForm" label-width="100px" :style="{ width: '600px' }">
      <div v-if="!active">
        <el-form-item label="条形码" prop="BookBarcode">
          <el-input v-model="formData.BookBarcode" :style="{ width: '200px' }"></el-input>
        </el-form-item>
        <el-form-item :style="{ paddingTop: '20px' }">
          <el-button type="primary" @click="nextStep()">下一步</el-button>
        </el-form-item>
      </div>
      <div v-else>
        <el-form-item label="条形码">
          <el-input v-model="formData.BookBarcode" :style="{ width: '200px' }" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="案场" prop="CaseId">
          <el-select v-model="formData.CaseId" placeholder="请选择案场" :disabled="detail.BookId?true:false" @change="filterTypes">
            <el-option
              v-for="item in cases"
              :key="item.CaseId"
              :label="item.CaseName"
              :value="item.CaseId"
            ></el-option>
          </el-select>
        </el-form-item>
        <div class="red-hint" style="margin-left:100px;">建议上传156*220尺寸的图片</div>
        <el-form-item label="图片" prop="BookImg">
          <el-upload
            class="avatar-uploader"
            action=""
            :show-file-list="false"
            :on-success="uploadSuccess"
            :before-upload="toolClass.beforeUpload"
            :http-request="toolClass.upload"
          >
            <img v-if="formData.BookImg" :src="formData.BookImg + '?x-oss-process=style/compress-rotate'" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="图书名" prop="BookName">
          <el-input v-model="formData.BookName" :style="{ width: '200px' }"></el-input>
        </el-form-item>
        <el-form-item label="分类" prop="BookTypeId">
          <el-select v-model="formData.BookTypeId" placeholder="请选择分类">
            <el-option
              v-for="item in types"
              :key="item.BookTypeId"
              :label="item.BookTypeName"
              :value="item.BookTypeId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作者" prop="Author">
          <el-input v-model="formData.Author" :style="{ width: '200px' }"></el-input>
        </el-form-item>
        <el-form-item label="出版社" prop="Publisher">
          <el-input v-model="formData.Publisher" :style="{ width: '200px' }"></el-input>
        </el-form-item>
        <el-form-item label="价格" prop="Price">
          <el-input-number v-model="formData.Price" :precision="2" controls-position="right" :style="{ width: '200px' }"></el-input-number>
        </el-form-item>
        <el-form-item label="总数量" prop="InStock">
          <el-input-number v-model="formData.InStock" :disabled="detail.BookId?true:false" controls-position="right" :style="{ width: '200px' }"></el-input-number>
        </el-form-item>
        <el-form-item label="借阅时间" prop="BorrowDays">
          <el-input-number v-model="formData.BorrowDays" controls-position="right" :style="{ width: '200px' }"></el-input-number>
          <span> /天</span>
        </el-form-item>
        <el-form-item label="图书描述" prop="BookDescription">
          <el-input type="textarea" rows="4" v-model="formData.BookDescription" :style="{ width: '400px' }"></el-input>
        </el-form-item>
        <el-form-item :style="{ paddingTop: '40px' }">
          <el-button type="primary" @click="submit()">保存</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import { createNamespacedHelpers, mapState } from 'vuex'

const {
  mapState: mapLibSate,
  mapActions,
  mapMutations
} = createNamespacedHelpers('library')

export default {
  name: 'bookDetail',
  data() {
    return {
      active: 0,
      rules: {
        BookBarcode: [{ required: true, message: '请录入条形码', trigger: 'blur' }],
        BookName: [{ required: true, message: '请输入图书名称', trigger: 'blur' }],
        // BookImg: [{ required: true, message: '请上传图片', trigger: 'change' }],
        CaseId: [{ required: true, message: '请选择一个案场', trigger: 'change' }],
        BookTypeId: [{ required: true, message: '请选择一个分类', trigger: 'change' }],
        InStock: [{ required: true, message: '请设置总数量', trigger: 'change' }],
        BorrowDays: [{ required: true, message: '请设置借阅时间', trigger: 'change' }],
      },
      formData: {},
    }
  },
  created() {
    const id = this.$route.params.id
    if (id) {
      this.active = 1

      this.getInfo({ id }).then((x) => {
        this.filterTypes(x.CaseId)
      }).catch(err => {
        this.$message({
          showClose: true,
          message: err,
          type: 'error'
        })
      })
    } else {
      this.active = 0
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCase: x => x.app.cases.default,
      orgid: x => x.app.user.OrgId
    }),
    ...mapLibSate({
      detail: s => s.detail,
      types: s => s.type.list,
    }),
  },
  watch: {
    detail: {
      handler (nv, ov) {
        if (nv.BookId === this.formData.BookId) {
          return
        }

        if (!this.$route.params.id) {
          return
        }

        let CaseId = nv.CaseId
        if (!CaseId) {
          CaseId = this.defaultCase
        }

        this.formData = { ...nv, CaseId }
      },
      immediate: true,
    },
  },
  beforeRouteLeave (to, from, next) {
    this.resetStore('detail')
    this.resetStore('type.list')
    next()
  },
  methods: {
    ...mapMutations({
      mergeDetail: 'mergeBookDetail',
      resetStore: 'clearData',
    }),
    ...mapActions({
      getInfo: 'getBookDetail',
      getTypeList: 'getTypeList',
      updateDetail: 'updateBookDetail'
    }),

    nextStep() {
      if (!this.formData.BookBarcode) {
        this.$message({
          showClose: true,
          message: '请录入条形码',
          type: 'error'
        })
        return
      }

      this.$confirm('条形码一旦输入将不能改变!', '请确认条形码正确', {
        confirmButtonText: '已确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.active = 1
        })
        .catch(x => x)
    },

    uploadSuccess(res) {
      this.$set(this.formData, 'BookImg', res.result.url)
      // this.formData.BookImg = res.result.url
    },

    filterTypes(val) {
      this.getTypeList({ page: 0, caseid: val, pagesize: 10000 })
    },

    submit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (!this.formData.Price) {
            this.formData.Price = 0
          }

          this.updateDetail(this.formData)
            .then(() => {
              this.$message({
                showClose: true,
                message: '更新或编辑成功',
                type: 'success'
              })
              this.$router.push({ name: 'libraryList' })
            })
            .catch(err => {
              this.$message({
                showClose: true,
                message: err,
                type: 'error'
              })
            })
        } else {
          this.$message({
            showClose: true,
            message: '表单填写错误',
            type: 'error'
          })
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
.edit-book  {
  .el-input__inner {
    text-align: left;
  }
}
</style>
