<template>
  <el-dialog
    :title="!dataForm.id ? this.$i18n.t('crud.addTitle') : this.$i18n.t('temp.modify')"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-row>
        <el-col :span="10">
          <el-form-item label="推广人" prop="tgUserName" :rules="{required: true, message: '推广人必填', trigger: 'blur'}"  size="small">
            <el-input v-model="dataForm.tgUserName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" :offset="1">
          <el-form-item label="推广店铺" prop="shopId" :rules="{required: true, message: '推广店铺必填', trigger: 'blur'}"  size="small">
            <el-select v-model="dataForm.shopId" placeholder="推广店铺" style="width: 330px" @change="shopChange"
                       controls-position="right" :clearable="true">
              <el-option v-for="(item,index) in shopList" :key="index" :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"  size="small">
            <el-input v-model="dataForm.upId"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="10" :offset="1">
          <el-form-item label="博主名称" prop="upName" :rules="{required: true, message: '博主昵称必填', trigger: 'blur'}" size="small">
            <el-input v-model="dataForm.upName"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="视频平台" prop="publishPlatform" size="small">
            <el-autocomplete
              class="inline-input"
              v-model="dataForm.publishPlatform"
              :fetch-suggestions="querySearch"
              placeholder="请输入内容"
              style="width :330px;">
              <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" size="small">
            <el-input v-model="dataForm.fansNum" type="number"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="是否拉黑" prop="isBlack" size="small">
            <el-select v-model="dataForm.isBlack" clearable style="width: 330px">
              <el-option label="拉黑" :value="1"></el-option>
              <el-option label="正常" :value="0"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10" :offset="1">
          <el-form-item label="备注" prop="remark" size="small">
            <el-input v-model="dataForm.remark" type="textarea"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-col :span="10">
        <el-form-item label="企微备注" prop="qwRemark" size="small">
          <el-input v-model="dataForm.qwRemark" ></el-input>
        </el-form-item>
      </el-col>
    </el-form>
    <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>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: null,
        upId: null,
        upName: null,
        publishPlatform: null,
        fansNum: null,
        tgUserName: null,
        shopId: this.$store.state.user.shopId,
        shopName: null,
        remark: null,
        isBlack: 0,
        createTime: null,
        qwRemark: null
      },
      dataRule: {
      },
      shopList:[],
      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/promotionUp/info/' + this.dataForm.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({data}) => {
            this.dataForm = data
          })
        }
      })
    },
    shopChange (index) {
      this.dataForm.shopName = this.shopList[index].shopName
    },
    getShopList() {
      this.$http({
        url: this.$http.adornUrl('/platform/shopDetail/getShopList'),
        method: 'get',
        params: this.$http.adornParams({})
      }).then(({ data }) => {
        if (data) {
          this.shopList = data
        }
      })
    },
    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)
      }
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.dataForm.shopName = this.shopList.find(item => item.shopId === this.dataForm.shopId).shopName
          this.$http({
            url: this.$http.adornUrl('/promotion/promotionUp'),
            method: this.dataForm.id ? 'put' : 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({data}) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 200,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    }
  }
}
</script>
