<template>
  <el-dialog
    :title="!dataForm.id ? this.$i18n.t('crud.addTitle') : this.$i18n.t('temp.modify')"
    :close-on-click-modal="false"
    :modal-append-to-body="false"
    :visible.sync="visible"
    top="1vh"
    @close="closeDialog">
    <el-row>
      <el-col :span="24">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="110px">
<!--          <el-form-item label="" prop="id">-->
<!--            <el-input v-model="dataForm.id"size="small"></el-input>-->
<!--          </el-form-item>-->
          <el-row>
            <el-col :span="10">
              <el-form-item label="推广人" prop="tgUserName">
                <el-input v-model="dataForm.tgUserName"size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10" :offset="1">
              <el-form-item label="推广店铺" prop="shopId">
                <el-select v-model="dataForm.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-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="博主ID" prop="upId">
                <el-input v-model="dataForm.upId"size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10"  :offset="1">
              <el-form-item label="博主昵称" prop="upName">
                <el-input v-model="dataForm.upName"size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="21">
              <el-form-item label="视频链接" prop="videoUrl">
                <el-input v-model="dataForm.videoUrl"size="small" type="textarea" rows="3"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="t+1点赞数" prop="likeNumT1">
                <el-input v-model="dataForm.likeNumT1"size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10" :offset="1">
              <el-form-item label="t+7点赞数" prop="likeNumT7">
                <el-input v-model="dataForm.likeNumT7"size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="t+1播放数" prop="playNumT1">
                <el-input v-model="dataForm.playNumT1"size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="10" :offset="1">
              <el-form-item label="t+7播放数" prop="playNumT7">
                <el-input v-model="dataForm.playNumT7"size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
              <el-col :span="10">
                <el-form-item label="发布平台" prop="publishPlatform">
                  <el-autocomplete style="width:300px"
                    class="inline-input"
                    v-model="dataForm.publishPlatform"
                    :fetch-suggestions="querySearch"
                    placeholder="请输入内容">
                    <template slot-scope="{ item }">
                      <div class="name">{{ item.label }}</div>
                    </template>
                  </el-autocomplete>
                </el-form-item>
              </el-col>
            <el-col :span="10" :offset="1">
              <el-form-item label="粉丝数" prop="fansNum">
                <el-input v-model="dataForm.fansNum"size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
              <el-col :span="7">
                <el-form-item label="评论数" prop="commNum">
                  <el-input v-model="dataForm.commNum"size="small"></el-input>
                </el-form-item>
              </el-col>
            <el-col :span="7">
              <el-form-item label="点赞数" prop="likeNum">
                <el-input v-model="dataForm.likeNum"size="small"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="播放数" prop="playNum">
                <el-input v-model="dataForm.playNum"size="small"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="寄拍付费" prop="payType">
                <el-select v-model="dataForm.payType" placeholder="请选择" size="small" style="width: 295px;">
                  <el-option :key="0" label="寄拍" value="寄拍"></el-option>
                  <el-option :key="1" label="付费 " value="付费"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="10" :offset="1">
              <el-form-item label="付费金额" prop="payAmt">
                <el-input v-model="dataForm.payAmt"size="small" type="number"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="发布状态" prop="status">
                <el-select v-model="dataForm.status" placeholder="请选择" size="small" style="width: 295px">
                  <el-option :key="1" label="待发布" value="待发布"></el-option>
                  <el-option :key="2" label="已发布 " value="已发布"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="10" :offset="1">
              <el-form-item label="结算状态" prop="settleStatus">
                <el-select v-model="dataForm.settleStatus" placeholder="请选择" size="small" style="width: 295px;">
                  <el-option :key="1" label="待结算" value="待结算"></el-option>
                  <el-option :key="2" label="已结算 " value="已结算"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="10">
              <el-form-item label="发布时间" prop="publishTime">
                <el-date-picker
                  style="width: 295px"
                  size="small"
                  v-model="dataForm.publishTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder="选择日期时间"
                  default-time="12:00:00">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="10" :offset="1">
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="相关图片" prop="pic">
              <el-upload
                :disabled="isView"
                ref="upload"
                :multiple="true"
                :action="$http.adornUrl('/admin/file/simpleUpload')"
                list-type="picture-card"
                :headers="{Authorization: $cookie.get('Authorization_vs'),locale:lang}"
                :on-success="onUploadSuccess"
                :file-list="fileList"
                :limit="1"
                :on-remove="handlePicRemove"
                :on-preview="handlePicPreview">
                <i class="el-icon-plus"></i>
              </el-upload>
              <div  @paste="pasteImg($event)">
                <textarea placeholder="点击这里，然后把图片粘贴" style="width: 100%;height: 100px;border: 1px solid #c9c9c9;border-radius: 5px;"></textarea>
              </div>
            </el-form-item>
          </el-row>
    </el-form>
      </el-col>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button class="default-btn" @click="visible = false">{{$t("crud.filter.cancelBtn")}}</el-button>
      <el-button class="default-btn primary-btn" type="primary" @click="dataFormSubmit()">{{$t("crud.filter.submitBtn")}}</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import { uploadFile } from '@/utils/httpRequest.js'
export default {
  data () {
    return {
      visible: false,
      isView: false,
      lang: localStorage.getItem('lang') || 'zh_CN',
      dataForm: {
        id: null,
        tgUserName: null,
        upId: null,
        upName: null,
        videoUrl: null,
        likeNumT1: null,
        playNumT1: null,
        commNum: null,
        likeNumT7: null,
        playNumT7: null,
        shopId: this.$store.state.user.shopId,
        shopName: null,
        publishPlatform: null,
        payType: null,
        payAmt: null,
        settleStatus: null,
        createTime: null,
        updateTime: null,
        playNum: null,
        likeNum: null,
        status: null,
        publishTime:null,
        pic:null
      },
      dataRule: {
      },
      imgDialogVisible: false,
      shopList: [],
      fileList:[],
      resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
      videoPlatformList: [
        {value: '抖音', label: '抖音'},
        {value: '小红书', label: '小红书'},
        {value: '视频号', label: '视频号'},
        {value: '快手', label: '快手'},
        {value: 'B站', label: 'B站'},
      ],
    }
  },
  mounted () {
    this.getShopList()
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl('/promotion/promotionVideo/info/' + this.dataForm.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.dataForm = data
            if(this.dataForm.pic) {
              this.fileList = [];
              let picArray = this.dataForm.pic.split(",");
              for (let i = 0; i < picArray.length; i++) {
                this.fileList.push({ url: this.resourcesUrl + picArray[i], response: {filePath: picArray[i] }})
              }
            }
          })
        }
      })
    },
    closeDialog(){
      this.fileList = []
      this.$emit('refreshDataList')
    },
    pasteImg(e) {
      const { items } = e.clipboardData; // 获取粘贴板文件对象
      if (items.length) {
        for (const item of items) {
          if (item.type.indexOf('image') !== -1) {
            const file = item.getAsFile(); // 获取图片文件
            if (file) {
              uploadFile(
                this.$http.adornUrl('/admin/file/simpleUpload'),
                file
              ).then(({ data }) => {
                let obj = {url: data.resourcesUrl + data.filePath, response: {filePath: data.filePath }}
                this.fileList.push(obj)
              })
            }
          }
        }
      }
    },
    onUploadSuccess(response, file, fileList){
      this.fileList = fileList;
    },
    handlePicRemove(file, fileList){
      this.fileList = fileList;
    },
    handlePicPreview(file){
      this.dialogImageUrl = file.url
      let url = file.response.resourcesUrl + file.response.filePath
      this.videoFlag = this.checkMediaType(url)
      this.imgDialogVisible = true
    },
    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('/shop/shopDetail/getShopList'),
        method: 'get',
        params: this.$http.adornParams({shopId: this.shopId})
      }).then(({ data }) => {
        if (data) {
          this.shopList = data
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      let filePath = [];
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if(this.fileList.length > 0 && !this.dataForm.id){
            this.fileList.forEach(item => {
              filePath.push(item.response.filePath);
            });
            this.dataForm.pic = filePath.join(",");
          }else{
            this.fileList.forEach(item => {
              filePath.push(item.response.filePath);
            });
            this.dataForm.pic = filePath.join(",");
          }
          this.$http({
            url: this.$http.adornUrl('/promotion/promotionVideo'),
            method: this.dataForm.id ? 'put' : 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({data}) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
            })
            this.visible = false
            this.$emit('refreshDataList')
          })
        }
      })
    }
  }
}
</script>
