<template>
  <div class="mod-prod">
    <avue-crud
      ref="crud"
      :page="page"
      :data="dataList"
      :table-loading="dataListLoading"
      :option="tableOption"
      @search-change="searchChange"
      @on-load="getDataList"
      @refresh-change="refreshChange"
    >
      <!-- 0:商家下架 1:正常 2:平台下架 3:待平台审核 4:审核失败 -1表示删除 -->
      <template slot-scope="scope" slot="status">
        <el-tag v-if="scope.row.status === 1" type="success" size="small">上架</el-tag>
        <el-tag v-else-if="scope.row.status === 0" size="small">商家下架</el-tag>
        <el-tag v-else-if="scope.row.status === 2" size="small" type="danger">违规商品</el-tag>
        <el-tag v-else-if="scope.row.status === 3" type="warning" size="small">待审核</el-tag>
        <el-tag v-else-if="scope.row.status === 4" type="danger" size="small">拒绝通过</el-tag>
        <el-tag v-else size="small">其他</el-tag>
      </template>
      <template slot-scope="scope" slot="menuLeft">
        <el-button
          class="operation-btn"
          @click="getExportExcel()"
          size="medium"
          type="primary"
          icon="el-icon-document-checked"
        >导出</el-button>
        <el-button icon="el-icon-bottom" size="medium" type="primary" @click.stop="getUpload()">导入商品</el-button>
      </template>
      <template slot-scope="scope" slot="menu">
        <el-button
          type="primary"
          icon="el-icon-edit"
          size="small"
          class="operate-btn"
          @click="addOrUpdateHandle(scope.row.prodId)"
        >修改</el-button>
        <el-button
          type="primary"
          icon="el-icon-top"
          size="small"
          class="operate-btn"
          v-if="scope.row.status === 0"
          @click="changeProdStatus(scope.row.prodId, 1)"
        >上架</el-button>
        <el-button
          type="primary"
          icon="el-icon-bottom"
          size="small"
          class="operate-btn"
          v-if="scope.row.status === 1"
          @click="changeProdStatus(scope.row.prodId, 0)"
        >下架</el-button>
        <el-button
          type="primary"
          size="small"
          icon="el-icon-document-checked"
          v-if="scope.row.status > 1"
          @click="applyAuditHandle(scope.row.prodId)"
        >下线管理</el-button>
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          class="operate-btn"
          @click="deleteHandle(scope.row.prodId)"
        >删除</el-button>
      </template>
    </avue-crud>
    <!-- 下线管理弹窗-->
    <ProdOfflineHandleEvent
      v-if="prodOfflineHandleEventVisible"
      selectUrl="/prod/prod/getOfflineHandleEventByProdId"
      applyUrl="/prod/prod/auditApply"
      ref="prodOfflineHandleEvent"
      @refreshDataList="refreshChange"
    ></ProdOfflineHandleEvent>
    <prod-upload v-if="uploadVisible" ref="prodUpload" @inputCallback="getWaitingConsignmentExcel"></prod-upload>
  </div>
</template>

<script>
import { tableOption } from '@/crud/prod/prodList'
import ProdUpload from './prod-upload'
import ProdOfflineHandleEvent from '@/components/offline-event-handle'
export default {
  data () {
    return {
      dataForm: {
        prodName: ''
      },
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {},
      dataListSelections: [],
      dataListLoading: false,
      uploadVisible: false,
      prodOfflineHandleEventVisible: false,
      tableOption: tableOption,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl
    }
  },
  components: {
    ProdOfflineHandleEvent,
    ProdUpload
  },
  methods: {
    // 获取数据列表
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/prod/prod/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.records
        for (const key in this.dataList) {
          if (this.dataList.hasOwnProperty(key)) {
            const element = this.dataList[key]
            if (element.imgs != null && element.imgs !== '') {
              element.imgs = element.imgs.split(',')[0]
            }
          }
        }
        this.page.total = data.total
        this.page.current = data.current
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.$router.push({
        path: '/prodInfo',
        query: { prodId: id }
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(`确定进行删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl(`/prod/prod/${id}`),
            method: 'delete',
            data: this.$http.adornData({})
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        })
        .catch(() => { })
    },
    changeProdStatus (id, status) {
      this.$confirm(`确定进行[${status ? '上架' : '下架'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl(`/prod/prod/prodStatus`),
            method: 'put',
            data: this.$http.adornData({
              status: status,
              prodId: id
            })
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList(this.page)
              }
            })
          })
        })
    },
    // 条件查询
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 跳转至导入选择
    getUpload () {
      this.uploadVisible = true
      this.$nextTick(() => {
        this.$refs.prodUpload.init()
      })
    },
    getExportExcel () {
      this.$confirm(`导出报表会根据商品上下架条件导出`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/prod/prod/exportProd'),
          method: 'get',
          params: this.$http.adornParams(this.searchForm),
          responseType: 'blob' // 解决文件下载乱码问题
        }).then(({ data }) => {
          var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
          const fileName = '商品信息整理.xls'
          const elink = document.createElement('a')
          if ('download' in elink) { // 非IE下载
            elink.download = fileName
            elink.style.display = 'none'
            elink.href = URL.createObjectURL(blob)
            document.body.appendChild(elink)
            elink.click()
            URL.revokeObjectURL(elink.href) // 释放URL 对象
            document.body.removeChild(elink)
          } else { // IE10+下载
            navigator.msSaveBlob(blob, fileName)
          }
        })
      })
    },
    // 申请审核
    applyAuditHandle (id) {
      this.prodOfflineHandleEventVisible = true
      this.$nextTick(() => {
        this.$refs.prodOfflineHandleEvent.init(id)
      })
    },
    // 回调刷新
    refreshChange () {
      this.getDataList()
    }
  }
}
</script>

<style >
.operate-btn {
  margin-left: 5px !important;
}
.operate-btn:first-child {
  margin-left: 0 !important;
}
</style>
