<template>
  <div class="mod-print-promotionOrder">
    <!-- 搜索相关区域 -->
    <div class="search-bar">
      <el-form :inline="true" :model="searchForm" @keyup.enter.native="getDataList(this.page)" size="small">
        <div class="input-row">
          <el-form-item label="推广人" prop="tgUserName">
            <el-input v-model="searchForm.tgUserName" :clearable="true"></el-input>
          </el-form-item>
          <el-form-item label="博主ID" prop="upId">
            <el-input v-model="searchForm.upId" :clearable="true"></el-input>
          </el-form-item>
          <el-form-item label="博主名称" prop="upName">
            <el-input v-model="searchForm.upName" :clearable="true"></el-input>
          </el-form-item>
          <el-form-item label="是否拉黑" prop="isBlack">
            <el-select v-model="searchForm.isBlack" clearable>
              <el-option label="拉黑" :value="1"></el-option>
              <el-option label="正常" :value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="企微备注" prop="qwRemark">
            <el-input v-model="searchForm.qwRemark" :clearable="true"></el-input>
          </el-form-item>
          <el-form-item label="视频平台" prop="publishPlatform">
            <el-select v-model="searchForm.publishPlatform" filterable placeholder="请选择" :clearable="true">
              <el-option
                v-for="(item,index) in videoPlatformList"
                :key="index"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="推广店铺" prop="shopId">
            <el-select v-model="searchForm.shopId" placeholder="推广店铺"
                       controls-position="right" :clearable="true" style="width: 295px">
              <el-option v-for="item in shopList" :key="item.shopId" :label="item.shopName" :value="item.shopId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="登记时间" prop="orderTime">
            <el-date-picker
              :clearable="true"
              v-model="createTimeRange"
              type="datetimerange"
              align="right"
              value-format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              :default-time="['00:00:00', '23:59:59']">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <div class="default-btn primary-btn" @click="searchChange()">{{$t("crud.searchBtn")}}</div>
            <div class="default-btn" @click="clear()">重置</div>
          </el-form-item>
        </div>
      </el-form>
    </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="推广人" prop="tgUserName" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.tgUserName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="视频平台" prop="publishPlatform" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.publishPlatform}}</span>
            </template>
          </el-table-column>
          <el-table-column label="博主" prop="upId" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.upId}}</span>
              <br/>
              <span>{{ scope.row.upName}}</span>
            </template>
          </el-table-column>
          <!-- 博主粉丝 -->
          <el-table-column label="博主粉丝" prop="fansNum" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.fansNum}}</span>
            </template>
          </el-table-column>
          <el-table-column label="是否拉黑" prop="isBlack" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.isBlack === 1" type="danger">拉黑</el-tag>
              <!--              <el-tag v-if="scope.row.isBlack === 0" type="success"></el-tag>-->
            </template>
          </el-table-column>
          <el-table-column label="企微备注" prop="qwRemark" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.qwRemark}}</span>
            </template>
          </el-table-column>
          <!-- 推广店铺 -->
          <el-table-column label="推广店铺" prop="shopName" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.shopName}}</span>
            </template>
          </el-table-column>
          <!-- 登记时间 -->
          <el-table-column label="登记时间" prop="createTime" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.createTime}}</span>
            </template>
          </el-table-column>
          <!-- 备注 -->
          <el-table-column label="备注" prop="createTime" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.remark}}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" fixed="right" :label="$t('publics.operating')" width="auto">
            <template slot-scope="scope">
              <div class="text-btn-con">
                <div class="default-btn text-btn" @click="addOrUpdateHandle(scope.row.id)"
                     >{{$t("crud.updateBtn")}}</div>
                <div class="default-btn text-btn" @click.stop="deleteHandle(scope.row.id)"
                     >{{$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>
  </div>
</template>

<script>
import AddOrUpdate from './promotionUp-add-or-update'
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      shopId: this.$store.state.user.shopId,
      searchForm: {}, // 搜索
      dataListLoading: false,
      addOrUpdateVisible: false,
      shopList: [],
      videoPlatformList: [
        {value: '抖音', label: '抖音'},
        {value: '小红书', label: '小红书'},
        {value: '视频号', label: '视频号'},
        {value: '快手', label: '快手'},
        {value: 'B站', label: 'B站'},
      ],
      createTimeRange:[],
    }
  },
  components: {
    AddOrUpdate,
  },
  created () {
    this.getDataList(this.page);
  },
  mounted () {
    this.getShopList()
  },
  methods: {
    openPromotionVideoList(promotionOrderId){
      this.$refs.promotionOrderVideo.init(promotionOrderId)
    },
    openUploadDialog(){
      this.uploadVisible = true
      this.$nextTick(() => {
        this.$refs.promotionOrderUpload.init()
      })
    },
    clear(){
      this.searchForm = {}
    },
    shopChange (index) {
      this.dataForm.shopName = this.shopList[index].shopName
    },
    querySearch (queryString, cb) {
      var restaurants = this.videoPlatformList
      var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants
      // 调用 callback 返回建议列表的数据
      cb(results)
    },
    createFilter (queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
      }
    },
    getShopList() {
      this.$http({
        url: this.$http.adornUrl('/platform/shopDetail/getShopList'),
        method: 'get',
        params: this.$http.adornParams({shopId: this.shopId})
      }).then(({ data }) => {
        if (data) {
          this.shopList = data
        }
      })
    },
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/promotion/promotionUp/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,
            startTime: this.createTimeRange == null ? null : this.createTimeRange[0] ? this.createTimeRange[0] : null,
            endTime: this.createTimeRange == null ? null : this.createTimeRange[1] ? this.createTimeRange[1] : null
          },
            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('/promotion/promotionUp/' + id),
          method: 'delete',
          data: this.$http.adornData({})
        }).then(({ data }) => {
          this.$message({
            message: this.$i18n.t('publics.operation'),
            type: 'success',
            duration: 200,
            onClose: () => {
              this.refreshChange()
            }
          })
        })
      }).catch(() => { })
    },
    // 刷新回调
    refreshChange () {
      this.page.currentPage = 1
      this.getDataList(this.page)
    },
    searchChange (params) {
      this.page.currentPage = 1
      this.getDataList(null)
    },
    handleSizeChange (val) {
      this.page.pageSize = val
      this.getDataList()
    },
    handleCurrentChange (val) {
      this.page.currentPage = val
      this.getDataList()
    },
  }
}
</script>
<style lang="scss">
.mod-print-promotionOrder {
  .search-bar {
    .input-row {
      .select-time-btn {
        margin-right: 20px;
        display: inline-block;
        color: #AAAAAA;
        font-size: 14px;
        cursor: pointer;

        &:last-child {
          margin-right: 0;
        }
      }

      .select-time-btn.is-active {
        color: #155BD4;
      }

    }
  }
}
</style>
