<template>
  <div class="department-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('新增') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="!row.brand_id"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('品牌编号')"
        prop="brand_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('品牌名称')"
        prop="brand_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('品牌描述')"
        prop="brand_desc"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('所属分类')"
        prop="category_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('展示方式')"
        prop="brand_show_type"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('品牌LOGO')"
        prop="brand_image"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.brand_image"
            :preview-src-list="[scope.row.brand_image]"
            :src="scope.row.brand_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatRecommend"
        :label="__('是否推荐')"
        prop="brand_recommend"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否启用')"
        prop="brand_enable"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.brand_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatApply"
        :label="__('品牌申请')"
        prop="brand_apply"
        show-overflow-tooltip
      />
      <template #empty>
        <el-image
          class="vab-data-empty"
          :src="require('@/assets/empty_images/data_empty.png')"
        />
      </template>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, enable, getList } from '@/api/base/product/brand'
  import { getTree } from '@/api/base/product/category'
  import Edit from './components/ProductSellerBrandEdit'
  import { translateTitle as __ } from '@/utils/i18n'

  export default {
    name: 'ProductSellerBrand',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        categorys: [],
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
      this.getTree()
    },
    methods: {
      async getTree() {
        const { data } = await getTree({ category_parent_id: 0 })
        this.categorys = data
      },
      __,
      formatType(row) {
        return row.brand_show_type === 1 ? this.__('图片') : this.__('文字')
      },
      formatRecommend(row) {
        return row.brand_recommend ? this.__('推荐') : this.__('不推荐')
      },
      formatApply(row) {
        return row.brand_apply ? this.__('通过') : this.__('申请中')
      },
      handleEdit(row) {
        if (row.brand_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.brand_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({ brand_ids: row.brand_id })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async sChange(row) {
        const param = {
          brand_name: 'brand_enable',
          brand_id: row.brand_id,
          brand_enable: row.brand_enable,
        }

        const { msg, status } = await enable(param)

        if (status == 200) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
