<template>
  <div class="app-container">
    <div class="filter-container">
      <span>授权机构:</span>
      <el-input v-model="listQuery.title" placeholder="授权机构" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <span>商品SN码:</span>
      <el-input v-model="listQuery.snCode" placeholder="商品SN码" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button type="primary" @click="handleFilter">查询</el-button>
      <el-button v-if="userIdentity === 2 || proxyInfo !== null" type="primary" @click="$_navigationTo(`add?id=${listQuery.authId}`)">添加商品</el-button>
    </div>
    <!-- 表格区域 -->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      header-row-class-name="tableHeader"
    >
      <el-table-column label="序号" type="index" align="center" width="80" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="商品SN码" align="center" prop="snCode" />
      <el-table-column v-if="userIdentity===2 || proxyInfo !== null" label="上线状态" width="140px" align="center">
        <template slot-scope="{row}">
          <template v-if="row.status === 0">
            <span style="margin-right:10px;" class="status danger">已下线</span>
            <el-button type="primary" size="mini" @click="handleChangeStatus(row)">上线</el-button>
          </template>
          <template v-else>
            <span style="margin-right:10px;" class="status success ">已上线</span>
            <el-button type="info" size="mini" plain @click="handleChangeStatus(row)">下线</el-button>
          </template>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" class-name="status-col" width="300px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.createTime | formatTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" width="180px" align="center" prop="createBy" />
      <el-table-column label="操作" align="center" width="240px" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <template v-if="userIdentity === 2|| proxyInfo !== null">
            <el-button type="default" size="mini" @click="$_navigationTo(`edit?id=${row.productId}`)">
              编辑
            </el-button>
            <el-button type="danger" size="mini" @click="handleRemoveProduct(row)">
              删除
            </el-button>
          </template>
          <el-button type="primary" size="mini" @click="handleShowQRcode(row)">
            二维码
          </el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 二维码 -->
    <qrcode v-if="showQRcode" :is-visible="showQRcode" :product-info="productInfo" @close="showQRcode = false" />
  </div>
</template>

<script>
import { getProdList, setProductStatus, removeProduct } from '@/api/product'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import Qrcode from '@/components/qrcode'
import { formatDate } from '@/utils'
import { mapGetters } from 'vuex'
export default {
  name: 'ComplexTable',
  components: { Pagination, Qrcode },
  filters: {
    formatTime(time) {
      if (!time) {
        return ''
      }
      return formatDate(time, 'yyyy-MM-DD HH:mm:ss')
    }
  },

  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        authId: '',
        productName: '',
        snCode: '',
        pageNum: 1,
        pageSize: 10
      },
      showQRcode: false,
      productInfo: {}
    }
  },
  computed: {
    ...mapGetters(['userIdentity', 'proxyInfo'])
  },
  created() {
    this.listQuery.authId = this.$route.query.id
    this.getList()
  },

  methods: {
    // 添加
    handleAddPro() {
      this.$router.push('add')
    },
    // 修改
    handleEditPro() {
      this.$router.push('edit')
    },
    // 获取列表信息
    getList() {
      getProdList(this.listQuery).then(res => {
        this.total = res.data.total
        this.list = res.data.list
      }).finally(() => {
        this.listLoading = false
      })
    },
    // 过滤列表
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 改变启用状态
    handleChangeStatus(item) {
      if (this.userIdentity === 2 || this.proxyInfo !== null) {
        this.listLoading = true
        const { status,	productId } = item
        const newStatus = status === 0 ? 1 : 0
        setProductStatus({ status: newStatus, productId }).then(res => {
          // this.$message.success(res.data)
          this.$message({
            message: '操作成功',
            duration: 500,
            type: 'success'
          })
          this.listLoading = false
        }).finally(() => {
          this.getList()
        })
      }
    },
    // 删除商品
    async handleRemoveProduct(item) {
      const text = await this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(() => {
        this.$message.info('已取消操作')
      })
      if (text !== 'confirm') return

      removeProduct({ productId: item.productId }).then(res => {
        const h = this.$createElement
        this.$notify.success({
          title: '删除商品',
          message: h('i', { style: 'color: #333' }, `已删除商品："${item.productName}"`),
          duration: 2000
        })
      }).finally(() => {
        this.getList()
      })
    },
    // 显示二维码
    handleShowQRcode(item) {
      this.productInfo = item
      this.showQRcode = true
    }
  }
}
</script>

<style lang="scss" scoped>
.filter-container{
  span{
    display: inline-block;
    margin-bottom: 10px;
    vertical-align: middle;
    font-size: 14px;
  }
  .el-button{
    display: inline-block;
    margin-bottom: 10px;
    vertical-align: middle;
  }
  .el-input,.el-select{
    margin-right: 10px;
    margin-left: 10px;
  }
}
</style>
