<template>
  <div>
    <el-dropdown @command="handleClick">
      <span class="el-dropdown-link">
        操作<i class="el-icon-arrow-down el-icon--right"></i>
      </span>
      <el-dropdown-menu slot="dropdown">
        <el-dropdown-item v-if="isShowDetail" :command="beforeGoDetail(params.goodsId)">查看详情</el-dropdown-item>
        <el-dropdown-item v-if="isShowAddFavorite" :command="beforeAddFavorite(params.goodsId, params.monitorId)">加入收藏</el-dropdown-item>
        <el-dropdown-item v-if="isShowDelFavorite" :command="beforeDelFavorite(params.id)">取消收藏</el-dropdown-item>
        <el-dropdown-item divided v-if="isShowGroup" :command="beforeAddGroup(params.goodsId)">加入分组</el-dropdown-item>
        <el-dropdown-item divided :command="beforeCopy(params.url)">商品采集</el-dropdown-item>
        <el-dropdown-item divided :command="beforeGoWeb(params.url)">访问商品</el-dropdown-item>
        <el-dropdown-item :command="beforeGoMal(params.mallId)">访问店铺</el-dropdown-item>
      </el-dropdown-menu>
    </el-dropdown>

    <ai-dialog
      title="复制"
      :visible.sync="copyFromDlgShow"
      :close-on-click-modal="false"
      width="790px"
      customFooter
      :append-to-body="true"
      @close="handleClose">
      <ai-copy-from-temu v-if="copyFromDlgShow" :params="temuParams" @onClose="handleClose" @onSuccess="handleSuccess"></ai-copy-from-temu>
    </ai-dialog>

    <ai-dialog
      title="添加到分组"
      :visible.sync="addGroupDlgShow"
      :close-on-click-modal="false"
      width="790px"
      customFooter
      :append-to-body="true"
      @close="handleClose">
      <el-form class="ai-form" :model="addGroupForm" label-width="120px" ref="addGroupForm">
        <el-form-item label="分组" style="width: 100%;" prop="groupId" :rules="[{ required: true, message: '请选择分组', trigger: 'blur' }]">
          <el-select style="width: 380px" v-model="addGroupForm.groupId" placeholder="请选择分组">
            <el-option
              v-for="item in groupList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="addGroupDlgShow = false">取消</el-button>
        <el-button type="primary" @click="addToGroup">确定</el-button>
      </div>
    </ai-dialog>
  </div>
</template>
<script>
import AiCopyFromTemu from "./AiCopyFromTemu.vue";
import { Message } from 'element-ui'

export default {
  name: "AiProductDropDown",
  components: {AiCopyFromTemu},
  props: ['params', 'isShowDetail', 'isShowAddFavorite', 'isShowDelFavorite', 'isShowGroup'],
  data() {
    return {
      info: {},
      copyFromDlgShow: false,
      addGroupDlgShow: false,
      temuParams: {},
      addGroupForm: {
        groupId: '',
        goodsId: ''
      },
      groupList: []
    }
  },
  computed: {
  },
  created() {
  },
  methods: {
    handleClick(e) {
      if (e.type == 'detail') {
        this.$emit('onGoDetail')
      } else if (e.type == 'copy') {
        if (e.url.startsWith('http')) {
          this.temuParams = {url: e.url}
        } else {
          this.temuParams = {url: 'https://www.temu.com/' + e.url}
        }
        this.copyFromDlgShow = true
      } else if (e.type == 'addFavorite') {
        this.$http.post('/api/monitorFavorite/add',{goodsId: e.goodsId, monitorId: e.monitorId}).then(res => {
          if (res.code == 0) {
            Message.success('收藏成功')
          }
        })
      } else if (e.type == 'delFavorite') {
        this.$http.post('/api/monitorFavorite/del?id=' + e.id).then(res => {
          if (res.code == 0) {
            Message.success('删除收藏成功')
            this.$emit('onDelFavoriteSuccess')
          }
        })
      } else if (e.type == 'addGroup') {
        this.addGroupForm.goodsId = e.goodsId
        this.$http.post('/api/newProductGroup/myPage?size=1000').then(res => {
          if (res.code == 0) {
            this.addGroupDlgShow = true
            this.groupList = res.data.records
          }
        })
      } else if (e.type == 'goMall') {
        window.open('https://www.temu.com/mall.html?mall_id=' + e.mallId, '_blank');
      } else if (e.type == 'goWeb') {
        if (e.url.startsWith('http')) {
          window.open(e.url, '_blank');
        } else {
          window.open('https://www.temu.com/' + e.url, '_blank');
        }
      }
    },
    beforeGoDetail(goodsId) {
      return {type: 'detail', goodsId: goodsId}
    },
    beforeAddFavorite(goodsId, monitorId) {
      return {type: 'addFavorite', goodsId: goodsId, monitorId: monitorId}
    },
    beforeDelFavorite(id) {
      return {type: 'delFavorite', id: id}
    },
    beforeAddGroup(goodsId) {
      return {type: 'addGroup', goodsId: goodsId}
    },
    beforeCopy(url) {
      return {type: 'copy', url: url}
    },
    beforeGoMal(mallId) {
      return {type: 'goMall', mallId: mallId}
    },
    beforeGoWeb (url) {
      return {type: 'goWeb', url: url}
    },
    handleClose() {
      this.copyFromDlgShow = false
    },
    handleSuccess() {
      this.copyFromDlgShow = false
    },
    addToGroup() {
      this.$refs.addGroupForm.validate((valid) => {
        if (valid) {
          this.$http.post('/api/newProductGroupDetail/add', {...this.addGroupForm}).then(res => {
            if (res.code == 0) {
              this.addGroupDlgShow = false
              Message.success("商品成功添加到分组")
            }
          })
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">
</style>
