<template>
  <Container>
    <div slot="left">
      <el-input
        v-model="keyword"
        size="small"
        style="width: 220px; margin-right: 16px"
        clearable
        placeholder="编号/品牌名称"
        @keyup.enter.native="searchData"
        @clear="searchData"
      >
        <el-button slot="append" @click="searchData">
          <i class="el-icon-search"></i>
        </el-button>
      </el-input>
      <el-button
        v-if="$accessCheck($Access.BrandManageAddBrand)"
        style="margin-right: 20px"
        size="small"
        type="primary"
        @click="addBrand"
      >
        新增品牌
      </el-button>
    </div>
    <el-table :data="brand_list" @row-dblclick="goDetail">
      <el-table-column
        prop="id"
        label="ID"
        fixed="left"
        width="50"
        align="left"
      ></el-table-column>
      <el-table-column label="品牌" align="left" min-width="80">
        <template slot-scope="scope">
          <el-image fit="cover" :src="scope.row.images"></el-image>
        </template>
      </el-table-column>
      <el-table-column
        prop="code"
        label="编码"
        align="left"
        min-width="120"
      ></el-table-column>
      <el-table-column
        prop="title"
        label="品牌名称"
        align="left"
        min-width="100"
      ></el-table-column>
      <el-table-column
        prop="goodsBasicTotal"
        label="品牌商品数量"
        align="left"
        min-width="100"
      ></el-table-column>
      <el-table-column label="品牌状态" align="left" min-width="100">
        <template slot-scope="scope">
          <el-switch
            v-if="$accessCheck($Access.BrandManageUpdateBrandStatus)"
            v-model="scope.row.enableStatus"
            class="switchStyle"
            active-color="#36B365"
            :active-value="5"
            :inactive-value="4"
            active-text="启用"
            inactive-text="禁用"
            @change="updateBrand($event, scope.row)"
          ></el-switch>
          <div v-else>
            <span v-if="scope.row.enableStatus === 5" class="success-status">
              启用
            </span>
            <span v-else class="danger-status">禁用</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="sort"
        label="排序"
        align="left"
        min-width="80"
      ></el-table-column>
      <!-- <el-table-column
        prop="supplierTitle"
        label="关联供应商"
        align="left"
        min-width="200"
      >
        <template slot-scope="scope">
          <span v-for="(item, index) in scope.row.supplierTitle" :key="index">
            <span v-if="item">{{ item }}；</span>
          </span>
        </template>
      </el-table-column>-->
      <el-table-column
        width="170"
        label="操作"
        header-align="left"
        align="left"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-if="
              $accessCheck($Access.BrandManageEditBrand) ||
              $accessCheck($Access.BrandManageGetBrandInfoById)
            "
            type="text"
            @click="goDetail(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            v-if="$accessCheck($Access.BrandManageDelBrand)"
            type="text"
            @click="deleteBrand(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-drawer
      class="edit-drawer"
      size="50%"
      :title="drawer_tit"
      :visible.sync="is_drawer"
      direction="rtl"
    >
      <AddBrand
        v-if="is_drawer"
        :brand-id="edit_brand_id"
        @submitBrand="submitBrand"
      />
    </el-drawer>
    <FooterPage
      :page-size="pageSize"
      :total-page.sync="total"
      :current-page.sync="page"
      @pageChange="pageChange"
      @sizeChange="sizeChange"
    ></FooterPage>
  </Container>
</template>

<script>
  import FooterPage from "@/component/common/FooterPage";
  import AddBrand from "./AddBrand";
  import {
    getAllBrand,
    updateBrandStatus,
    delBrand,
    updateCategoryStatus,
  } from "@/api/goods";
  export default {
    name: "BrandManage",
    components: {
      FooterPage,
      AddBrand,
    },
    data() {
      return {
        edit_brand_id: 0,
        drawer_tit: "",
        is_drawer: false,
        total: 0,
        page: 1,
        pageSize: 10,
        searchConditionParams: [
          {
            key: "brand",
            operate: "like",
            value: "",
            link: "or",
          },
          {
            key: "brandNo",
            operate: "like",
            value: "",
            link: "or",
          },
        ],
        keyword: "",
        brand_list: [],
      };
    },
    created() {
      this.getList();
    },
    activated() {
      if (this.$_isInit()) return;
      this.getList();
    },
    methods: {
      async getList() {
        const data = await getAllBrand({
          keyword: this.keyword,
          page: this.page,
          pageSize: this.pageSize,
        });
        this.brand_list = data.data.map((item) => {
          return {
            ...item,
            goodsBasicTotal: item.goodsBasicTotal || 0,
          };
        });
        this.total = data.pageTotal;
      },
      // 切页
      pageChange(val) {
        this.page = val;
        this.getList();
      },
      // 每页数据大小改变
      sizeChange(val) {
        this.pageSize = val;
        this.pageChange(1);
      },
      searchData() {
        this.pageChange(1);
      },
      async updateBrand(val, row) {
        try {
          const data = await updateBrandStatus(row.id, {
            enableStatus: val,
          });
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        } catch (e) {
          this.getList();
        }
      },
      addBrand() {
        this.is_drawer = true;
        this.drawer_tit = "";
        this.edit_brand_id = "";
      },
      submitBrand() {
        this.is_drawer = false;
        this.getList();
      },
      goDetail(row) {
        this.is_drawer = true;
        this.drawer_tit = row.title;
        this.edit_brand_id = row.id;
      },
      deleteBrand(id) {
        this.$confirm("确定要删除该品牌吗?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(async () => {
          const data = await delBrand(id);

          this.$message({
            type: "success",
            message: "删除成功!",
          });
          this.getList();
        });
      },
    },
  };
</script>
<style scoped></style>
