<template>
  <el-dialog
    v-if="visible"
    :visible.sync="visible"
    :before-close="handleClose"
    :append-to-body="true"
    title="培训申请"
    width="60%">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <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: [{ required: true, message: '安监部门不能为空', trigger: 'blur' }],
        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: {
    async init(e) {
      if (e) {
        this.users = e
      }
      this.visible = true
      this.loading = false
      this.form = {}
      /**
       * 获取相关方的 集团单位  corpinfo/getDataForEditByCorpInfoId
       * 根据集团单位 找到 对应的 企业id
       */
      await this.getCorpinfo()
    },
    getCorpinfo() {
      requestFN(
        '/corpinfo/getDataForEditByCorpInfoId',
        {
          CORPINFO_ID: JSON.parse(sessionStorage.getItem('user')).CORPINFO_ID,
          tm: new Date().getTime()
        }
      ).then((data) => {
        this.corpinfo = data.pd
        // 河港机械	jtdw002  1e6dbbe16004402f8d2c0e52afd9a676
        // 河港港工	jtdw003  3a854eefa7894e06aaa1a2611bca80f6
        // 河港检测	jtdw004  020578a4c1f04bc692ee25145c2efbe5
        // 方宇物业	jtdw005 90966974de3c4b83aca6f8fd6432d5c2

        if (data.pd.CORP_TYPE == 'jtdw002') {
          this.jituanDanwei = '1e6dbbe16004402f8d2c0e52afd9a676'
        }
        if (data.pd.CORP_TYPE == 'jtdw003') {
          this.jituanDanwei = '3a854eefa7894e06aaa1a2611bca80f6'
        }
        if (data.pd.CORP_TYPE == 'jtdw004') {
          this.jituanDanwei = '020578a4c1f04bc692ee25145c2efbe5'
        }
        if (data.pd.CORP_TYPE == 'jtdw005') {
          this.jituanDanwei = '90966974de3c4b83aca6f8fd6432d5c2'
        }
        this.getTreeData()
      })
    },
    // 获得部门树
    getTreeData() {
      requestFN(
        '/regulatoryApi/department/listTree',
        {}
      ).then((data) => {
        this.treeData = JSON.parse(data.zTreeNodes)
        // 必须指定【股份-安全监督部】
        this.form.SUPERVISION = 'c31058273a2d40f9b83dd8e5ae3a723c'
        this.form.MANAGER = this.corpinfo.MAIN_DEPARTMENT
        this.getRegulatoryUserList(this.form.SUPERVISION)
        this.getmanagerUserList(this.corpinfo.MAIN_DEPARTMENT)
      }).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(
        '/regulatoryApi/user/listAll',
        {
          DEPARTMENT_ID: DEPARTMENT_ID,
          ISASSESS: '1'
        }
      ).then((data) => {
        this.userList = data.userList
      }).catch((e) => {
      })
    },
    // 人员
    getUserList({ id: DEPARTMENT_ID }) {
      requestFN(
        '/companyApi/user/listAll',
        {
          DEPARTMENT_ID
        }
      ).then((data) => {
        this.companyUserList = data.userList
      }).catch((e) => {
      })
    },
    getmanagerUserList(DEPARTMENT_ID) {
      requestFN(
        '/regulatoryApi/user/listAll',
        {
          DEPARTMENT_ID: DEPARTMENT_ID
        }
      ).then((data) => {
        this.managercompanyUserList = data.userList
      }).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
          requestFN(
            '/trainingbatch/add',
            {
              ...this.form,
              users: this.users.join(',')
            }
          ).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('finish', '')
    },
    closeWindow() {
      this.handleClose()
    }
  }
}
</script>
