<template>
  <div class="mod-print-prodAssist">
    <!-- 搜索相关区域 -->
    <!--<div class="search-bar">
      <el-button class="default-btn primary-btn" @click="searchChange">{{$t('crud.searchBtn')}}</el-button>
    </div>-->
    <!-- 列表相关区域 -->
    <div class="main-container">
      <div class="operation-bar">
        <div class="default-btn primary-btn" @click="addOrUpdateHandle()">{{$t("crud.addTitle")}}</div>
      </div>
      <div class="table-con spec-table">
        <el-table
          ref="specListTable"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row"
          style="width: 100%">
          <el-table-column label="ID" prop="assistName" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.assistId}}</span>
            </template>
          </el-table-column>
          <el-table-column label="助力封面" prop="assistImg" align="center">
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                :src="resourcesUrl + scope.row.assistImg"
                v-if="scope.row.assistImg"
                fit="fill"
              />
            </template>
          </el-table-column>
          <!-- 助力名称 -->
          <el-table-column label="助力名称" prop="assistName" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.assistName}}</span>
            </template>
          </el-table-column>
          <!-- 助力人数 -->
          <el-table-column label="助力人数" prop="assistNum" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.assistNum}}</span>
            </template>
          </el-table-column>
          <!-- 助力封面图 -->
          <!-- 已助力电量 -->
          <el-table-column label="已助力电量" prop="assistPower" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.assistPower}}</span>
            </template>
          </el-table-column>
          <!-- 单位发电量 -->
          <el-table-column label="单位发电量" prop="unitPower" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.unitPower}}</span>
            </template>
          </el-table-column>
          <!-- 解锁电量 -->
          <el-table-column label="解锁电量" prop="unlockPower" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.unlockPower}}</span>
            </template>
          </el-table-column>
          <!-- 助力描述 -->
          <el-table-column label="助力描述" prop="assistDesc" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.assistDesc}}</span>
            </template>
          </el-table-column>
          <el-table-column label="邀请海报" prop="assistImg" align="center">
            <template slot-scope="scope">
              <el-image
                style="width: 100px; height: 100px"
                :src="resourcesUrl + scope.row.assistPoster"
                v-if="scope.row.assistPoster"
                fit="fill"
              />
            </template>
          </el-table-column>
          <el-table-column align="center" fixed="right" :label="$t('publics.operating')" width="250px">
            <template slot-scope="scope">
              <div class="text-btn-con">
                <div class="default-btn text-btn" @click="addOrUpdateHandle(scope.row.assistId)">{{$t("crud.updateBtn")}}</div>
                <div class="default-btn text-btn" @click="assistRecord(scope.row.assistId)">助力记录</div>
                <div class="default-btn text-btn" @click="sendUnlockMessage(scope.row.assistId)">解锁通知</div>
                <div class="default-btn text-btn" @click.stop="deleteHandle(scope.row.assistId)">{{$t("text.delBtn")}}</div>
                </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
        <el-pagination
          v-if="dataList.length"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total">
        </el-pagination>
      </div>
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="refreshChange"></add-or-update>
    <ProdAssistRecord ref="assistRecordRef"></ProdAssistRecord>

  </div>
</template>

<script>
import AddOrUpdate from './prodAssist-add-or-update'
import ProdAssistRecord from './prodAssistRecord'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      shopId: this.$store.state.user.shopId,
      assistRecordVisible: false,
      resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
      searchForm: {}, // 搜索
      dataListLoading: false,
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate,
    ProdAssistRecord
  },
  created () {
    this.getDataList()
  },
  mounted () {
  },
  methods: {
    sendUnlockMessage(assistId){
      this.$confirm("确定要下发解锁通知吗？", this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/assist/prodAssist/unlockNotify'),
          method: 'POST',
          data: {assistId: assistId, shopId: this.shopId}
        }).then(({data}) => {
          this.$message.success("下发成功，请留意助力记录通知结果")
        })
      });
    },
    assistRecord(assistId){
      this.$refs.assistRecordRef.init(assistId);
    },
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/assist/prodAssist/page'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign({
            current: page == null ? this.page.currentPage : page.currentPage,
            size: page == null ? this.page.pageSize : page.pageSize,
            shopId: this.shopId
          },
            this.searchForm
          )
        )
      }).then(({data}) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    deleteHandle (id) {
      this.$confirm(this.$i18n.t('admin.isDeleOper') + '?', this.$i18n.t('text.tips'), {
        confirmButtonText: this.$i18n.t('crud.filter.submitBtn'),
        cancelButtonText: this.$i18n.t('crud.filter.cancelBtn'),
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/assist/prodAssist/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    // 刷新回调
    refreshChange () {
      this.page.currentPage = 1
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.searchForm = params
      this.getDataList(this.page)
    },
    handleSizeChange (val) {
      this.page.pageSize = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      this.getDataList()
    }
  }
}
</script>
<style lang="scss">
.mod-print-prodAssist {
}
</style>
