<template>
  <div class="subPage" :style="{ padding: '20px' }">
    <div>
      <el-form inline>
        <el-form-item label="案场">
          <el-select v-model="caseId" size="medium" placeholder="请选择案场">
            <el-option
              v-for="item in cases"
              :key="item.CaseId"
              :label="item.CaseName"
              :value="item.CaseId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :style="{ float: 'right' }">
          <el-button type="primary" size="medium" @click="filterList">搜索</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-button plain @click="editType()" size="medium">新增分类</el-button>
      <div :style="{ margin: '10px 0' }">
        <el-table :data="list" border style="width: 100%">
          <el-table-column label="图片" width="128">
            <template slot-scope="scope">
              <img width="64" :src="scope.row.BookTypeImg" alt>
            </template>
          </el-table-column>
          <el-table-column prop="BookTypeName" label="名称"></el-table-column>
          <el-table-column label="案场" width="200">
            <template slot-scope="scope">
              <span>{{ getCaseName(scope.row.CaseId) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="Nums" label="图书数量"></el-table-column>
          <el-table-column fixed="right" label="操作" width="150">
            <template slot-scope="scope">
              <el-button @click="editType(scope.row)" type="text" size="small">编辑</el-button>
              <el-button @click="removeType(scope.row)" type="text" size="small">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          layout="prev, pager, next"
          :current-page="page"
          :total="pagenum"
          @current-change="pageChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>

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

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

export default {
  name: 'bookTypeList',
  data() {
    return {
      caseId: ''
    }
  },
  computed: {
    ...mapState({
      cases: x => x.app.cases.list,
      defaultCase: x => x.app.cases.default,
      orgid: x => x.app.user.OrgId,
    }),
    ...mapLibSate({
      list: s => s.type.list,
      page: s => s.type.page,
      pagenum: s => s.type.pagenum,
    })
  },
  created() {
    this.filterList()
  },
  methods: {
    ...mapActions({
      getList: 'getTypeList',
      deleteType: 'deleteType',
    }),

    filterList({ page = 0 } = {}) {
      const caseId = this.caseId || this.defaultCase
      if (!caseId) return

      this.getList({
        page,
        caseid: caseId,
      }).catch(err => {
        this.$message({
          showClose: true,
          message: err,
          type: 'error'
        })
      })
    },

    pageChange(page) {
      this.filterList({ page })
    },

    removeType({ BookTypeId: id }) {
      this.$confirm('确认删除此数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteType({
          BookTypeId: id
        }).then(() => {
          this.$message({
            showClose: true,
            message: '删除成功',
            type: 'success'
          })
        }).catch((err) => {
          this.$message({
            showClose: true,
            message: err,
            type: 'error'
          })
        })
      }).catch(x => x)
    },

    editType({ BookTypeId: id } = {}) {
      this.$router.push({ name: 'libraryTypeEdit', params: { id } })
    },

    getCaseName(id) {
      return (this.cases.filter(x => x.CaseId === id)[0] || {}).CaseName
    }
  }
}
</script>
