<template>
  <div class="department-management-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="8">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="16">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.brand_name"
              clearable
              :placeholder="__('请输入品牌名称')"
              style="width: 200px"
            />
          </el-form-item>
          <el-form-item prop="brand_apply">
            <el-select
              v-model.trim="queryForm.brand_apply"
              clearable
              :placeholder="__('品牌申请状态')"
              style="width: 140px"
            >
              <el-option label="拒绝" :value="0"/>
              <el-option label="通过" :value="1"/>
              <el-option label="待审核" :value="2"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" type="primary" @click="queryData">
              {{ __('刷新') }}
            </el-button>
          </el-form-item>
        </el-form>
      </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
        v-if="isAdmin"
        align="center"
        label="店铺名称"
        prop="store_name"
        show-overflow-tooltip
        width="200"
      >
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('品牌编号')"
        prop="brand_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('品牌名称')"
        prop="brand_name"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('品牌描述')"
        prop="brand_desc"
        show-overflow-tooltip
        width="300"
      />
      <el-table-column
        align="center"
        :label="__('所属分类')"
        prop="category_name"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatType"
        :label="__('展示方式')"
        prop="brand_show_type"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('品牌LOGO')"
        prop="brand_image"
        show-overflow-tooltip
        width="100"
      >
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" trigger="hover">
            <img
              v-if="scope.row.brand_image"
              alt=""
              :src="scope.row.brand_image"
              style="width: 150px; height: 150px"
            />
            <img
              v-if="scope.row.brand_image"
              slot="reference"
              :src="scope.row.brand_image"
              style="width: 30px; height: 30px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatRecommend"
        :label="__('是否推荐')"
        prop="brand_recommend"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否启用')"
        prop="brand_enable"
        show-overflow-tooltip
        width="100"
      >
        <template #default="{ row }">
          <el-tag :type='({"3000":"warning","1":"success","0":"info"})[row.brand_enable]'>
            {{ ({"0":"禁用","1":"启用","3002":"待审核"})[row.brand_enable] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('店铺编号')"
        prop="store_id"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('所属店铺')"
        prop="store_name"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :formatter="formatApply"
        :label="__('品牌申请')"
        prop="brand_apply"
        show-overflow-tooltip
        width="100"
      >
        <template #default="{ row }">
          <el-tag :type='({"0":"warning","1":"success","0":"info"})[row.brand_apply]'>
            {{ ({"0":"拒绝","1":"通过","2":"待审核"})[row.brand_apply] }}
          </el-tag>
        </template>
      </el-table-column>
      <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" :categorys="categorys" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { doDelete, getList } from '@/api/base/product/brand'
  import { getTree } from '@/api/base/product/category'
  import Edit from './components/brandManagementEdit'
  import { translateTitle as __ } from '@/utils/i18n'
  import { mapState } from "vuex";
  export default {
    name: 'BrandManagement',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        categorys: [],
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          brand_name: '',
          brand_apply: null,
        },
      }
    },
    computed:{
      ...mapState('user',['isAdmin'])
    },
    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.__('不推荐')
      },
      formatEnable(row) {
        return row.brand_enable ? 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()
          })
        } else {
          if (this.selectRows.length > 0) {
            const brand_ids = this.selectRows
              .map((item) => item.brand_id)
              .join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ brand_ids })
                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }
                await this.fetchData()
              }
            )
          } else {
            this.$baseMessage(
              this.__('未选中任何行'),
              'error',
              false,
              'vab-hey-message-error'
            )
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
