<template>
  <div class="app-container">
    <!-- 组合按钮 -->
    <el-button-group>
      <el-button type="primary" icon="el-icon-plus" @click="$router.replace('/model/add')">添加</el-button>
      <el-button type="danger" icon="el-icon-delete" @click="deletePlaneClass(countSelIdArr($refs.myTable.selection))">删除
      </el-button>
      <el-button type="warning" icon="el-icon-edit" @click="toEditPage()">编辑</el-button>
    </el-button-group>
    <!-- 用户select选项 -->
    <el-button-group class="m-l-20">
      <SelectionShopId v-model="form.shop_id" :allSel="true" />
    </el-button-group>
    <!-- 站点表格 -->
    <el-table class="m-t-20 w-100" ref="myTable"
      :data="classListArr.slice((currentPage - 1) * pageSize, currentPage * pageSize)" border tooltip-effect="dark">
      <el-table-column align="center" type="selection" width="40">
      </el-table-column>
      <el-table-column align="center" prop="id" label="id" width="50">
      </el-table-column>
      <el-table-column prop="class_name" label="机型名称" width="120" min-width="100">
      </el-table-column>
      <el-table-column prop="wheelbase" label="轴距" width="120" min-width="150">
      </el-table-column>
      <el-table-column prop="category" label="机型类型" width="120" min-width="150">
      </el-table-column>
      <el-table-column prop="weight_max" label="最大载重" width="120" min-width="150">
      </el-table-column>
      <el-table-column prop="describe" label="描述" min-width="80" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="controler" label="操作" width="140" min-width="140">
        <template slot-scope="scope">
          <el-button-group>
            <el-button type="danger" icon="el-icon-delete" @click="deletePlaneClass([scope.row.id])">删除</el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination class="m-t-20" layout="prev, pager, next" :current-page.sync="currentPage" :page-size="pageSize"
      :total="classListArr.length">
    </el-pagination>
  </div>
</template>

<script>
import { countSelIdArr } from '@/utils'
import SelectionShopId from '@/components/SelectionShopId'

export default {
  name: 'Model',
  data () {
    return {
      pageSize: 8, // 每页显示记录条数
      currentPage: 1, // 当前页
      form: {
        shop_id: ''
      }
    }
  },
  components: {
    SelectionShopId
  },
  computed: {
    // 机型列表
    planeClassList () {
      return this.$store.state.planeClassList || []
    },
    /**
     * @description: 过滤掉 不对应客户的 站点列表
     * @return: 站点列表
     */
    classListArr () {
      if (this.form.shop_id !== '') {
        return this.planeClassList.filter((item) => item.shop_id === this.form.shop_id)
      } else {
        return this.planeClassList
      }
    }
  },
  methods: {
    countSelIdArr,
    /**
     * @description: 跳转到编辑页面
     */
    toEditPage () {
      const selId = this.countSelIdArr(this.$refs.myTable.selection)
      switch (selId.length) {
        case 0:
          this.$message.error('请选择一条需要编辑的记录')
          break
        case 1:
          this.$router.push('/model/edit/' + selId['0'])
          break
        default:
          this.$message.error('只能选择一条记录')
      }
    },
    /**
     * @description: 删除机型
     */
    deletePlaneClass (idArr) {
      this.$store.dispatch('fetchDelPlaneClass', idArr)
    }
  },
  watch: {
  },
  created () {
  },
  filters: {
    countSelIdArr
  }
}
</script>

<style lang="scss" scoped>
@import "@/styles/theme.scss";

.el-tag {
  i {
    vertical-align: middle
  }
}

.image-placeholder {
  position: relative;
  display: inline-block;
}
</style>
