<template>
  <el-dialog
    v-if="visible"
    :visible.sync="visible"
    :append-to-body="true"
    title="培训申请"
    width="60%">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row v-if="false">
        <el-col :span="12">
          <el-form-item
            prop="TERRITORIALITY"
            label="集团单位 "
          >
            <Treeselect
              :options="jituanDanweiTreeData"
              :normalizer="normalizer"
              v-model="form.TERRITORIALITY"
              placeholder="请选择集团单位 "
              no-options-text="暂无数据"
              no-children-text="暂无数据"
              style="width: 100%;"
              @select="getUserList($event)"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item
            prop="TERRITORIALITY_USER_ID"
            label="审核人员"
          >
            <div style="display:flex;justify-content: space-between;align-items: flex-start;">
              <el-select v-model="form.TERRITORIALITY_USER_ID">
                <el-option
                  v-for="item in companyUserList"
                  :key="item.USER_ID"
                  :label="item.NAME"
                  :value="item.USER_ID"/>
              </el-select>
            </div>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="12">
          <el-form-item
            prop="MANAGER"
            label="主管部门"
          >
            <Treeselect
              :options="treeData"
              :normalizer="normalizer"
              v-model="form.MANAGER"
              :disabled="true"
              placeholder="请选择主管部门"
              no-options-text="暂无数据"
              no-children-text="暂无数据"
              style="width: 100%;"
              @select="getmanagerUserList($event)"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item
            prop="MANAGER_USER_ID"
            label="审核人员"
          >
            <div style="display:flex;justify-content: space-between;align-items: flex-start;">
              <el-select v-model="form.MANAGER_USER_ID">
                <el-option
                  v-for="item in managercompanyUserList"
                  :key="item.USER_ID"
                  :label="item.NAME"
                  :value="item.USER_ID"/>
              </el-select>
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item
            prop="SUPERVISION"
            label="安监部门"
          >
            <Treeselect
              :options="treeData"
              :normalizer="normalizer"
              v-model="form.SUPERVISION"
              :disabled="true"
              placeholder="请选择安监部门"
              no-options-text="暂无数据"
              no-children-text="暂无数据"
              style="width: 100%;"
            />
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item
            prop="SUPERVISION_USER_ID"
            label="审核人员"
          >
            <div style="display:flex;justify-content: space-between;align-items: flex-start;">
              <el-select v-model="form.SUPERVISION_USER_ID">
                <el-option v-for="item in userList" :key="item.USER_ID" :label="item.NAME" :value="item.USER_ID"/>
              </el-select>
            </div>
          </el-form-item>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input :rows="3" v-model="form.DESCR" type="textarea" placeholder="这里输入备注..."/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div slot="footer" class="dialog-footer">
      <el-button @click="closeWindow">取 消</el-button>
      <el-button :loading="loading" type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { requestFN } from '@/utils/request'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: {
    Treeselect
  },
  props: {
    corpInfo: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      visible: false,
      loading: false,
      treeData: [],
      companyTreeData: [],
      corpinfo: {},
      userList: [],
      companyUserList: [],
      managercompanyUserList: [],
      jituanDanwei: '',
      jituanDanweiTreeData: [],
      normalizer(node) {
        return {
          id: node.id,
          label: node.name,
          children: node.nodes
        }
      },
      form: {},
      rules: {
        SUPERVISION_USER_ID: [{ required: true, message: '审核人员不能为空', trigger: 'blur' }],
        MANAGER: [{ required: true, message: '主管部门不能为空', trigger: 'blur' }],
        MANAGER_USER_ID: [{ required: true, message: '审核人员不能为空', trigger: 'blur' }],
        TERRITORIALITY: [{ required: true, message: '属地管理部门不能为空', trigger: 'blur' }],
        TERRITORIALITY_USER_ID: [{ required: true, message: '审核人员不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(e) {
      if (e) {
        this.users = e
      }
      // 判断是否有主管部门
      for (let i; this.users.length > 0; i++) {
        if (!this.users[0].MANAGER_DEPARTMENT_ID || this.users[0].MANAGER_DEPARTMENT_ID === '') {
          this.$message({
            message: '系统未指定主管部门，请将完善相关方信息',
            type: 'error'
          })
          return false
        }
      }
      // 判断主管部门是否一致
      const warden = this.users[0].MANAGER_DEPARTMENT_ID
      for (let i; this.users.length > 0; i++) {
        if (warden !== this.users[i].MANAGER_DEPARTMENT_ID) {
          this.$message({
            message: '选择的人员(' + this.users[i].NAME + ')主管部门不一致，请重新选择',
            type: 'error'
          })
          return false
        }
      }
      this.visible = true
      this.loading = false
      requestFN(
        '/openApi/department/listTree',
        { CORPINFO_ID: JSON.parse(sessionStorage.getItem('user')).CORPINFO_ID, tm: new Date().getTime() }
      ).then((data) => {
        this.form.MANAGER = this.users[0].MANAGER_DEPARTMENT_ID
        this.jituanDanweiTreeData = JSON.parse(data.zTreeNodes)
        this.getTreeData()
      })
    },
    // 获得部门树
    getTreeData() {
      requestFN(
        '/openApi/corpDepartment/listTree',
        {}
      ).then((data) => {
        this.treeData = JSON.parse(data.zTreeNodes)
        // 必须指定【股份-安全监督部】
        this.form.SUPERVISION = 'c31058273a2d40f9b83dd8e5ae3a723c'
        this.getRegulatoryUserList(this.form.SUPERVISION)
        this.getmanagerUserList(this.form.MANAGER)
      }).catch((e) => {
        console.log(e)
      })

      if (this.jituanDanwei) {
        requestFN(
          '/companyApi/department/listTree',
          { CORPINFO_ID: this.jituanDanwei }
        ).then((data) => {
          this.jituanDanweiTreeData = JSON.parse(data.zTreeNodes)
        }).catch((e) => {
        })
      }
    },
    getRegulatoryUserList(DEPARTMENT_ID) {
      requestFN(
        '/user/getRegUserInfo',
        {
          DEPARTMENT_ID: DEPARTMENT_ID,
          ISASSESS: '1'
        }
      ).then((data) => {
        this.userList = data.list
      }).catch((e) => {
      })
    },
    // 人员
    getUserList({ id: DEPARTMENT_ID }) {
      requestFN(
        '/user/listAll',
        { DEPARTMENT_ID: DEPARTMENT_ID }
      ).then((data) => {
        this.companyUserList = data.userList
      }).catch((e) => {
      })
    },
    getmanagerUserList(DEPARTMENT_ID) {
      requestFN(
        '/user/getRegUserInfo',
        {
          DEPARTMENT_ID: DEPARTMENT_ID
        }
      ).then((data) => {
        this.managercompanyUserList = data.list
      }).catch((e) => {
      })
    },
    save() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          this.form.USER_COUNT = this.users.length
          this.form.AUDIT_STATUS = 0
          this.form.TYPE = 2
          const userIds = this.users.map((item) => { return item.XGF_USER_ID })
          requestFN(
            '/trainingbatch/add',
            {
              ...this.form,
              users: userIds.join(','),
              BELONG_TO_CORP: this.users[0].BELONG_TO_CORP,
              BELONG_TO_CORP_NAME: this.users[0].BELONG_TO_CORP_NAME
            }
          ).then((data) => {
            this.loading = false
            this.$message({
              message: '申请成功',
              type: 'success'
            })
            this.handleClose()
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },
    handleClose() {
      this.visible = false
      this.$emit('refresh', '')
    },
    closeWindow() {
      this.visible = false
    }
  }
}
</script>
