<template>
  <div style="height: 100%;" v-loading="loading">
    <el-steps :space="200" :active="step" align-center finish-status="success">
      <el-step title="步骤 1" description="转入单位先与转出单位电话沟通"></el-step>
      <el-step title="步骤 2" description="转入单位提出申请(勾选与转出单位沟通)"></el-step>
      <el-step title="步骤 3" description="转出单位确认已与转入单位电话沟通"></el-step>
      <el-step title="步骤 4" description="转入单位提交转入申请表"></el-step>
      <el-step title="步骤 5" description="转出单位提交转出申请表"></el-step>
      <el-step title="步骤 6" description="北京监理委员会审核"></el-step>
      <el-step title="步骤 7" description="北京监理委员会同意变更"></el-step>
    </el-steps>
    <div
      style="background-color:rgb(235, 235, 235);display: flex;align-items: center;justify-content: center;font-size: 16px;font-weight: bold;padding: 16px;margin: 24px 0;">
      铁路建设监理人员证书变更申请表单
    </div>
    <div style="height: calc(100% - 260px);overflow-y: auto;">
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="聘用单位名称">
          <el-input v-model="form.newdeptname" readonly placeholder="聘用单位名称" style="width: 400px;"></el-input>
        </el-form-item>
        <el-form-item label="聘用单位联系方式" prop="newphone">
          <el-input v-model="form.newphone" :readonly="step > 1 || wancheng == true" placeholder="聘用单位联系方式"
            style="width: 400px;"></el-input>
        </el-form-item>
        <el-form-item label="监理人员身份证号" prop="usercord">
          <el-input v-model="form.usercord" :readonly="id != null" @focus="clearGetInfo" @blur="getPeoInfo"
            placeholder="监理人员身份证号" style="width: 400px;"></el-input>
        </el-form-item>
        <el-form-item label="监理人员姓名">
          <el-input v-model="form.username" readonly placeholder="监理人员姓名" style="width: 400px;"></el-input>
        </el-form-item>
        <el-form-item label="监理员证号">
          <el-input v-model="form.jlyzh" readonly placeholder="监理员证号" style="width: 400px;"></el-input>
        </el-form-item>
        <el-form-item label="监理工程师证号">
          <el-input v-model="form.gcszh" readonly placeholder="监理工程师证号" style="width: 400px;"></el-input>
        </el-form-item>
        <el-form-item label="总监理证号">
          <el-input v-model="form.zjzh" readonly placeholder="总监理证号" style="width: 400px;"></el-input>
        </el-form-item>
        <el-form-item label="现所在单位名称">
          <el-input v-model="form.olddeptname" readonly placeholder="现所在单位名称" style="width: 400px;"></el-input>
        </el-form-item>
        <el-form-item label="" label-width="0" prop="newgt" style="margin-left: 40px;">
          <div style="width: 1000px;">
            <el-radio v-model="form.newgt" label="1">已与转出单位沟通</el-radio>
          </div>
        </el-form-item>
        <el-form-item label="申请表下载" v-if="step > 2">
          <div style="width: 900px;">
            <!-- <a href="../../../assets/files/铁路建设监理人员证书变更申请表.doc"></a> -->
            <div class="xiazai" @click="xiazai">
              铁路建设监理人员证书变更申请表.doc
            </div>
          </div>
        </el-form-item>
        <el-form-item label="申请表扫描件" prop="zhuanruImg" v-if="step > 2">
          <div style="width: 400px;">
            <ImgUpload v-model="form.zhuanruImg" v-if="step == 3 && qyId == form.newdeptid" :limit="1"></ImgUpload>
            <ImgUpload v-model="form.zhuanruImg" v-else :readonly="true" :limit="1"></ImgUpload>
          </div>
        </el-form-item>
        <el-form-item label="转出申请表扫描件" prop="zhuanchuImg" v-if="step > 3">
          <div style="width: 400px;">
            <ImgUpload v-model="form.zhuanchuImg" v-if="step == 4 && qyId == form.olddeptid" :limit="1"></ImgUpload>
            <ImgUpload v-model="form.zhuanchuImg" v-else :readonly="true" :limit="1"></ImgUpload>
          </div>
        </el-form-item>
        <el-form-item label="" prop="oldgt" label-width="0px"
          v-if="step == 2 && qyId == form.olddeptid || step > 2 || (step == 1 && form.oldshyj != '' && form.oldshyj != null)"
          style="margin-left: 40px;">
          <div style="width: 1000px;">
            <el-radio-group v-model="form.oldgt">
              <el-radio v-for="(v, i) in dict.type.bg_reject_type" :label="v.value" :key="i"
                :disabled="(step != 2 && form.oldgt != v.value) || wancheng == true">{{ v.label }}</el-radio>
            </el-radio-group>
          </div>
        </el-form-item>
        <el-form-item label="上传附件" prop="rejectfile"
          v-if="(step == 2 && qyId == form.olddeptid || step > 2 || step == 1) && (form.oldgt == 2 || form.oldgt == 3)">
          <div style="width: 400px;">
            <ImgUpload v-model="form.rejectfile" v-if="step == 2 && qyId == form.olddeptid" :limit="1"></ImgUpload>
            <ImgUpload v-model="form.rejectfile" v-else :readonly="true" :limit="1"></ImgUpload>
          </div>
        </el-form-item>
        <el-form-item label="原因(转出单位)"
          v-if="(step == 2 && form.olddeptid == qyId) || (step == 4 && form.olddeptid == qyId) || (form.oldshyj != '' && form.oldshyj != null)">
          <div style="width: 900px;">
            <Editor2 v-model="form.oldshyj" :typeInfo="true"
              v-if="form.olddeptid == qyId && (form.oldshenhe == 3 || (form.oldshenhe == 4 && form.zhuanchusqbtime != null && form.zhuanchusqbtime != ''))">
            </Editor2>
            <div v-html="form.oldshyj" v-else>

            </div>
          </div>
        </el-form-item>
        <el-form-item label="原因(监理委员会)" v-if="form.glyshyj != '' && form.glyshyj != null && form.glyshenhe == 2">
          <div style="width: 900px;">
            <div v-html="form.glyshyj">

            </div>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <div style="display: flex;align-items: center;justify-content: center;">
      <el-button type="primary" v-if="step == 1 && id == null" @click="submit">提 交</el-button>
      <el-button type="primary" v-else-if="step == 1 && id != null && qyId == form.newdeptid" @click="chongfuSubmit">提
        交</el-button>
      <el-button type="primary" v-if="step == 2 && qyId == form.olddeptid" :disabled="form.oldgt == 1 ? false : true"
        @click="zhuanchuTg">通 过</el-button>
      <el-button type="warning" v-if="step == 2 && qyId == form.olddeptid" :disabled="form.oldgt == 1 ? true : false"
        @click="zhuanchuBtg">不通过</el-button>
      <el-button type="primary"
        v-if="step == 3 && qyId == form.newdeptid && (form.zhuanchusqbtime == '' || form.zhuanchusqbtime == null)"
        @click="zhuanruTijiao">提 交</el-button>
      <el-button type="primary"
        v-if="step == 4 && qyId == form.olddeptid && form.zhuanchusqbtime != '' && form.zhuanchusqbtime != null"
        @click="zhuanchuTijiao">通 过</el-button>
      <el-button type="warning"
        v-if="step == 4 && qyId == form.olddeptid && form.zhuanchusqbtime != '' && form.zhuanchusqbtime != null"
        @click="zhuanchuBtg">不通过</el-button>
      <el-button type="primary" v-if="step == 5 && roles.includes('xiehui')" @click="xiehuiTg">通 过</el-button>
      <el-button type="warning" v-if="step == 5 && roles.includes('xiehui')" @click="xiehuiBtg">不通过</el-button>
    </div>
  </div>
</template>

<script>
import { getInfoByidCard, submitZhuanru, getQyBiangeng, zhuanruSubmit, zhuanchuSubmit, xiehuiSubmit } from "../../../api/xiehui/jlrybg/qyBiangeng.js"
import Editor1 from "../../../components/Editor1.vue";
import Editor2 from "../../../components/Editor2.vue";

export default {
  name: "bgForm",
  components: {
    Editor1, Editor2
  },
  props: {
    id: {
      type: Number,
      default: null
    },
    wancheng: {
      type: Boolean,
      default: false
    }
  },
  dicts: ['bg_reject_type'],
  data() {
    return {
      form: {
        newdeptid: null,//现聘单位id 登录企业id
        olddeptid: null,//旧单位id  身份证查询返回的DeptID
        newdeptname: null,//现聘单位名称 登录的企业名称
        olddeptname: null,//旧单位名称  身份证查询返回的Dept
        gcszh: null,//监理工程师证号  身份证查询返回的JLZhengHao
        usercord: null,//监理人身份证号
        username: null,//监理人员姓名
        newgt: null,//现聘单位：已与转出单位沟通  1
        jlyzh: null,//监理员证号 身份证查询返回的 JLYZhengHaoNum
        newphone: null,//现聘单位联系人
        zjzh: null,//总监证号 身份证查询返回的 ZJZhengHao
        oldgt: null,//转出单位是否沟通
        oldshenhe: null,//转出企业审核结果
        oldshyj: null,//转出单位拒绝意见
        zhuanchusqbtime: null,
        zhuanchuImg: null,
        zhuanruImg: null,
        rejectfile: null,//审核不通过附件
      },
      ti: null,
      rules: {
        usercord: [
          { required: true, message: '请输入监理人身份证号', trigger: 'blur' },
        ],
        newphone: [
          { required: true, message: '请输入聘用单位联系方式', trigger: 'blur' },
        ],
        newgt: [
          { required: true, message: '请勾选已与转出单位沟通', trigger: 'change' },
        ],
        oldgt: [
          { required: true, message: '请勾选', trigger: 'change' },
        ],
        zhuanchuImg: [
          { required: true, message: '请上传转入申请表', trigger: 'change' },
        ],
        zhuanruImg: [
          { required: true, message: '请上传转出申请表', trigger: 'change' },
        ],
        rejectfile: [
          { required: true, message: '请上传附件', trigger: 'change' },
        ]
      },
      //当前步骤
      step: 1,
      //加载状态
      loading: false,
      //企业Id
      qyId: null,
      roles: null,
    }
  },
  created() {
    this.loading = true
    this.qyId = this.$store.getters.userInfo.id
    this.roles = this.$store.getters.roles
    if (this.id != null) {
      this.getBgInfo()
    } else {
      this.form.newdeptid = this.$store.getters.userInfo.id
      this.form.newdeptname = this.$store.getters.userInfo.qyname
      this.form.newphone = this.$store.getters.userInfo.photo
      this.loading = false
    }
  },
  beforeDestroy() {
    this.form = {}
  },
  methods: {
    // 查询人员信息开始
    clearGetInfo() {
      clearTimeout(this.ti)
    },
    getPeoInfo() {
      this.$refs.form.validateField('usercord', (qwe) => {
        if (!qwe) {
          if (this.id == null) {
            this.ti = setTimeout(() => {
              getInfoByidCard(this.form.usercord).then(res => {
                this.form.olddeptname = res.data.Dept
                this.form.olddeptid = res.data.DeptID
                this.form.gcszh = res.data.JLZhengHao
                this.form.username = res.data.Name
                this.form.jlyzh = res.data.JLYZhengHaoNum
                this.form.zjzh = res.data.ZJZhengHao
              })
            }, 2000)
          }
        }
      })
    },
    // 查询人员信息结束
    //查询变更信息
    async getBgInfo() {
      const res = await getQyBiangeng(this.id)
      this.form = { ...res.data }
      if (this.form.oldshenhe == 1 && this.form.glyshtime == null) {
        this.step = 5
      } else if (this.form.oldshenhe == 2) {
        this.step = 1
      } else if (this.form.oldshenhe == 3) {
        this.step = 2
      } else if (this.form.oldshenhe == 4 && (this.form.zhuanchusqbtime == '' || this.form.zhuanchusqbtime == null)) {
        this.step = 3
      } else if (this.form.oldshenhe == 4 && (this.form.zhuanchusqbtime !== "" && this.form.zhuanchusqbtime != null)) {
        this.step = 4
      } else if (this.form.oldshenhe == 1 && this.form.glyshtime != null && this.form.bgflg != 1) {
        this.step = 6
      } else if (this.form.bgflg == 1) {
        this.step = 7
      } else {
        this.step = 2
      }
      this.loading = false
    },
    //提交
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.step == 1) {
            submitZhuanru(this.form).then(res => {
              this.$message({
                type: "success",
                message: "提交申请成功，定等待确认"
              })
              this.$emit('success', false)
            })
          }
        } else {

        }
      });
    },
    //转出公司通过审核
    zhuanchuTg() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          zhuanchuSubmit(this.form).then(res => {
            this.$message({
              type: "success",
              message: "审核通过"
            })
            this.$emit('success', false)
          })
        } else {

        }
      });
    },
    //转出公司不通过审核
    zhuanchuBtg() {
      if (this.form.oldgt == null || this.form.oldgt == '') {
        this.$message({
          type: "warning",
          message: "请勾选转入单位是否与我单位沟通"
        })
      } else if ((this.form.oldgt == 2 || this.form.oldgt == 3) && this.form.rejectfile == null) {
        this.$message({
          type: "warning",
          message: "请上传附件"
        })
      } else if (this.form.oldshyj == null || this.form.oldshyj == "") {
        this.$message({
          type: "warning",
          message: "请填写原因"
        })
      } else {
        zhuanchuSubmit({ ...this.form, oldshenhe: 2 }).then(res => {
          this.$message({
            type: "error",
            message: "审核不通过"
          })
          this.$emit('success', false)
        })
      }
      // this.$refs.form.validate((valid) => {
      // 	if (valid) {
      // 		if(this.form.oldshyj == null || this.form.oldshyj == ""){
      // 			this.$message({
      // 				type:"warning",
      // 				message:"请填写审核意见"
      // 			})
      // 		}else{
      // 			zhuanchuSubmit({...this.form,oldshenhe:2}).then(res=>{
      // 				this.$message({
      // 					type:"error",
      // 					message:"审核不通过"
      // 				})
      // 				this.$emit('success',false)
      // 			})
      // 		}
      // 	} else {

      // 	}
      // });
    },
    //转入单位提交
    zhuanruTijiao() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          zhuanruSubmit(this.form).then(res => {
            this.$message({
              type: "success",
              message: "提交成功"
            })
            this.$emit('success', false)
          })
        } else {

        }
      });
    },
    //转出单位提交
    zhuanchuTijiao() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          zhuanchuSubmit(this.form).then(res => {
            this.$message({
              type: "success",
              message: "提交成功"
            })
            this.$emit('success', false)
          })
        } else {

        }
      });
    },
    //协会审核通过
    xiehuiTg() {
      xiehuiSubmit({ glyshenhe: 1, id: this.id }).then(res => {
        this.$message({
          type: "success",
          message: "审核通过"
        })
        this.$emit('success', false)
      })
    },
    //协会审核不通过
    xiehuiBtg() {
      this.$prompt('请输入原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: (value) => {
          console.log(value)
          if (!value) {
            return false
          }
        },
        inputErrorMessage: '原因不能为空'
      }).then(({ value }) => {
        xiehuiSubmit({ glyshenhe: 2, id: this.id, glyshyj: value }).then(res => {
          this.$message({
            type: "error",
            message: "审核不通过",
          })
          this.$emit('success', false)
        })
      }).catch(() => {

      });
    },
    //重复提交申请
    chongfuSubmit() {
      zhuanruSubmit({ id: this.id }).then(res => {
        this.$message({
          type: "success",
          message: "提交成功"
        })
        this.$emit('success', false)
      })
    },
    xiazai() {
      // this.$download.saveAs("../../../assets/files/铁路建设监理人员证书变更申请表.doc", "铁路建设监理人员证书变更申请表.doc");
      this.download('common/download', {
        fileName: '/zs/铁路建设监理人员证书变更申请表.doc', delete: false
      }, `铁路建设监理人员证书变更申请表.doc`)
    }

  }
}
</script>

<style lang="scss" scoped>.xiazai:hover {
  cursor: pointer;
  color: #409EFF;
  text-decoration: underline;
}</style>
