<!--
  @description: 专家-完善基本信息-教育信息
  @Modifier: 徐媛媛
  @routerPath: expert/expertEducation
  @date: 2020/8/12-18:20
-->
<template>
  <div>
    <div class="mb16">
      <div class="mb16 clearfix">
        <div class="fl">
          <el-button
            type="primary"
            icon="iconfont icon-tianjia1"
            @click="addEducationCondition('')"
          >
            添加
          </el-button>
        </div>
      </div>
      <el-form ref="forms" :model="formData" :rules="formData.rules" :inline-message="true">
        <div>
          <el-table key="index" :data="formData.projectBuyersList" class="tableLimit">
            <el-table-column type="index" label="序号" width="60" align="center" />
            <el-table-column header-align="center" min-width="200" align="center">
              <template slot="header">
                <span>毕业院校</span>
              </template>
              <template slot-scope="scope">
                <span>{{ scope.row.graduateSchool }}</span>
              </template>
            </el-table-column>
            <el-table-column header-align="center" min-width="200" align="center">
              <template slot="header">
                <span>所学专业</span>
              </template>
              <template slot-scope="scope">
                <span>{{ scope.row.specialty }}</span>
              </template>
            </el-table-column>
            <el-table-column header-align="center" min-width="200" align="center">
              <template slot="header">
                <span>学历</span>
              </template>
              <template slot-scope="scope">
                <span>{{ filterDictText(EducationList, scope.row.educationDvCode) }}</span>
                <!--                <span-->
                <!--                  v-if="scope.row.educationDvCode==='7'"-->
                <!--                >博士</span>-->
                <!--                <span v-else-if="scope.row.educationDvCode==='6'">硕士</span>-->
                <!--                <span v-else-if="scope.row.educationDvCode==='8'">其他</span>-->
                <!--                <span v-else-if="scope.row.educationDvCode==='4'">大专</span>-->
                <!--                <span v-else-if="scope.row.educationDvCode==='5'">本科</span>-->
              </template>
            </el-table-column>
            <el-table-column header-align="center" align="center" min-width="200">
              <template slot="header">
                <span>入学时间</span>
              </template>
              <template slot-scope="scope">
                <span>{{ scope.row.startDate }}</span>
              </template>
            </el-table-column>
            <el-table-column header-align="center" align="center" min-width="200">
              <template slot="header">
                <span>毕业时间</span>
              </template>
              <template slot-scope="scope">
                <span>{{ scope.row.endDate }}</span>
              </template>
            </el-table-column>
            <el-table-column header-align="center" label="操作" align="center" width="130">
              <template slot-scope="scope">
                <el-link
                  v-if="!scope.row.state"
                  type="primary"
                  @click="addEducationCondition(scope.row.id)"
                >
                  [编辑]
                </el-link>
                <el-link type="primary" @click="deleteRow(scope.$index,scope.row)">
                  [删除]
                </el-link>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-form>
      <!--分页-->
      <pagination :pagination-data="paginationData" @change="loadData" />
    </div>
    <!--保存按钮-->
    <div class="but_footer">
      <div class="but_footer-h">
        <div class="but_footer-but">
          <el-button @click="prevStep">
            上一步
          </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="confirmLoading" type="primary" @click="allSubmit">
            提交
          </el-button>
        </div>
      </div>
    </div>
    <!-- 添加教育信息弹窗 -->
    <addeducationalDialog
      v-if="dialogData.dialog"
      :dialog-data="dialogData"
      @dialogDataBuyersInfo="dialogDataBuyersInfo"
    />
  </div>
</template>
<script>
/* 接口*/
import { deleteExptExpertEducation, getExpertEducationById } from '@/api/expert/expertInfo' //  教育信息接口
import { deleteTBExpertEducationInfoById, queryAllTBExpertEducationInfoByApplyId } from '@/api/expert/expertChange'//  教育信息删除某个数据 查询专家所有教育经历信息
import { submitExpertApply } from '@/api/expert/expertInfo' //  教育信息接口
import { submitExpertChangeApply } from '@/api/expert/expertChange'//  教育信息提交接口
import pagination from '@comp/common/pagination/pagination' // 分页
/* 组件*/
import addeducationalDialog from './addeducational-dialog' // 添加教育信息弹窗
/* 公共方法*/
import { ListMixin } from '@/mixins/ListMixin'
import { mapGetters } from 'vuex'
import { expertInfo } from '@/views/expert/expertInfo/mixins/expertInfo'
import { filterDictText } from '@/assets/js/utils'// 常用方法
export default {
  name: 'ExperEducationAdd',
  components: {
    addeducationalDialog, // 选择任职经历弹窗
    pagination
  },
  mixins: [ListMixin, expertInfo],
  props: {
    projectData: {
      type: Object,
      default: function() {
        return {
          id: '',
          applyId: '',
          type: ''
        }
      }
    }
  },
  data() {
    return {
      confirmLoading: false, // 暂存、下一步Loding
      // 创建/增加的教育信息字段
      formData: {
        specialty: '', // 所学专业
        educationDvCode: '', // 学历
        startDate: '', // 入学时间
        graduateSchool: '', // 学校
        endDate: '', // 毕业时间
        credentialsId: '', // 证明文件
        projectBuyersList: [],
        id: ''
      },
      dialogData: {
        dialog: false, // 展示状态
        id: '' // 编辑时才会有财务状况id
      },
      list: [],
      fileUrl: '',
      pathData: {
        id: this.$route.query.id
      }
    }
  },
  computed: {
    ...mapGetters(['info'])
  },
  created() {
    this.loadData()
  },
  methods: {
    filterDictText,
    // 加载数据
    loadData() {
      const data = {
        userId: this.info.id,
        applyId: this.projectData.applyId
      }
      if (this.projectData.type === 'declarationChange') {
        queryAllTBExpertEducationInfoByApplyId(data).then(res => {
          this.formData.projectBuyersList = res.data.records
          this.formData.id = this.formData.projectBuyersList.id
          this.paginationData.pageNo = res.data.current
          this.paginationData.pageSize = res.data.size
          this.paginationData.total = res.data.total
        })
      } else {
        const expertId = this.$route.query.id
        if (expertId) {
          data.expertId = expertId
          delete data.userId
        }
        getExpertEducationById(data).then(res => {
          this.formData.projectBuyersList = res.data.records
          this.formData.id = this.formData.projectBuyersList.id
          this.paginationData.pageNo = res.data.current
          this.paginationData.pageSize = res.data.size
          this.paginationData.total = res.data.total
        })
      }
    },
    addEducationCondition(id) {
      this.dialogData = {
        dialog: true,
        id: id
      }
    },
    // 添加教育经历到表格
    dialogDataBuyersInfo() {
      this.paginationData.pageNo = 0
      this.paginationData.pageSize = 10
      this.loadData()
    },
    // 教育经历表格的编辑和保存
    editRow(row, index) {
      const data = {
        id: row.id
      }
      this.$refs.addeducationalDialog.formData = row
      this.$refs.addeducationalDialog.openOrganizationDialog('edit', data, index)
      this.loadData()
    },
    // 教育经历表格删除
    deleteRow(row, index, id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.projectData.type === 'declarationChange') {
          deleteTBExpertEducationInfoById(this.formData.projectBuyersList[row].id).then(res => {
            this.loadData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
        } else {
          deleteExptExpertEducation(this.formData.projectBuyersList[row].id).then(res => {
            this.loadData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 返回
    backList() {
      this.$router.push({
        path: '/expert/expertQuitInfoAudit-list'
      })
    },
    // 上一步
    prevStep() {
      this.$emit('stepPage', 'prev', this.pathData) // 步骤条切换方法 prev 上一步
    },
    //  下一步
    nextStep() {
      this.$refs['forms'].validate((valid, field) => {
        if (valid) {
          this.confirmLoading = false
          this.$emit('stepPage', 'next', this.pathData) // 步骤条切换方法 next下一步
        }
      })
    },
    //  提交
    allSubmit() {
      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',
                  query: {
                    metaData: this.$route.meta.type
                  }
                })
              }).catch(() => {})
            } else {
              submitExpertApply().then(res => {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.$router.push({
                  path: '/expert/perfectInfo',
                  query: {
                    metaData: this.$route.meta.type
                  }
                })
              }).catch(() => {})
            }
          }).catch(() => {})
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
/deep/.el-form-item--small.el-form-item {
  margin: 0;
}

.summaries {
  margin-right: 130px;
}
</style>
