<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="标签名称" prop="tagName">
        <el-input
          v-model="queryParams.tagName"
          placeholder="请输入标签名称"
          clearable
          size="small"
          maxlength="4"
          show-word-limit
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['store:storeProductTag:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="序号" type="index" width="55" align="center" />
      <el-table-column label="标签名称" align="center" prop="tagName" />
      <el-table-column label="关联商品数量" align="center" width="120">
        <template slot-scope="scope">
          <el-button type="text" @click="showProducts(scope.row)">{{ scope.row.productCount || 0 }}个</el-button>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="80">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 1">显示</el-tag>
          <el-tag v-else type="info">隐藏</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" width="80" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['store:storeProductTag:edit']">编辑</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['store:storeProductTag:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 添加/编辑标签 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="标签名称" prop="tagName">
          <el-input v-model="form.tagName" placeholder="最多4个字" maxlength="4" show-word-limit />
        </el-form-item>
        <el-form-item label="标签状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio :label="1">显示</el-radio>
            <el-radio :label="0">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="form.sort" :min="0" placeholder="排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 关联商品弹窗 -->
    <el-dialog title="关联商品" :visible.sync="productsOpen" width="800px" append-to-body>
      <div class="mb8">关联商品数量: {{ productsTotal }}个</div>
      <el-table v-loading="productsLoading" :data="productsList" border>
        <el-table-column label="商品ID" align="center" prop="productId" width="80" />
        <el-table-column label="商品名称" align="center" prop="productName" min-width="120" show-overflow-tooltip />
        <el-table-column label="主图" align="center" width="80">
          <template slot-scope="scope">
            <el-image v-if="scope.row.image" style="width:50px;height:50px" :src="scope.row.image" fit="contain" />
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="售价" align="center" width="100">
          <template slot-scope="scope">
            <span v-if="scope.row.price != null">¥{{ scope.row.price.toFixed(2) }}</span>
            <span v-else>-</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="80">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isShow === 1" type="success">上架</el-tag>
            <el-tag v-else type="info">未上架</el-tag>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="productsTotal>0" :total="productsTotal" :page.sync="productsQuery.pageNum" :limit.sync="productsQuery.pageSize" @pagination="loadProducts" />
    </el-dialog>
  </div>
</template>

<script>
import { listStoreProductTag, getStoreProductTag, addStoreProductTag, updateStoreProductTag, delStoreProductTag, listTagProducts } from '@/api/hisStore/storeProductTag'

export default {
  name: 'StoreProductTag',
  data() {
    return {
      loading: true,
      showSearch: true,
      list: [],
      total: 0,
      title: '',
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tagName: null,
        status: null
      },
      form: {},
      rules: {
        tagName: [{ required: true, message: '标签名称不能为空', trigger: 'blur' }, { max: 4, message: '最多4个字', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }]
      },
      productsOpen: false,
      productsLoading: false,
      productsList: [],
      productsTotal: 0,
      productsQuery: { pageNum: 1, pageSize: 10 },
      currentTagId: null
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listStoreProductTag(this.queryParams).then(response => {
        this.list = response.rows || []
        this.total = response.total || 0
        this.loading = false
      })
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = { id: null, storeId: null, tagName: null, status: 1, sort: 0 }
      this.resetForm('form')
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加商品标签'
    },
    handleUpdate(row) {
      this.reset()
      getStoreProductTag(row.id).then(response => {
        this.form = { ...response.data }
        this.open = true
        this.title = '编辑商品标签'
      })
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) return
        if (this.form.id != null) {
          updateStoreProductTag(this.form).then(() => {
            this.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        } else {
          addStoreProductTag(this.form).then(() => {
            this.msgSuccess('新增成功')
            this.open = false
            this.getList()
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否确认删除该商品标签?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return delStoreProductTag(row.id)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      }).catch(() => {})
    },
    showProducts(row) {
      this.currentTagId = row.id
      this.productsQuery = { pageNum: 1, pageSize: 10 }
      this.productsOpen = true
      this.loadProducts()
    },
    loadProducts() {
      if (!this.currentTagId) return
      this.productsLoading = true
      listTagProducts(this.currentTagId, this.productsQuery).then(response => {
        this.productsList = response.rows || []
        this.productsTotal = response.total || 0
        this.productsLoading = false
      })
    }
  }
}
</script>
