<template>
  <div>
    <a-card size="small" :bordered="false" class="productBrandList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="productBrandList-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="品牌名称">
                <a-input id="productBrandList-brandName" v-model.trim="queryParam.brandName" allowClear placeholder="请输入品牌名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="品牌分类">
                <v-select code="BRAND_TYPE" id="productBrandList-brandType" v-model="queryParam.brandType" allowClear placeholder="请选择品牌分类"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="排序字母">
                <a-select placeholder="请选择排序字母" id="productBrandList-sortChar" allowClear v-model="queryParam.sortChar">
                  <a-select-option v-for="item in sortCharList" :key="item.id" :value="item.id">{{ item.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="productBrandList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="productBrandList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 操作按钮 -->
        <div class="table-operator" v-if="$hasPermissions('B_productBrand_add')" >
          <a-button id="productBrandList-add" type="primary" @click="handleEdit()">新增</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+70+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :pageSize="30"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 产品图片 -->
          <template slot="imageUrl" slot-scope="text, record">
            <img
              :id="'productBrandList-imageUrl'+record.id"
              :src="record.imageUrl+'?x-oss-process=image/resize,h_30,m_lfit' || ''"
              width="30"
              height="30"
              class="imageUrl"
              @click="lookPic(record.imageUrl)" />
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_productBrand_edit')"
              class="button-info"
              @click="handleEdit(record)"
              :id="'productBrandList-edit-btn'+record.id">编辑</a-button>
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_productBrand_detail')"
              class="button-success"
              @click="handleDetail(record)"
              :id="'productBrandList-detail-btn'+record.id">详情</a-button>
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_productBrand_del')"
              class="button-error"
              @click="handleDel(record)"
              :id="'productBrandList-del-btn'+record.id">删除</a-button>
            <span v-if="!$hasPermissions('B_productBrand_edit') && !$hasPermissions('B_productBrand_detail') && !$hasPermissions('B_productBrand_del')">--</span>
          </template>
        </s-table>
      </a-spin>
      <!-- 新增/编辑产品品牌 -->
      <product-brand-edit-modal
        v-drag
        :openModal="openModal"
        :nowData="nowData"
        :itemSn="itemSn"
        @ok="$refs.table.refresh()"
        @close="closeModal" />
      <!-- 查看产品品牌 -->
      <product-brand-detail-modal v-drag :openModal="openDetailModal" :itemSn="itemSn" @close="closeDetailModal" />
      <!-- 查看图片 -->
      <a-modal
        centered
        v-drag
        class="productBrandList-modal"
        :footer="null"
        :maskClosable="false"
        title="品牌图片"
        v-model="openPicModal"
        @cancel="nowPic=''">
        <img :src="nowPic" width="100%" />
      </a-modal>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import dataList from '@/libs/commonData'
import productBrandEditModal from './editModal.vue'
import productBrandDetailModal from './detailModal.vue'
// 接口
import { productBrandList, productBrandDel } from '@/api/productBrand'
export default {
  name: 'ProductBrandList',
  mixins: [commonMixin],
  components: { STable, VSelect, productBrandEditModal, productBrandDetailModal },
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0, // 表格高度
      //  查询条件
      queryParam: {
        brandName: '', //  品牌名称
        brandType: undefined,
        sortChar: undefined
      },
      sortCharList: dataList.sortCharList, // 排序字母
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '品牌图片', scopedSlots: { customRender: 'imageUrl' }, width: '15%', align: 'center' },
        { title: '品牌名称', dataIndex: 'brandName', align: 'center', width: '30%', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '品牌分类', dataIndex: 'brandTypeDictValue', width: '17%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '排序字母', dataIndex: 'sortChar', width: '13%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        // 获取表格数据 有分页
        return productBrandList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            // 计算表格序号
            const no = (data.pageNo - 1) * data.pageSize
            for (var i = 0; i < data.list.length; i++) {
              data.list[i].no = no + i + 1
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      openModal: false, //  新增编辑  弹框
      openPicModal: false, //  查看图片 弹框
      openDetailModal: false, // 详情  弹框
      itemSn: '', //  当前sn
      nowData: null, //  当前记录数据
      nowPic: '' // 品牌图片
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.brandName = ''
      this.queryParam.brandType = undefined
      this.queryParam.sortChar = undefined
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      this.itemSn = row ? row.brandSn : null
      this.nowData = row || null
      this.openModal = true
    },
    // 详情
    handleDetail (row) {
      this.itemSn = row ? row.brandSn : null
      this.openDetailModal = true
    },
    // 查看图片
    lookPic (url) {
      this.nowPic = url
      this.openPicModal = true
    },
    //  关闭弹框
    closeModal () {
      this.itemSn = ''
      this.nowData = null
      this.openModal = false
    },
    // 关闭详情弹框
    closeDetailModal () {
      this.itemSn = ''
      this.openDetailModal = false
    },
    // 删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          productBrandDel({ sn: row.brandSn }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 240
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  }
}
</script>
