<template>
  <div class="subPage" :style="{ padding: '20px' }">
    <div>
      <el-form label-width="100px" >
        <el-form-item label="名称" prop="BookName">
          <el-input v-model="book.BookName" :style="{ width: '200px' }" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="总库存" prop="InStockTotal">
          <el-input v-model="book.InStockTotal" :style="{ width: '200px' }" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="剩余库存" prop="InStockAfter">
          <el-input-number v-model="formData.InStockAfter" :min="0" :style="{ width: '200px' }" ></el-input-number>
        </el-form-item>
        <el-form-item label="盘点备注" prop="ChangeReason">
          <el-input type="textarea" rows="4" v-model="formData.ChangeReason" :style="{ width: '500px' }" ></el-input>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <div :style="{ margin: '10px 0' }">
        <el-table :data="list" border style="width: 100%">
          <el-table-column prop="InStockTotal" label="总库存"></el-table-column>
          <el-table-column prop="InStockBefore" label="盘点前剩余库存"></el-table-column>
          <el-table-column prop="InStockAfter" label="盘点后剩余库存"></el-table-column>
          <el-table-column label="盘点时间">
            <template slot-scope="scope">
              <span>{{ toolClass.dateFormat(scope.row.ChangeDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="ChangeReason" label="盘点备注"></el-table-column>
          <el-table-column prop="ChangeUserName" label="操作人"></el-table-column>
        </el-table>
        <el-pagination
          layout="prev, pager, next"
          :current-page="page"
          :total="1000"
          @current-change="pageChange">
        </el-pagination>
      </div>
      <div>
          <el-button type="primary" @click="submit()">保存</el-button>
          <el-button @click="$router.back()">取消</el-button>
      </div>
    </div>
  </div>
</template>

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

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

export default {
  name: 'stockChange',
  data() {
    return {
      bookId: '',
      formData: {},
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCase: x => x.app.cases.default,
      orgid: x => x.app.user.OrgId,
    }),
    ...mapLibSate({
      book: s => s.detail,
      list: s => s.stock.list,
      page: s => s.stock.page,
    })
  },
  created() {
    this.bookId = this.$route.params.id
    this.filterList()
    this.getBook({ BookId: this.bookId }).then(() => {
      this.formData = {
        BookId: this.book.BookId,
        InStockAfter: this.book.LeftNum,
        CaseId: this.book.CaseId,
        OrgId: this.book.OrgId,
        ChangeReason: '',
        InStockTotal: this.book.InStock,
      }
    }).catch((err) => {
      this.$message({
        showClose: true,
        message: err,
        type: 'error'
      })
    })
  },
  methods: {
    ...mapActions({
      getList: 'getStockList',
      getBook: 'getBookDetail',
      setChange: 'setStockChange',
    }),

    filterList({ page = 0 } = {}) {
      this.getList({
        page,
        BookId: this.bookId
      }).catch(err => {
        this.$message({
          showClose: true,
          message: err,
          type: 'error'
        })
      })
    },

    submit() {
      this.setChange(this.formData).then(() => {
        this.$message({
          showClose: true,
          message: '库存盘点成功',
          type: 'success'
        })
      }).catch(err => {
        this.$message({
          showClose: true,
          message: err,
          type: 'error'
        })
      })
    },

    pageChange(page) {
      this.filterList({ page })
    },
  }
}
</script>
