<template>
  <div class="page-body">
    <div class="head-box">
      <div class="head-box-top">
        <div class="top-call" />
        <div class="top-call"><img src="@/assets/home_images/cisnet_logo_b.png"></div>
        <div class="top-call"><title-menus ref="titleMenus" menu-index="99" style="padding-top: 15px" /></div>
        <div class="top-call"><user-login ref="userLogin" /></div>
        <div class="top-call"><language-setting ref="languageSetting" /></div>
      </div>
    </div>
    <div class="content-box">
      <div style="width: 88%;padding-left: 9%;">
        <el-row :gutter="20" style="padding-top: 10px">
          <el-col :span="3">
            <user-menus menu-index="1" />
          </el-col>
          <el-col :span="21" style="padding: 0 20px">
            <!--工具栏-->
            <div class="head-container">
              <el-input v-model="query.productId" clearable placeholder="请输入产品ID" style="width:150px;" />
              <el-input v-model="query.productName" clearable placeholder="请输入产品名称" style="width:280px;" />
              <el-select v-model="query.productType" clearable placeholder="请选择分类" style="width: 150px">
                <el-option v-for="item in dict.product_type" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
              <el-select v-model="query.status" clearable placeholder="请选择状态" style="width: 150px">
                <el-option v-for="item in dict.product_status" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
              <date-range-picker v-model="query.updateTime" start-placeholder="最后维护开始日期" style="width: 320px" />
              <el-button type="primary" icon="el-icon-search" style="opacity: 0.9" @click="toSearch">查询</el-button>
              <el-button icon="el-icon-refresh" @click="clearLimit">重置</el-button>
            </div>
            <div class="toolbar">
              <div>
                <el-button type="primary" icon="el-icon-circle-plus-outline" style="opacity: 0.9" @click="toAdd">新增产品</el-button>
              </div>
              <div style="text-align: right">
                <el-button icon="el-icon-finished" type="primary" plain :disabled="!multipleSelection.length" @click="batchOperate(1, null)">批量发布</el-button>
                <el-button icon="el-icon-download" type="primary" plain :disabled="!multipleSelection.length" @click="batchOperate(0, null)">批量下架</el-button>
              </div>
            </div>
            <!-- 表格 -->
            <div class="content">
              <el-table id="dataTable" ref="dataTable" v-loading="loading" :data="tableData" border height="58vh" :header-cell-style="{background:'#F8F8F8',color:'#333'}" tooltip-effect="dark" @selection-change="handleSelectionChange">
                <el-table-column type="selection" align="center" width="55" />
                <el-table-column label="产品预览图" align="center" width="100">
                  <template slot-scope="scope">
                    <el-image
                      :src="scope.row.imgSrc?imgSrcStart+'/productPic/'+scope.row.imgSrc:defaultImgProduct"
                      :preview-src-list="scope.row.imgSrcList?scope.row.imgSrcList:[defaultImgProduct]"
                      style="width:64px;height: 64px;border: 1px solid rgba(0, 0, 0, 0.2);"
                    >
                      <div slot="placeholder" class="image-slot">加载中<span class="dot">...</span></div>
                    </el-image>
                  </template>
                </el-table-column>
                <el-table-column prop="productId" label="产品ID" align="center" width="100" />
                <el-table-column prop="productType" label="产品分类" align="center" width="140" :formatter="(row, col, val)=>{return dict.label.product_type[val]}" />
                <el-table-column prop="productName" label="产品名称" align="center" />
                <el-table-column label="状态" align="center" width="100">
                  <template slot-scope="scope">
                    <div :style="'color:'+textColors[scope.row.status]">{{ dict.label.product_status[scope.row.status] }}</div>
                  </template>
                </el-table-column>
                <el-table-column prop="updateTime" label="最后维护日期" align="center" width="180" />
                <el-table-column label="操作" align="center" width="200">
                  <template slot-scope="scope">
                    <div v-if="scope.row.status==='4'" class="table-btn touch" type="text" style="color:#32cd32" @click="batchOperate(1, scope.row)">上架</div>
                    <div v-if="scope.row.status==='5'" class="table-btn touch" type="text" style="color:#999" @click="batchOperate(0, scope.row)">下架</div>
                    <div v-if="scope.row.status==='2'" class="table-btn touch" type="text" style="color:#f00" @click="toView(scope.row)">查看原因</div>
                    <div class="table-btn touch" type="text" @click="toEdit(scope.row)">编辑</div>
                  </template>
                </el-table-column>
              </el-table>
              <!--分页组件-->
              <el-pagination :total="total" :current-page="page" :page-size="pageSize" style="margin-top: 8px;" layout="total, prev, pager, next, sizes" @size-change="sizeChange" @current-change="pageChange" />
            </div>
          </el-col>
        </el-row>
      </div>
    </div>
    <home-footer ref="homeFooter" />
    <inquiry-view ref="inquiryView" />
    <el-backtop />
    <add-page ref="addPage" />
    <edit-page ref="editPage" />
    <view-page ref="viewPage" />
  </div>
</template>
<script>
import default_product from '@/assets/home_images/default_product.png'

import titleMenus from '../components/titleMenusV2'
import userLogin from '../components/userLoginV2'
import languageSetting from '../components/languageSettingV2'
import homeFooter from '../components/homeFooter'
import inquiryView from '../components/inquiryView'
import userMenus from '../components/userMenus'
import DateRangePicker from '@/components/DateRangePicker'
import { HttpReq } from '@/api/common'
import addPage from './add'
import editPage from './edit'
import viewPage from './view'

export default {
  components: { titleMenus, userLogin, languageSetting, homeFooter, inquiryView, userMenus, DateRangePicker, addPage, editPage, viewPage },
  dicts: ['product_type', 'product_status'],
  data() {
    return {
      defaultImgProduct: default_product,
      imgSrcStart: process.env.VUE_APP_BASE_API,
      textColors: ['#ccc', '#00008b', '#f00', '#F98052',
        '#000', '#32cd32', '#bba'],
      loading: false,
      page: 1,
      pageSize: 20,
      total: 0,
      query: {},
      tableData: [],
      multipleSelection: [],
      throttle: null, // 节流器
      throttleTime: 300
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.loadData()
    })
  },
  methods: {
    indexMethod(index) {
      return 1 + index + this.page * this.pageSize - this.pageSize
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    loadData() {
      // 清除参数无值的情况
      Object.keys(this.query).length !== 0 && Object.keys(this.query).forEach(item => {
        if (this.query[item] === null || this.query[item] === '') this.query[item] = undefined
      })
      HttpReq.webClientApi.productManagement({
        page: this.page - 1,
        pageSize: this.pageSize,
        ...this.query
      }).then((res) => {
        this.tableData = res.data.data
        this.total = res.data.total
      })
    },
    batchOperate(type, row) {
      if (!row && !this.multipleSelection.length) {
        return this.$message({
          message: '未选取数据',
          type: 'info'
        })
      }
      HttpReq.backstageApi.batchReleaseAndRecovery({
        ids: row ? [row.productId] : this.multipleSelection.map(item => { return item.productId }),
        type: type
      }).then((res) => {
        this.$notify({
          title: res.msg,
          type: res.code === 200 ? 'success' : 'error',
          duration: 2500
        })
        this.loadData()
      })
    },
    toAdd() {
      this.$refs.addPage.loadData()
    },
    toEdit(item) {
      this.$refs.editPage.loadData(item.productId)
    },
    toView(item) {
      this.$refs.viewPage.loadData(item.productId)
    },
    toSearch() {
      this.page = 1
      this.loadData()
    },
    clearLimit() {
      this.query = {}
      this.loadData()
    },
    pageChange(e) {
      this.page = e
      this.loadData()
    },
    sizeChange(e) {
      this.page = 1
      this.pageSize = e
      this.loadData()
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.touch {
  cursor:pointer;
}
.clear {
  clear: both
}
.inline-block {
  display: inline-block;
}
.ellipsis {
  // 多行溢出省略号
  display: -webkit-box;
  -webkit-box-orient: vertical;
  overflow: hidden;
  -webkit-line-clamp: 2;
  text-overflow: ellipsis;
}
.pagination {
  margin-top: 23px;
  text-align: center;
  padding: 10px;
  display: flex;
  justify-content: center;
  align-items: center;
  >>>.el-pagination.is-background .el-pager li {
    font-style: normal;
    font-weight: 400;
    font-size: 14px;
    text-align: center;
    background-color: #fff;
    color: #000000;
    border: 1px solid #e5e5ea;
  }
  /* 激活后的样式 */
  >>>.el-pagination.is-background .el-pager li:not(.disabled).active {
    background-color: #ffffff;
    color: #0366ed;
    border: 1px solid #0366ed;
    text-align: center;
  }
  /* 修改左右箭头样式 */
  >>>.el-pagination .btn-next .el-icon, >>>.el-pagination .btn-prev .el-icon {
    font-style: normal;
    font-weight: 400;
    font-size: 14px;
    text-align: center;
    background-color: #fff;
    color: #e5e5ea;
    border: 1px solid #e5e5ea;
  }
  >>>.el-pager {
    height: 35.5px !important;
  }
  >>>.number, >>>.el-icon {
    height: 35.5px !important;
    line-height: 35.5px !important;
  }
}
.head-box {
  width: 100vw;
  top: 0;
  position: fixed;
  background: none repeat scroll 0 0 white;
  z-index: 999;
  box-shadow: 0px 3px 7px 0px rgba(0,0,0,0.2);
  .head-box-top {
    display: flex;
    justify-content: space-evenly;
    align-items: center;
    .top-call {
      height: 80px;
      padding-top: 15px;
    }
  }
}
.content-box {
  min-height: 70vh;
  margin-top: 94px;
  font-family: Source Han Sans CN;
  user-select: none;
  >>>.el-table--mini {
    font-size: 14px;
  }
  >>>.el-button--mini {
    font-size: 14px;
  }
}
.toolbar {
  display: flex;
  justify-content: space-between;
}
.content {
  margin-top: 10px;
  .table-btn {
    display: inline-block;
    margin: 0 10px;
    color: #1791E7;
    opacity: 0.8;
  }
  .table-btn:hover {
    opacity: 1;
  }
}
</style>
