<template>
  <div class="padding-20">
    <div class="search-box">
      <el-form ref="form"
               :inline="true"
               :model="searchForm"
               clearable
               class="mt-10">
        <el-form-item label="分类名称:">
          <el-input v-model="searchForm.category_name" placeholder="请输入分类名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="是否上架:">
          <el-select clearable v-model="searchForm.category_status">
            <el-option label="上架" :value="1"></el-option>
            <el-option label="下架" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="ml-10">
          <el-button icon="el-icon-search" type="primary" @click="searchSubmit">查询</el-button>
        </el-form-item>
        <el-form-item class="ml-10">
          <el-button icon="el-icon-plus" type="primary" v-permission="'business_pms_category_add'" @click="add">添加分类
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData"
              stripe
              v-loading="tableLoading"
              fit
              class="marginT-10 order-table"
              border
              :max-height="vheight">
      <el-table-column label="ID" prop="id" width="80"></el-table-column>
      <el-table-column label="分类名称" prop="category_name" width="240"></el-table-column>
      <el-table-column label="商品数量" prop="product_num"></el-table-column>
      <el-table-column label="排序" prop="category_sort_id"></el-table-column>
      <el-table-column label="分类状态">
        <template slot-scope="scope">
          <p>{{ ['下架', '上架'][scope.row.category_status] }}</p>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" prop="created_at" width="180"></el-table-column>
      <el-table-column label="修改时间" prop="updated_at" width="180"></el-table-column>
      <el-table-column label="操作" width="120">
        <template slot-scope="scope">
          <el-button type="text"
                     v-permission="'business_pms_category_edit'"
                     @click="edit(scope.row)">编辑
          </el-button>
          <el-button type="text"
                     v-permission="'business_pms_category_del'"
                     @click="del(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="marginT-20"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :hide-on-single-page="true"
      :current-page="page"
      :page-size="page_size"
      :page-sizes="[10, 20, 100, 200, 300, 400]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"/>
    <detail v-if="detailsDialog.show"
            v-model="detailsDialog.show"
            :exData="detailsDialog.exData"
            @success="init"></detail>
  </div>
</template>

<script>
import page from '@/mixin/page'
import detail from './details'

export default {
  mixins: [page],
  components: {
    detail,
  },
  data () {
    return {
      detailsDialog: {
        show: false,
        exData: {}
      },
      searchForm: {},
      tableData: [],
      tableUrl: '/v1/bar/product/category/list'
    }
  },
  methods: {
    add () {
      this.detailsDialog.exData = {}
      this.detailsDialog.show = true
    },
    edit (row) {
      this.detailsDialog.exData = row
      this.detailsDialog.show = true
    },
    del (row) {
      this.$confirm('确定要删除吗', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const data = await this.$fetch('/v1/bar/product/category/delete', { id: row.id }, 'get')
        if (data.code === 200) {
          this.$message.success('删除成功')
          this.init()
          this.$store.dispatch('common/fetchCategory')
        }
      }).catch(() => {})
    }
  },
  mounted () {
    this.init()
  }
}
</script>

<style lang="scss" scoped>
.el-dropdown {
  margin: 0 10px;

  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
}
</style>
