<template>
  <div class="mod-distribution-prod">
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               v-model="dataForm"
               :table-loading="dataListLoading"
               :option="tableOption"
               @search-change="searchChange"
               @on-load="getDataList"
               @selection-change="selectionChange">
      <template slot-scope="scope"
                slot="prodName">
        {{scope.row.product.prodName}}
      </template>

      <template slot-scope="scope"
                slot="pic">
        <img :src="scope.row.product.pic"
             width="100"
             height="100" />
      </template>

      <!-- <template slot-scope="scope"
                slot="menuLeft">
        <el-button type="primary"
                   icon="el-icon-plus"
                   size="small"
                   @click.stop="addOrUpdateHandle(scope.row)"
                   v-if="isAuth('distribution:distributionProd:save')">新增</el-button>

        <el-button type="danger"
                   size="small"
                   @click.stop="deleteHandle"
                   :disabled="dataListSelections.length <= 0"
                   v-if="isAuth('distribution:distributionProd:delete')">批量删除</el-button>
      </template> -->

      <template slot-scope="scope"
                slot="state">
        <el-tag v-if="scope.row.state === 0"
                size="small">商家下架</el-tag>
        <el-tag v-if="scope.row.state === 1"
                size="small"
                type="success">商家上架</el-tag>
        <el-tag v-else-if="scope.row.state ===2"
                size="small"
                type="danger">违规下线</el-tag>
        <el-tag v-else-if="scope.row.state ===3"
                size="small"
                type="warning">等待审核</el-tag>
      </template>

      <template slot-scope="scope"
                slot="menu">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-edit"
                   @click="addOrUpdateHandle(scope.row)"
                   v-if="isAuth('platform:distributionProd:info')">查看详情</el-button>
        <el-button icon="el-icon-bottom"
                   size="small"
                   type="primary"
                   v-if="isAuth('platform:distributionProd:update') && scope.row.state<2"
                   @click="offlineDistributionProdHandle(scope.row)">下线活动</el-button>
        <el-button icon="el-icon-document-checked"
                   size="small"
                   type="primary"
                   v-if="isAuth('platform:distributionProd:update') && scope.row.state > 1"
                   @click="distributionProdAuditHandle(scope.row.distributionProdId)">下线管理</el-button>

        <!-- <el-button type="danger"
                   icon="el-icon-delete"
                   size="small"
                   v-if="isAuth('prod:prod:delete')"
                   @click="deleteHandle(scope.row)">删除</el-button> -->
      </template>

    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible"
                   ref="addOrUpdate"
                   @refreshDataList="refreshChange"></add-or-update>
    <!-- 商品审核弹窗 -->
    <DistributionProdAudit v-if="distributionProdAuditVisible"
                           ref="distributionProdAudit"
                           selectUrl='/platform/distributionProd/getOfflineEventByDistProdId'
                           updateUrl='/platform/distributionProd/auditDistributionProd'
                           @refreshDataList="refreshChange"></DistributionProdAudit>
  </div>
</template>

<script>
import { tableOption } from '@/crud/distribution/distributionProd'
import AddOrUpdate from './distribution-prod-add-or-update'
import DistributionProdAudit from '@/components/offline-audit'
export default {
  data () {
    return {
      dataForm: {
        prodName: ''
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      searchForm: {},
      dataList: [],
      tableOption: tableOption,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      distributionProdAuditVisible: false
    }
  },
  components: {
    AddOrUpdate,
    DistributionProdAudit
  },
  methods: {
    // 多选回调
    selectionChange (list) {
      this.dataListSelections = list
    },
    // 新增 / 修改
    addOrUpdateHandle (data) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data)
      })
    },
    // 点击查询
    searchChange (params) {
      this.page.currentPage = 1
      this.searchForm = params
      this.getDataList(this.page)
    },
    // 刷新回调
    refreshChange () {
      this.page = this.$refs.crud.$refs.tablePage.defaultPage
      this.getDataList(this.page)
    },
    // 获取数据列表
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/platform/distributionProd/page'),
        method: 'get',
        params: this.$http.adornParams(Object.assign({
          current: page ? page.currentPage : 1,
          size: page ? page.pageSize : 20
        }, this.searchForm))
      }).then(({ data }) => {
        this.page.total = data.total
        this.page.pageSize = data.size
        this.page.currentPage = data.current
        this.dataList = data.records
        this.dataListLoading = false
      })
    },
    // 删除
    deleteHandle (row, index) {
      var ids = row.distributionProdId ? [row.distributionProdId] : this.dataListSelections.map(item => {
        return item.distributionProdId
      })
      this.$confirm(`确定进行[${row.distributionProdId ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/platform/distributionProd'),
          method: 'delete',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      })
    },
    // 下线活动
    offlineDistributionProdHandle (row) {
      this.$prompt('确定对活动需要修改[' + row.couponName + ']进行下线操作？如是，请填写下线理由', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then((obj) => {
        if (!obj.value) {
          this.$message.error('下线原因不能为空')
          return
        }
        this.$http({
          url: this.$http.adornUrl(`/platform/distributionProd/offline`),
          method: 'post',
          data: this.$http.adornData({
            handleId: row.distributionProdId,
            offlineReason: obj.value || null
          })
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      })
    },
    // 弹窗处理
    distributionProdAuditHandle (id) {
      this.distributionProdAuditVisible = true
      this.$nextTick(() => {
        this.$refs.distributionProdAudit.init(id)
      })
    }
  }
}
</script>
