<template>
  <div>
<!--    <el-button type="text" size="mini" @click="releaseNotice">{{data.status === '发布'?'取消发布':'发布'}}</el-button>-->
    <el-button v-if="release" type="primary" plain size="small"  @click="open">发布</el-button>
    <el-button v-if="unRelease" type="primary" plain size="small"  @click="unOpen">取消发布</el-button>
  </div>
</template>

<script>
export default {
  props:['data'],
  data () {
    return {
      release:false,
      unRelease:false
    }
  },
  methods:{
    async releaseNotice () {
      const res = await this.$api.requested({
        // "classname": "webmanage.saletool.notice.notice",
        // "method": this.data.status === "发布"?"undercarriage":"release",
        "id":"20221101095203",
        "content": {
          "sat_noticeids":[this.$route.query.id]
        }
      })
      console.log(res)
      if (res.code === 1) {
        this.$notify({
          title: '成功',
          message: this.data.status === "发布"?'下架成功':'发布成功',
          type: 'success'
        });

        this.$emit('onSuccess')
        this.release = false
        this.unRelease = true
      } else {
        this.$notify({
          title: '失败',
          message: res.data,
          type: 'error'
        });
      }
    },
    async unReleaseNotice () {
      const res = await this.$api.requested({
        // "classname": "webmanage.saletool.notice.notice",
        // "method": this.data.status === "发布"?"undercarriage":"release",
        "id":"20221101095303",
        "content": {
          "sat_noticeids":[this.data.sat_noticeid]
        }
      })
      console.log(res)
      if (res.code === 1) {
        this.$notify({
          title: '成功',
          message: this.data.status === "发布"?'下架成功':'发布成功',
          type: 'success'
        });
        this.$emit('onSuccess')
        this.release = true
        this.unRelease = false
      } else {
        this.$notify({
          title: '失败',
          message: res.data,
          type: 'error'
        });
      }
    },
    open() {
      this.$confirm('确定发布此通告吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.releaseNotice()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消发布'
        });
      });
    },
    unOpen(){
      this.$confirm('确定取消发布此通告吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.unReleaseNotice()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    }
  },
  watch: {
    data (val) {
      if (this.data.status === "新建"){
        this.release = true
        this.unRelease = false
      }else{
        this.release = false
        this.unRelease = true
      }
    }
  },
  mounted() {
    if (this.data.status === "新建"){
      this.release = true
      this.unRelease = false
    }else{
      this.release = false
      this.unRelease = true
    }
  }
}

</script>
<style>
</style>