<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"
    >
    <!-- <template #search>
      
      <div class="el-col el-col-24 el-col-xs-24 el-col-md-6" style="padding-left: 10px; padding-right: 10px;">
        <el-form-item label="门店">
          <el-select v-model="searchForm.orgCode" size="small" placeholder="门店">
            <el-option
              v-for="item in orgCodeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>

      </div>
    </template> -->
   <template #images="{ row }">
      <img :src="picUrl(row.images)" alt="产品图片" style="width: 150px;">
    </template>
      <!-- 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-view"
          size="small"
          class="operate-btn"
          @click="addOrUpdateHandle(scope.row)"
        >查看</el-button>
      </template>
    </avue-crud>

    <prod-upload v-if="uploadVisible" ref="prodUpload" @inputCallback="getWaitingConsignmentExcel"></prod-upload>


     <add-or-update v-if="addOrUpdateVisible"
                   ref="addOrUpdate" ></add-or-update>
  </div>
</template>

<script>
import { tableOption } from '@/crud/prod/prodList'
import AddOrUpdate from './prodInfo'

import ProdUpload from './prod-upload'
import ProdOfflineHandleEvent from '@/components/offline-event-handle'
export default {
  data () {
    return {
      addOrUpdateVisible:false,
      dataForm: {
        prodName: ''
      },
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      searchForm: {orgCode:''},
      dataListSelections: [],
      dataListLoading: false,
      uploadVisible: false,
      prodOfflineHandleEventVisible: false,
      tableOption: tableOption,
      resourcesUrl: window.SITE_CONFIG.resourcesUrl,
      orgCodeOptions: [] // 添加orgCode选项数组
    }
  },
  components: {
    ProdOfflineHandleEvent,
    ProdUpload,
    AddOrUpdate

  },
  created() {
    // 在组件创建时获取orgCode数据
    this.getOrgCode()
  },
  methods: {
    // 获取orgCode选项数据
    getOrgCode() {
      this.$http({
        url: this.$http.adornUrl('/shop/shopDetail/headInfo'), 
        method: 'get',
        params: this.$http.adornParams({shopId:localStorage.getItem('shopId')})
      }).then(({ data }) => {
            this.searchForm.orgCode = data.thirdPartyCode
            this.getDataList(this.page)
      })
    },
    picUrl(url) {
      if(!url) return
      if(url.indexOf('http') === -1) {
        return this.resourcesUrl + url.split(',')[0]
      }else {
        return url.split(',')[0]
      }
    },
    // 搜索
    search(page){
       this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/qnh/itemQuery'),
        method: 'post',
        data: this.$http.adornData(
          Object.assign(
            {
              pageNo: page == null ? this.page.currentPage : page.currentPage,
              pageSize: page == null ? this.page.pageSize : page.pageSize
            },
            this.searchForm
          )
        )
      }).then(({ data }) => {
        this.dataList = data.data
        // 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.totalCount
        // this.page.current = data.current
        this.dataListLoading = false
      })
    },
    // 获取数据列表
    getDataList (page) {
      if(this.searchForm.orgCode === '') {
          this.getOrgCode()
      }else{
        this.search()
      }
    },
    // 新增 / 修改
    addOrUpdateHandle (data) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data)
      })
    },
    // 删除
    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 >
.item-width-250 .el-input{
  width: 250px !important;
}
.operate-btn {
  margin-left: 5px !important;
}
.operate-btn:first-child {
  margin-left: 0 !important;
}
</style>
