<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="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="menuLeft">
        <el-button type="primary"
                   icon="el-icon-plus"
                   size="small"
                   @click.stop="addOrUpdateHandle(scope.row)">新增</el-button>

        <el-button type="danger"
                   size="small"
                   @click.stop="deleteHandle"
                   :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </template>

      <template slot-scope="scope"
                slot="menu">
        <el-button type="primary"
                   size="small"
                   icon="el-icon-edit"
                   @click="addOrUpdateHandle(scope.row)">修改</el-button>
        <el-button type="primary"
                   icon="el-icon-document-checked"
                   size="small"
                   v-if="scope.row.state > 1"
                   @click="auditEventHandle(scope.row.distributionProdId)">下线管理</el-button>
        <el-button type="danger"
                   icon="el-icon-delete"
                   size="small"
                   @click="deleteHandle(scope.row)">删除</el-button>
      </template>

    </avue-crud>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible"
                   ref="addOrUpdate"
                   @refreshDataList="refreshChange"></add-or-update>

    <!-- 下线管理弹窗-->
    <offlineEventHandle v-if="offlineEventHandleVisible"
                        selectUrl="/distribution/distributionProd/getOfflineEventByDistProdId"
                        applyUrl="/distribution/distributionProd/auditApply"
                        ref="offlineEvent"
                        @refreshDataList="refreshChange"></offlineEventHandle>
  </div>
</template>

<script>
import { tableOption } from '@/crud/marketing/distribution/distributionProd'
import AddOrUpdate from './distribution-prod-add-or-update'
import OfflineEventHandle from '@/components/offline-event-handle'
export default {
  data () {
    return {
      dataForm: {
        prodName: ''
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      searchForm: {},
      dataList: [],
      tableOption: tableOption,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      offlineEventHandleVisible: false
    }
  },
  components: {
    AddOrUpdate,
    OfflineEventHandle
  },
  methods: {
    // 多选回调
    selectionChange (list) {
      this.dataListSelections = list
    },
    // 新增 / 修改
    addOrUpdateHandle (data) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data)
      })
    },
    // 点击查询
    searchChange (params) {
      this.searchForm = params
      this.page.currentPage = 1
      this.page.pageSize = 10
      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('/distribution/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('/distribution/distributionProd'),
          method: 'delete',
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      })
    },
    // 下线管理
    auditEventHandle (id) {
      this.offlineEventHandleVisible = true
      this.$nextTick(() => {
        this.$refs.offlineEvent.init(id)
      })
    }
  }
}
</script>
