<template>
  <div>
    <el-dialog
      :visible.sync="visible"
      :append-to-body="appendToBody"
      :before-close="beforeClose"
      title="推送"
      width="500px"
      destroy-on-close>
      <el-form label-position="right" label-width="100px">
        <el-form-item prop="TERRITORIALITY" label="单位 ">
          <el-select v-model="corp_id" placeholder="请选择单位 " style="width: 90%" @change="getInfo">
            <el-option
              v-for="item in jituanDanweiTreeData"
              :key="item.CORPINFO_ID"
              :label="item.CORP_NAME"
              :value="JSON.stringify(item)"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">关 闭</el-button>
        <el-button type="primary" @click="sendMessage()">推 送</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import vueQr from 'vue-qr'
import Treeselect from '@riophae/vue-treeselect'
import { requestFN } from '@/utils/request'

export default {
  components: { Treeselect, vueQr },
  props: {
    appendToBody: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      visible: false,
      jituanDanwei: '',
      loading: false,
      jituanDanweiTreeData: [],
      corp_id: null,
      corp_name: '',
      heirloom: {},
      listFlag: false
    }
  },
  methods: {
    init(e) {
      this.visible = true
      this.listFlag = false
      this.getCorpinfo()
      this.heirloom = JSON.stringify(e)
    },
    getCorpinfo() {
      requestFN(
        '/corpinfo/getDataForEditByCorpInfoId',
        {
          CORPINFO_ID: JSON.parse(sessionStorage.getItem('user')).CORPINFO_ID,
          tm: new Date().getTime()
        }
      ).then((data) => {
        this.corpinfo = data.pd
        this.getTreeData()
      })
    },
    getTreeData() {
      requestFN(
        '/department/getPreventCrop',
      ).then((data) => {
        this.jituanDanweiTreeData = data.data
      }).catch((e) => {
      })
    },
    getInfo(e) {
      const info = JSON.parse(e)
      this.corp_name = info.CORP_NAME
    },
    beforeClose() {
      this.corp_id = null
      this.visible = false
    },
    sendMessage() {
      const corp_id = JSON.parse(this.corp_id).CORPINFO_ID
      requestFN(
        '/trainingbatch/sendMessage',
        {
          list: this.heirloom,
          corp_id: corp_id,
          corp_name: this.corp_name,
          tm: new Date().getTime()
        }
      ).then((data) => {
        if (data.code !== '0') {
          this.$message.error(data.msg)
        } else {
          this.$message.success('推送成功')
          this.visible = false
          this.$emit('refresh', '')
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
