<!--//88888888888888888-->
<!-- 商城商品管理 -->
<template>
    <div class="execution">
      <basic-container>
          <avue-crud ref="crud"
                       :page.sync="page"
                       :data="tableData"
                       :permission="permissionList"
                       :table-loading="tableLoading"
                       :option="tableOption"
                       @on-load="getList"
                       @selection-change="onSelectionChange"
                       @search-change="searchChange"
                       @refresh-change="refreshChange"
                       @size-change="sizeChange"
                       @current-change="currentChange"
                       @row-update="handleUpdate"
                       @row-save="handleSave"
                       @row-del="handleDel">
              <!--  1.行内按钮-->
              <template slot-scope="scope" slot="menu">
                <el-button  type="button" size="small"
                            @click="lookDetail(scope.row)" style="color: #409EFF;margin: 5px;padding: 0px;border: 0px">详情</el-button>
                <el-button  type="button" size="small"
                            v-if="scope.row.auditState!=1"
                            @click="handleUpdate(scope.row)" style="color: #409EFF;margin: 5px;padding: 0px;border: 0px">编辑</el-button>
                <el-button  type="button" size="small"
                            @click="handleDel(scope.row)" style="color: #409EFF;margin: 5px;padding: 0px;border: 0px">删除</el-button>

              </template>
              <!-- 2.表外按钮 -->
              <template slot-scope="scope" slot="menuLeft">
                <div class="layout_h">
                  <el-button
                    type="primary"
                    size="small"
                    @click="add">添加</el-button>
                  <el-button
                    type="primary"
                    size="small"
                    @click="batchIsUpon(scope.row)">上架</el-button>
                  <el-button
                    type="primary"
                    size="small"
                    @click="batchIsNotUpon">下架</el-button>
                  <el-button
                    type="primary"
                    size="small"
                    @click="handleDelBatch(scope.row)">删除</el-button>
                </div>
              </template>
            </avue-crud>
      </basic-container>
      <goods_edits  :show="goodsEditVue" :id="id" @close="goodsEditVue=false" @success="onChangeDone" />
    </div>
</template>

<script>
  import {fetchList,addObj,delObj,isUpon, isNotUpon,getMerchantByAccount} from '@/api/mall/merchantMallgoods'
  import {tableOption} from '@/const/crud/mall/merchantMallgoods'
  import {mapGetters} from 'vuex'
  import {ZmMixin} from '@/mixins/ZmMixin.js'
  export default {
    name: 'businessGoods',
    mixins: [ZmMixin],

    data() {
          return {
            merchantId:'',
            goodsEditVue:false,
            id:undefined,
            searchForm: {},
            tableData: [],
            page: {
                total: 0, // 总页数
                currentPage: 1, // 当前页数
                pageSize: 20 // 每页显示多少条
            },
            tableLoading: false,
            tableOption: tableOption
          }
      },
      computed: {
          ...mapGetters(['permissions']),
          permissionList() {
              return {
                  addBtn: this.vaildData(this.permissions.admin_mallgoods_add, false),
                  delBtn: this.vaildData(this.permissions.admin_mallgoods_del, false),
                  editBtn: this.vaildData(this.permissions.admin_mallgoods_edit, false)
              };
          }
      },
      components: {
      'goods_edits': () => import('./goodsEdit/goods_edit_pop'),
      },
      methods: {
        // 更新
        handleUpdate: function (row, index, done,loading) {
          this.id=row.id
          this.goodsEditVue=true
        },
        // 添加
        add() {
          this.goodsEditVue=true
        },
        // 查看详情
        lookDetail(row) {
            this.$router.push('/businessGoods/details?id=' + row.id)
        },
        onChangeDone() {
          this.goodsEditVue=false
          this.refreshChange()
        },
        //   列表查询
        getList: function (page, params) {
          this.tableLoading = true
          // 1.获取当前登陆用户ID
          let userInfo=  JSON.parse(sessionStorage.getItem("linshenxs-userInfo"))
          let account=userInfo.content.username
          getMerchantByAccount(
          {account: account}
          ).then(response => {
            fetchList(Object.assign({
              current: page.currentPage,
              size: page.pageSize,
              merchantId:response.data.data.id
            }, params, this.searchForm)).then(response => {
              this.tableData = response.data.data.records
              this.page.total = response.data.data.total
              this.tableLoading = false
            }).catch(() => {
              this.tableLoading = false
            })
          }).catch(() => {
            this.tableLoading = false
          })
        },

        // 选择事件(多选事件)
        onSelectionChange(r) {
          this.selectedData = r
        },
        // 批量 上架
        batchIsUpon(r) {
          let d = this.selectedData
          if(d == undefined || d.length == 0) {
            this.$message.error('请选择项进行上架')
            return
          }
          let a = d.map(r => { return r.stateName }).join(',')
          if(a.search('审核中') != -1){
            this.$message.error('只作用于商家下架的商品，已经上架正在审核')
            return;
          }else if (a.search('平台下架') != -1){
              this.$message.error('只作用于商家下架的商品，平台下架商品。')
              return;
          }else if(a.search('有效') != -1) {
            this.$message.error('只作用于商家下架的商品，商品已经上架。')
            return;
          }else {
            let ids = d.map(r => { return r.id }).join(',')
            this.$confirm('请确认是否上架已勾选的商品', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(function () {
              return isUpon({ids: ids})
            }).then(data => {
              this.$message.success('上架成功')
              this.getList(this.page)
            })
          }

        },
        // 批量 下架
        batchIsNotUpon() {
          let d = this.selectedData
          if(d == undefined || d.length == 0) {
            this.$message.error('请选择项进行下架')
            return
          }

          let a = d.map(r => { return r.stateName }).join(',')
          if(a.search('有效') != -1){
            let ids = d.map(r => { return r.id }).join(',')
            this.$confirm('商品正在审核，是否确认下架', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'error'
            }).then(function () {
              return isNotUpon({ids: ids})
            }).then(data => {
              this.$message.success('下架成功')
              this.getList(this.page)
            })
          }else if(a.search('下架') != -1){
            this.$message.error('可作用于上架,商品已经下架。')
            return;
          }else if(a.search('平台下架') != -1){
            this.$message.error('可作用于上架,商品审核拒绝。')
            return;
          }else if(a.search('审核中') != -1){
            this.$message.error('可作用于上架,商品正在审核。')
            return;
          }




        },
        // 删除
        handleDel: function (row, index) {
          if(row.stateName==='下架' || row.stateName==='平台下架'){
            this.$confirm('是否确认删除？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(function () {
              return delObj({
                ids:row.id
              })
            }).then(data => {
              this.$message.success('删除成功')
              this.getList(this.page)
            })
          }else {
            this.$message.error('只能删除下架/平台下架商品')
            return;
          }

        },
        // 批量删除
        handleDelBatch() {

          let d = this.selectedData
          if(d == undefined || d.length == 0) {
            this.$message.error('请选择项进行删除')
            return
          }
          let a = d.map(r => { return r.stateName }).join(',')

          if(a.search('下架') != -1 || a.search('平台下架') != -1){
            let ids = d.map(r => { return r.id }).join(',')
            this.$confirm('是否确认删除？', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(function () {
              return delObj({ids: ids})
            }).then(data => {
              this.$notify.success('删除成功')
              this.$refs.crud.toggleSelection();
              this.refreshChange();
            })
          }else {
            this.$message.error('只能删除下架/平台下架商品')
            return;
          }

        },

        // 保存
        handleSave: function (row, done,loading) {
            addObj(row).then(data => {
                this.$message.success('添加成功')
                done()
                this.getList(this.page)
            }).catch(() => {
                loading();
            });
        },
        // 每页条数改变事件
        sizeChange(pageSize){
            this.page.pageSize = pageSize
        },
        // 当前页发生改变事件
        currentChange(current){
            this.page.currentPage = current
        },
        // 查询事件
        searchChange(form, done) {
            this.searchForm = form
            this.page.currentPage = 0
            this.getList(this.page, form)
            done()
        },
        // 刷新事件
        refreshChange() {
            this.getList(this.page)
        },



        // queryParams(page, params) {
        //   let p = {
        //     current: page.currentPage,
        //     size: page.pageSize
        //   }
        //   if(params) {
        //     let range = this.parseRange(params)
        //     p = {
        //       ...p,
        //       startTime: range[0],
        //       endTime: range[1],
        //       goodsId: params.goodsId,
        //       shopName: params.shopName,
        //       classifyName: params.classifyId,
        //       goodsName: params.goodsName,
        //       state: params.state
        //
        //     }
        //   }
        //   return p
        // },
        // getList(page, params) {
        //   this.tableLoading = true
        //   let p = this.queryParams(page, params)
        //   fetchList(p).then(response => {
        //     this.tableData = response.data.data.records
        //     this.page.total = response.data.data.total
        //     this.tableLoading = false
        //   }).catch(() => {
        //     this.tableLoading=false
        //   })
        // },

      }
  }
</script>
