<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="orderNumber">
            <el-input v-model="searchForm.orderNumber" :clearable="true" size="small"></el-input>
          </el-form-item>
          <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="tgUserName">
            <el-input v-model="searchForm.upId" :clearable="true"></el-input>
          </el-form-item>
          <el-form-item label="博主名称" prop="tgUserName">
            <el-input v-model="searchForm.upName" :clearable="true"></el-input>
          </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="status">
            <el-select v-model="searchForm.status" placeholder="请选择" size="small" :clearable="true">
              <el-option :key="2" label="待发货" value="待发货"></el-option>
              <el-option :key="3" label="已发货" value="已发货"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="视频状态" prop="status">
            <el-select v-model="searchForm.publishStatus" placeholder="视频是否已发布" size="small" :clearable="true">
              <el-option :key="2" label="视频已发布" value="已发布"></el-option>
              <el-option :key="3" label="视频未发布" value="待发布"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="粉丝数：" label-width="85px">
            <InputNumberRange v-model="fansNumRange" type="number"></InputNumberRange>
          </el-form-item>
          <el-form-item label="下单时间" prop="orderTime">
            <el-date-picker
              :clearable="true"
              v-model="orderTimeRange"
              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 label="发货时间" prop="deliveryTime">
            <el-date-picker
              :clearable="true"
              v-model="deliveryTimeRange"
              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" @click="setDateRange(1)">今日</div>
            <div class="default-btn" @click="setDateRange(2)">昨日</div>
            <div class="default-btn" @click="setDateRange(3)">近3天</div>
            <div class="default-btn" @click="setDateRange(4)">近5天</div>
            <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 class="default-btn primary-btn" @click="openUploadDialog()">导入数据</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="博主ID:名称" prop="upId" align="center" width="160px">
            <template slot-scope="scope">
              <div v-for="upItem in scope.row.videoList">
                <div>{{upItem.upId}}</div>
                <div>{{upItem.upName}}</div>
              </div>
            </template>
          </el-table-column>
          <!-- 博主粉丝 -->
          <el-table-column label="博主粉丝" prop="fansNum" align="center">
            <template slot-scope="scope">
              <div v-for="item in scope.row.videoList">{{ item.publishPlatform + ":" + (item.fansNum? item.fansNum:"") + "\n"}}</div>
            </template>
          </el-table-column>
          <!-- 订单编号 -->
          <el-table-column label="推广订单" prop="orderNumber" align="center" width="200px">
            <template slot-scope="scope">
              <span>{{ scope.row.orderNumber}}</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="status" align="center" width="160px">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === '待发货'" type="warning">待发货</el-tag>
              <div v-if="scope.row.status === '已发货'">
                <div class="buyer-phone" v-for="deliveryOrder in scope.row.deliveryOrderList"
                     style="text-decoration:underline;cursor: pointer; color: dodgerblue;font-size: 16px"
                     @click="openDeliveryPage(deliveryOrder.dvyFlowId, deliveryOrder.dvyNo)">{{ deliveryOrder.dvyFlowId }}
                </div>
              </div>
<!--              <el-tag v-if="scope.row.status === '已发货'" type="success">-->
<!--                -->
<!--              </el-tag>-->
              <el-tag v-if="scope.row.status.indexOf('部分发货') != -1" type="warning">{{scope.row.status}}</el-tag>
            </template>
          </el-table-column>
          <!-- 关联视频 -->
          <el-table-column label="推广视频" prop="promotionVideo" align="center">
            <template slot-scope="scope">
              <el-button size="small" @click="openPromotionVideoList(scope.row.id)">查看视频</el-button>
            </template>
          </el-table-column>
          <!-- 未发货，已发货 -->
          <el-table-column label="寄拍/付费" prop="payType" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.payType === '寄拍'" type="primary">寄拍</el-tag>
              <el-tag v-if="scope.row.payType === '付费'" type="danger">付费：{{scope.row.payAmt}}元</el-tag>
            </template>
          </el-table-column>
          <!-- 未发货，已发货 -->
          <el-table-column label="结算状态" prop="settleStatus" align="center">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.settleStatus === '待结算'" type="danger">待结算</el-tag>
              <el-tag v-if="scope.row.settleStatus === '已结算'" type="success">已结算</el-tag>
            </template>
          </el-table-column>
          <!-- 发货时间-->
          <el-table-column label="下单时间" prop="orderTime" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.orderTime}}</span>
            </template>
          </el-table-column>
          <!-- 发货时间-->
          <el-table-column label="发货时间" prop="deliveryTime" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.deliveryTime}}</span>
            </template>
          </el-table-column>
          <!-- 视频发布时间-->
          <el-table-column label="视频发布时间" prop="publishTime" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.videoList.length > 0? scope.row.videoList[0].publishTime: ""}}</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>
    <promotion-order-upload
      v-if="uploadVisible"
      ref="promotionOrderUpload"
      @refreshDataList="getDataList(page)">
    </promotion-order-upload>
    <PromotionOrderVideo ref="promotionOrderVideo"></PromotionOrderVideo>
  </div>
</template>

<script>
import AddOrUpdate from './promotionOrder-add-or-update'
import PromotionOrderUpload from './promotion-order-upload'
import PromotionOrderVideo from './promotion-order-video'
import InputNumberRange from '@/components/input-number-range'
import moment from "moment"
export default {
  data () {
    return {
      dataList: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      shopId: this.$store.state.user.shopId,
      searchForm: {}, // 搜索
      dataListLoading: false,
      addOrUpdateVisible: false,
      shopList: [],
      orderTimeRange: [],
      deliveryTimeRange: [],
      uploadVisible: false,
      fansNumRange:[]
    }
  },
  components: {
    AddOrUpdate,
    PromotionOrderUpload,
    InputNumberRange,
    PromotionOrderVideo
  },
  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 = {}
      this.dateRange = []
      this.orderTimeRange = [],
        this.deliveryTimeRange = [],
        this.fansNumRange = []
    },
    getShopList() {
      this.$http({
        url: this.$http.adornUrl('/shop/shopDetail/getShopList'),
        method: 'get',
        params: this.$http.adornParams({shopId: this.shopId})
      }).then(({ data }) => {
        if (data) {
          this.shopList = data
        }
      })
    },
    openDeliveryPage (deliveryNo, dvyNo) {
      // window.open("https://t.17track.net/zh-cn#nums=" + deliveryNo, '_blank');
      let copy = (e) => {
        e.preventDefault()
        e.clipboardData.setData('text/plain', deliveryNo)
        document.removeEventListener('copy', copy)
      }
      document.addEventListener('copy', copy)
      document.execCommand('Copy')
      if (dvyNo === 'STO') {
        window.open('https://www.sto.cn/pc/service-page/iframe_2_21_' + deliveryNo + ',')
      } else if (dvyNo === 'YTO') {
        window.open('https://www.yto.net.cn/tracesimple.html/?orders=' + deliveryNo)
      } else if (dvyNo === 'ZTO') {
        window.open('https://www.zto.com/express/expressCheck.html?txtBill=' + deliveryNo)
      } else {
        window.open('https://www.baidu.com/s?wd=%E7%89%A9%E6%B5%81%E6%9F%A5%E8%AF%A2&rsv_spt=1&rsv_iqid=0xe1fe334b0008559a&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_dl=tb&rsv_sug3=16&rsv_sug1=12&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=2849&rsv_sug4=3141', '_blank')
      }
    },
    getDataList (page) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/promotion/promotionOrder/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,
            startOrderTime: this.orderTimeRange == null ? null : this.orderTimeRange[0] ? this.orderTimeRange[0] : null,
            endOrderTime: this.orderTimeRange == null ? null : this.orderTimeRange[1] ? this.orderTimeRange[1] : null,
            startDeliveryTime: this.deliveryTimeRange == null ? null : this.deliveryTimeRange[0] ? this.deliveryTimeRange[0] : null,
            endDeliveryTime: this.deliveryTimeRange == null ? null : this.deliveryTimeRange[1] ? this.deliveryTimeRange[1] : null,
            startFansNum: this.fansNumRange == null ? null : this.fansNumRange[0] ? this.fansNumRange[0] : 0,
            endFansNum: this.fansNumRange == null ? null : this.fansNumRange[1] ? this.fansNumRange[1] : 0,
            shopId: this.$store.state.user.shopId,
          },
            this.searchForm
          )
        )
      }).then(({data}) => {
        this.dataList = data.records
        this.page.total = data.total
        this.dataListLoading = false
      })
    },
    setDateRange(val) {
      var startDay = null
      var endDay = null
      if (val === 1) {
        startDay = 0
        endDay = 0
      } else if (val === 2) {
        startDay = -1
        endDay = -1
      } else if (val === 3) {
        startDay = -3
        endDay = -1
      } else if (val === 4) {
        startDay = -5
        endDay = -1
      } else {
        return
      }
      // 开始时间
      let startTime = moment().add(startDay, 'days').startOf('days').format('LL')
      // 结束时间
      let endTime = moment().add(endDay, 'days').endOf('days').format('LL')
      // this.orderTimeRange = [startTime, endTime]
      this.deliveryTimeRange = [startTime, endTime]
    },
    // 新增 / 修改
    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/promotionOrder/' + 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>
