<template>
  <div>
    <div class="pending">
      <!-- 搜索 -->
      <div class="formSearch">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="商品名称">
            <el-input v-model="formInline.search" maxlength="20" placeholder="请输入商品名称" />
          </el-form-item>
          <el-form-item label="商品型号">
            <el-input v-model="formInline.productMarque" placeholder="请输入商品型号" />
          </el-form-item>
          <!-- <el-form-item label="上架状态">
            <el-select v-model="formInline.shelveState" placeholder="请选择上架状态">
              <el-option label="全部" :value="null" />
              <el-option label="上架" value="1" />
              <el-option label="下架" value="0" />
            </el-select>
          </el-form-item> -->
          <el-form-item label="官方分类">
            <el-cascader v-model="formInline.classifyId" :options="categoryList" clearable :props="{
          checkStrictly: true,
          expandTrigger: 'hover',
          label: 'categoryName',
          value: 'id',
          children: 'childs'
        }" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain @click="handleSearch">查询</el-button>
            <el-button plain @click="clear">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <!-- 表格 -->
      <div class="tableBox">
        <el-table ref="multipleTable" :data="tableData" border
          :header-cell-style="{ background: '#EEF3FF', color: '#333333' }" tooltip-effect="dark" style="width: 100%"
          v-loading="loading">
          <el-table-column prop="productId" label="商品id" show-overflow-tooltip />
          <el-table-column label="商品主图" width="150" align="center">
            <template slot-scope="scope">
              <!-- 修复图片不能正常显示的问题 -->
              <img height="80" width="80" :src="scope.row.productImage.replace(/http:\/\//, 'https://')" alt srcset>
            </template>
          </el-table-column>
          <el-table-column prop="productName" label="商品名称" width="220" />
          <el-table-column prop="section" label="售价区间" show-overflow-tooltip />
          <el-table-column prop="memberSection" label="会员价" show-overflow-tooltip />
          <el-table-column prop="ifCredit" label="积分兑换" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.ifCredit == 1">允许</span>
              <span v-if="scope.row.ifCredit == 0">不允许</span>
            </template>
          </el-table-column>
          <el-table-column prop="stockNumber" label="库存" show-overflow-tooltip />
          <el-table-column prop="volume" label="销量" show-overflow-tooltip />
          <el-table-column prop="volume" label="上架状态" show-overflow-tooltip>
            <template slot-scope="scope">
              <span v-if="scope.row.shelveState == 0">未上架</span>
              <span v-if="scope.row.shelveState == 1">已上架</span>
              <span v-if="scope.row.shelveState == 2">待审核</span>
              <span v-if="scope.row.shelveState == 3">审核失败</span>
            </template>
          </el-table-column>
          <el-table-column prop="reject" label="驳回原因" show-overflow-tooltip />
          <el-table-column :formatter="row => getBrandName(row)" label="品牌" show-overflow-tooltip />
          <el-table-column prop="createTime" label="创建时间" width="180" />
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <div class="btnList">
                <el-button type="text" @click="edit(scope.row)">选择商品</el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <div class="fenye">
          <el-pagination :current-page="formInline.page" :page-sizes="[10, 20, 50, 100]" :page-size="10"
            layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
    <!-- 新增/修改商品弹窗 -->
    <el-dialog :title="!commId ? '新增商品' : '编辑商品'" :visible.sync="commidyVisible" width="74%" center top="10vh"
      :close-on-click-modal="false" @before-close="closeModal" @close="closeModal">
      <CommAdd  ref="child"  :voucher-list="voucherList" :show-tinymce="showTinymce" :product-id="commId"
        @cancel="cancelForm" />
    </el-dialog>
  </div>
</template>

<script>
import CommAdd from '@/views/threeSelection/platformSelect/addCommodity.vue'
import { getBtnList, getToken, getShopId } from '@/utils/auth'
import { uploadUrl } from '@/utils/request'
import {
  getClassify,
  getBrandList,
} from '@/api/commodity'

import {
  getlLibraryList,
} from '@/api/threeSelection'
// import { shopSelectionAddShopLibrary } from '@/api/selectionCenter/selectionCenter'
export default {
  name: 'CommoditySystem',
  components: {
    CommAdd
  },
  data() {
    // 这里存放数据
    return {
      showTinymce: false,
      btnList: '',
      activeName: 'first',
      formInline: {
        productMarque: '', // 商品型号
        search: '', // 搜索字段
        shelveState: 1, // 上架状态 1-上架 0-不上架 null-全部
        stock: '', // 库存状态 1-有库存 0-无库存 null-全部
        classifyId: '', // 最下级分类id
        page: 1, // 当前页
        pageSize: 10
      },
      batchAdd: false,
      batchFileList: [],
      total: 1,
      tableData: [],
      categoryList: [],
      UploadUrl: uploadUrl,
      headers: {
        'Authorization-business': getToken()
      },
      vipPriceVisible: false,
      productId: 0,
      mode: 1,
      productData: [],
      gradeList: [],
      commidyVisible: false,
      commId: 0,
      brandList: [],
      voucherList: [],
      //  控制加载
      loading: false
    }
  },
  computed: {
    getBrandName() {
      return (row) => {
        const find = this.brandList.find((item) => item.id === row.brandId)
        if (find) return find.brandName
        return '-'
      }
    }
  },
  created() {
  },
  mounted() {
    this.getBrandList()
    this.getAll(this.formInline)
    this.queryAllCategory()
    this.btnList = getBtnList()
  },
  // 方法集合
  methods: {
    async getBrandList() {
      const { data } = await getBrandList()
      this.brandList = data
    },
    //  编辑
    edit(row) {
      this.showTinymce = true
      this.commidyVisible = true
      this.commId = row.productId
      this.$nextTick(() => {
        this.$refs.child?.details()
      })
    },
    cancelForm() {
      this.commidyVisible = false
      this.getAll(this.formInline)
      this.closeModal()
    },
    closeModal() {
      this.showTinymce = false
      this.$refs.child.last()
      this.$refs.child.reset()
    },
    handleSizeChange(val) {
      this.formInline.pageSize = val
      this.getAll(this.formInline)
    },
    handleCurrentChange(val) {
      this.formInline.page = val
      this.getAll(this.formInline)
    },
    //  查询
    handleSearch() {
      this.total = 1
      this.formInline.page = 1
      this.getAll(this.formInline)
    },
    // 重置
    clear() {
      this.formInline = {
        search: '', // 搜索字段
        shelveState: '', // 上架状态 1-上架 0-不上架 null-全部
        stock: '', // 库存状态 1-有库存 0-无库存 null-全部
        classifyId: '', // 最下级分类id
        page: 1, // 当前页
        pageSize: 10
      }
    },
    // 初始化查询所有数据
    async getAll(formInline) {
      this.formInline.classifyId =
        this.formInline.classifyId[2] ||
        this.formInline.classifyId[1] ||
        this.formInline.classifyId[0] ||
        this.formInline.classifyId
      this.loading = true
      try {
        const res = await getlLibraryList(formInline)
        this.total = res.data.total
        this.tableData = res.data.list
      } finally {
        this.loading = false
      }

    },
    // 初始化查询所有分类
    async queryAllCategory() {
      const res = await getClassify()
      this.categoryList = res.data
      this.handleRemoveEmptyChild(this.categoryList)
    },
    handleRemoveEmptyChild(arr) {
      for (let i = 0; i < arr.length; i++) {
        if (arr[i].childs && Array.isArray(arr[i].childs)) {
          this.handleRemoveEmptyChild(arr[i].childs)
        }
        if (arr[i].childs && Array.isArray(arr[i].childs) && arr[i].childs.length === 0) {
          delete arr[i].childs
        }
      }
    },
  }
}
</script>

<style lang='scss' scoped>
//@import url(); 引入公共css类
@import url("../../../styles/elDialog.scss");

.pending {
  padding: 30px;
}

.fenye {
  margin-top: 20px;
}

.clickMe {
  color: #3a68f2;
  cursor: pointer;
}

.uploadDialog {

  // display: flex;
  // flex-direction: column;
  // justify-content: center;
  >>>.el-upload__tip {
    display: inline-block;
    width: 360px;
    margin: auto;
  }
}

.vipDialog {
  .priceTable {
    table {
      width: 100%;
      text-align: center;
      border-left: 1px solid #EBEEF5;
      border-bottom: 1px solid #EBEEF5;
      font-size: 14px;
      color: #606266;
      border-collapse: collapse;

      tr {
        border-top: 1px solid #EBEEF5;

        th {
          padding: 12px 0;
          background: #EEF3FF;
          color: #333;
          border-right: 1px solid #EBEEF5;
        }

        td {
          padding: 12px 0;
          border-right: 1px solid #EBEEF5;

          &.td-input {
            .el-input {
              width: 80px;
              margin-right: 0;
            }
          }

          &:nth-child(1),
          &:nth-child(2) {
            width: 80px;
          }

          .el-input {
            width: 100px;
            margin-right: 10px;
          }
        }
      }
    }
  }

  .clearBtn {
    margin-top: 20px;
  }
}
</style>
