<!--
  @description: 专家-专家成果
  @Modifier: 徐媛媛
  @routerPath:/expert/expertAchievements
  @date: 2020/8/12-13:27
-->
<template>
  <div>
    <el-form ref="forms" :model="formData" :rules="rules" :inline-message="true">
      <table class="from_table">
        <!--研究成果--->
        <tr>
          <td>
            <el-form-item label="研究成果" prop="researchResult" />
          </td>
          <td>
            <el-input
              v-model="formData.researchResult"
              type="textarea"
              placeholder="研究成果"
              style="width: 100%;"
            />
          </td>
        </tr>
        <!--主要业绩-->
        <tr>
          <td>
            <el-form-item label="主要业绩 " prop="mainAchievement" />
          </td>
          <td>
            <el-input
              v-model="formData.mainAchievement"
              type="textarea"
              placeholder="主要业绩"
              style="width: 100%;"
            />
          </td>
        </tr>
        <!--社会兼聘职情况--->
        <tr>
          <td>
            <el-form-item label="社会兼聘职情况" prop="concurrentPostDesc" />
          </td>
          <td>
            <el-input
              v-model.trim="formData.concurrentPostDesc"
              type="textarea"
              style="width: 100%;"
              placeholder="社会兼聘职情况"
              class="show-word-limit-padding"
            />
          </td>
        </tr>
        <!--其他需要说明的情况-->
        <tr>
          <td>
            <el-form-item label="其他需要说明的情况" prop="otherDesc" />
          </td>
          <td>
            <el-input
              v-model.trim="formData.otherDesc"
              type="textarea"
              maxlength="50"
              style="width: 100%;"
              placeholder="其他需要说明的情况"
              class="show-word-limit-padding"
            />
          </td>
        </tr>
      </table>
    </el-form>
    <!--保存按钮-->
    <div class="but_footer">
      <div class="but_footer-h">
        <div class="but_footer-but">
          <el-button @click="prevStep">
            上一步
          </el-button>
          <el-button :loading="preserLoading" @click="preser">
            保存
          </el-button>
          <el-button :loading="confirmLoading" type="primary" @click="nextStep">
            下一步
          </el-button>
          <el-button v-if="this.$route.query.id" type="primary" @click="backList">
            返回
          </el-button>
          <el-button v-else :loading="allSubmitLoading" type="primary" @click="allSubmit">
            提交
          </el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
/* 接口*/
import {
  saveOrUpdateExpertAchievement,
  getExpertAchievementById
} from '@/api/expert/expertAchievements' //  专家成果接口
import { submitExpertApply } from '@/api/expert/expertInfo' //  完善基本信息提交接口
import { submitExpertChangeApply, saveOrUpdateyExpertAchievementTitle, queryExpertAchievementByApplyId } from '@/api/expert/expertChange'//  完善基本信息提交接口
import { upLoadFileUrl } from '@/api/common/uploadCenter'
/* 公共方法*/
import { mapGetters } from 'vuex'
import { validateInput } from '@/assets/js/validate'
export default {
  name: 'BidWorkExperienceDialog',
  components: {
    // upload
  },
  props: {
    projectData: {
      type: Object,
      default: function() {
        return {
          id: '',
          applyId: '',
          type: ''
        }
      }
    }
  },
  data() {
    return {
      formData: {
        researchResult: '', // 研究成果
        otherDesc: '', // 其他补充说明
        mainAchievement: '', // 主要业绩
        concurrentPostDesc: '', // 社会兼聘职情况
        projectBuyersList: [],
        userId: '', // 用户id
        id: '' // 唯一id
      },
      rules: {
        researchResult: [{ required: true, validator: validateInput, trigger: 'blur' }], // 研究成果
        otherDesc: [{ required: true, validator: validateInput, trigger: 'blur' }], // 其他补充说明
        mainAchievement: [{ required: true, validator: validateInput, trigger: 'blur' }], // // 主要业绩
        concurrentPostDesc: [
          { required: true, validator: validateInput, trigger: 'blur' }
        ] // 社会兼聘职情况
      },
      confirmLoading: false,
      preserLoading: false,
      allSubmitLoading: false,
      idImportFileUrl: upLoadFileUrl,
      pathData: {
        id: this.$route.query.id
      }
    }
  },
  computed: {
    ...mapGetters(['info'])
  },
  created() {
    this.geAchievementById()
  },
  methods: {
    // 提交信息
    nextStep() {
      this.$refs['forms'].validate((valid, field) => {
        if (valid) {
          this.confirmLoading = false // btnLoding
          const userId = this.info.id
          this.formData.userId = userId
          const data = { ...this.formData }
          if (this.projectData.type === 'declarationChange') {
            saveOrUpdateyExpertAchievementTitle(data).then(res => {
              this.confirmLoading = false
              this.$emit('stepPage', 'next', this.pathData) // 步骤条切换方法 next下一步
            }).catch(() => {
              this.confirmLoading = true // btnLoding
            })
          } else {
            const expertId = this.$route.query.id
            if (expertId) {
              data.expertId = expertId
              delete data.userId
            }
            saveOrUpdateExpertAchievement(data).then(res => {
              this.confirmLoading = false
              this.$emit('stepPage', 'next', this.pathData) // 步骤条切换方法 next下一步
            }).catch(() => {
              this.confirmLoading = true // btnLoding
            })
          }
        }
      })
    },
    //  提交所有页面信息
    allSubmit() {
      this.allSubmitLoading = false // btnLoding
      this.$refs['forms'].validate((valid, field) => {
        if (valid) {
          this.$confirm('确认提交审核吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            if (this.projectData.type === 'declarationChange') {
              submitExpertChangeApply().then(res => {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.$router.push({ path: '/expert/perfectInfo/expertEssentialInfo' })
              }).catch(() => {})
            } else {
              submitExpertApply().then(res => {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.$router.push({ path: '/expert/perfectInfo/expertEssentialInfo' })
              }).catch(() => {})
            }
          }).catch(() => {})
        }
      })
    },
    preser() {
      this.$refs['forms'].validate((valid, field) => {
        if (valid) {
          const userId = this.info.id
          this.formData.userId = userId
          const data = { ...this.formData }
          if (this.projectData.type === 'declarationChange') {
            saveOrUpdateyExpertAchievementTitle(data).then(res => {
              this.confirmLoading = false
              this.formData.id = res.data
              this.$message.success('保存成功')
            })
          } else {
            const expertId = this.$route.query.id
            if (expertId) {
              data.expertId = expertId
              delete data.userId
            }
            saveOrUpdateExpertAchievement(data).then(res => {
              this.confirmLoading = false
              this.formData.id = res.data
              this.$message.success('保存成功')
            })
          }
        }
      })
    },
    geAchievementById() {
      const data = {
        userId: this.info.id,
        applyId: this.projectData.applyId
      }
      if (this.projectData.type === 'declarationChange') {
        queryExpertAchievementByApplyId(data).then(res => {
          this.formData.id = res.data.id
          this.formData = res.data
        }).catch(() => {})
      } else {
        const expertId = this.$route.query.id
        if (expertId) {
          data.expertId = expertId
          delete data.userId
          delete data.applyId
        }
        getExpertAchievementById(data).then(res => {
          this.formData.id = res.data.id
          this.formData = res.data
        }).catch(() => {})
      }
    },
    // 返回
    backList() {
      this.$router.push({
        path: '/expert/expertQuitInfoAudit-list'
      })
    },
    // 上一步
    prevStep() {
      this.$emit('stepPage', 'prev', this.pathData) // 步骤条切换方法 prev 上一步
    }
  }
}
</script>

<style scoped lang="less">
.el-dialog__body {
  height: 55vh;
}
</style>
