<template>
  <div>
    <el-dialog
      v-loading="loading"
      :visible.sync="visible"
      :append-to-body="appendToBody"
      :before-close="beforeClose"
      title="推送"
      width="1100px"
      destroy-on-close>
      <el-form ref="form" :model="form" :rules="rules" label-width="200px" label-position="right" type="flex">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="APPOINT_CORP_ID" label="单位 ">
              <el-select v-model="corp_id" filterable placeholder="请选择单位 " style="width: 300px" @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-col>
          <el-col :span="12">
            <el-form-item prop="APPOINT_DEPARTMENT_ID" label="指定部门:">
              <Treeselect
                :options="departmentTree"
                :normalizer="normalizer"
                v-model="form.APPOINT_DEPARTMENT_ID"
                placeholder="请选择部门"
                no-options-text="暂无数据"
                no-children-text="暂无数据"
                style="width: 300px"
                @select="getPeopleList($event)"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item prop="APPOINT_USER_ID" label="指定审批人:">
              <el-select v-model="form.user" style="width: 300px" placeholder="请选择" @change="chooseUser">
                <el-option v-for="item in peopleList" :key="item.USER_ID" :value="JSON.stringify(item)" :label="item.NAME"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="APPOINT_ANNEX" label="承诺书:">
              <upload-file
                :file-list.sync="form.APPOINT_ANNEX"
                :multiple="false"
                :accept="'.pdf,.jpg,.png,doc,docx'"
                :limit="20"
                :size="1024"
                :upload-type="1"/>
            </el-form-item>
          </el-col>
        </el-row>

      </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'
import uploadFile from '../../../util/uploadFile/index.vue'
import { upload } from '@/utils/upload'

export default {
  components: { Treeselect, vueQr, uploadFile },
  props: {
    appendToBody: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      visible: false,
      jituanDanwei: '',
      loading: false,
      jituanDanweiTreeData: [],
      corp_id: null,
      corp_name: '',
      heirloom: {},
      listFlag: false,
      departmentTree: [],
      peopleList: [],
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.nodes
        }
      },
      form: {
        STATUS: null,
        APPOINT_CORP_ID: '',
        APPOINT_CORP_NAME: '',
        APPOINT_DEPARTMENT_ID: null,
        APPOINT_DEPARTMENT_NAME: '',
        APPOINT_USER_ID: '',
        APPOINT_USER_NAME: '',
        APPOINT_ANNEX: [],
        user: '',
        tm: new Date().getTime(),
        list: []
      },
      rules: {
        APPOINT_DEPARTMENT_ID: [
          { required: true, message: '请选择指定部门', trigger: 'change' }
        ],
        APPOINT_USER_ID: [
          { required: true, message: '请选择指定审批人', trigger: 'change' }
        ],
        APPOINT_CORP_ID: [
          { required: true, message: '请选择企业', trigger: 'change' }
        ],
        APPOINT_ANNEX: [
          { required: true, message: '请上传文件', trigger: 'change' }
        ]
      }
    }
  },
  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
        this.jituanDanweiTreeData.push({ CORPINFO_ID: '1', CORP_NAME: '秦皇岛港股份有限公司' })
      }).catch((e) => {
      })
    },
    getInfo(e) {
      const info = JSON.parse(e)
      this.form.APPOINT_CORP_ID = info.CORPINFO_ID
      this.form.APPOINT_CORP_NAME = info.CORP_NAME
      this.form.APPOINT_DEPARTMENT_ID = null
      this.form.APPOINT_DEPARTMENT_NAME = ''
      this.form.APPOINT_USER_ID = ''
      this.form.APPOINT_USER_NAME = ''
      this.form.user = ''
      this.getDepartmentTree()
    },
    beforeClose() {
      this.corp_id = null
      this.visible = false
      this.form = {
        STATUS: null,
        APPOINT_CORP_ID: '',
        APPOINT_CORP_NAME: '',
        APPOINT_DEPARTMENT_ID: null,
        APPOINT_DEPARTMENT_NAME: '',
        APPOINT_USER_ID: '',
        APPOINT_USER_NAME: '',
        user: '',
        tm: new Date().getTime(),
        list: []
      }
    },
    sendMessage() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          this.$message.error('请填写完整信息')
        } else {
          if (this.form.APPOINT_CORP_ID === this.form.APPOINT_DEPARTMENT_ID) {
            this.$message.error('不能选择主部门或主账号')
            return
          }

          this.form.list = this.heirloom
          const formData = new FormData()
          Object.keys(this.form).map(key => {
            formData.append(key, this.form[key])
          })
          for (let i = 0; i < this.form.APPOINT_ANNEX.length; i++) {
            formData.append('chenNuoShu', this.form.APPOINT_ANNEX[i].raw)
          }
          this.loading = true
          upload('/trainingbatch/sendMessage', formData
          ).then((data) => {
            if (data.code !== '0') {
              this.$message.error(data.msg)
            } else {
              this.$message.success('推送成功')
              this.visible = false
              this.$emit('refresh', '')
              this.beforeClose()
              this.loading = false
            }
          })
        }
      })
    },
    getDepartmentTree() {
      requestFN(
        '/companyApi/department/listTree',
        { CORPINFO_ID: this.form.APPOINT_CORP_ID }
      ).then((data) => {
        this.departmentTree = this.removeEmptyChildren(JSON.parse(data.zTreeNodes))
      }).catch((e) => {
        console.log(e)
      })
    },
    getPeopleList(e) {
      this.form.APPOINT_DEPARTMENT_NAME = e.name
      requestFN(
        '/companyApi/user/listAll',
        {
          DEPARTMENT_ID: e.id,
          CORPINFO_ID: JSON.parse(this.corp_id).CORPINFO_ID
        }
      ).then((data) => {
        this.form.APPOINT_USER_ID = ''
        this.form.APPOINT_USER_NAME = ''
        this.form.user = ''
        this.peopleList = data.userList
      }).catch((e) => {
        console.log(e)
      })
    },
    chooseUser(e) {
      const entity = JSON.parse(e)
      this.form.APPOINT_USER_ID = entity.USER_ID
      this.form.APPOINT_USER_NAME = entity.NAME
    }

  }
}
</script>

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