<!--
  @description: 专家-完善基本信息-任职经历
  @Modifier: 徐媛媛
  @routerPath: /expert/expertWorkPerience
  @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="addWorkPerience('')"
          >
            添加
          </el-button>
        </div>
      </div>
      <el-form ref="form" :model="formData" :rules="formData.rules" :inline-message="true">
        <div>
          <el-table :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="left">
              <template slot="header">
                <span>单位名称</span>
              </template>
              <template slot-scope="scope">
                <span>{{ scope.row.comName }}</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.startDate }}</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.postName }}</span>
              </template>
            </el-table-column>
            <el-table-column header-align="center" 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" label="操作" align="center" width="130">
              <template slot-scope="scope">
                <el-link
                  v-if="!scope.row.state"
                  type="primary"
                  @click="addWorkPerience(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>
    <!-- 添加任职经历弹窗 -->
    <workExperienceDialog
      v-if="workPerienceData.dialog"
      :data="workPerienceData"
      :project-data="projectData"
      @workPerienceInfo="workPerienceInfo"
    />
  </div>
</template>

<script>
// 混入公共方法
import { ListMixin } from '@/mixins/ListMixin'
/* 组件*/
import workExperienceDialog from './workExperience-dialog' // 添加任职经历弹窗
import pagination from '@comp/common/pagination/pagination' // 分页
/* api */
import { getExpertCareerById, deleteExptExpertCareer } from '@/api/expert/expertInfo' // 获取用户任职经历信息 提交
import { submitExpertApply } from '@/api/expert/expertInfo' //  提交所有信息接口
import {
  deleteTBExpertCareerHistorById,
  submitExpertChangeApply,
  queryAllTBExpertCareerHistoryForUpdate
} from '@/api/expert/expertChange' //  变更删除  提交所有信息接口
import { mapGetters } from 'vuex'
export default {
  name: 'ExpertWorkPerience',
  components: {
    workExperienceDialog, // 选择任职经历弹窗
    pagination // 页签
  },
  mixins: [ListMixin],
  props: {
    projectData: {
      type: Object,
      default: function() {
        return {
          id: '',
          applyId: '',
          type: ''
        }
      }
    }
  },
  data() {
    return {
      confirmLoading: false, // 暂存、下一步Loding
      // 创建/增加的任职经历信息字段
      formData: {
        comCode: '', // 组织机构代码
        startDate: '', // 开始时间startDate
        email: '', // 邮箱
        postName: '', // 职务 postName
        specialty: '', // 从事专业specialty
        comName: '', // 单位名称
        telOffice: '', // 单位电话
        isAvoidCompany: '0', // 回避单位
        projectBuyersList: [], // 任职经历表格
        state: true, // 设置表格为可编辑状态
        id: '', // 编辑删除所需要用户id
        index: '' // 下表
      },
      workPerienceData: {
        dialog: false, // 弹框展示状态
        id: '', // 证书id-编辑时才会有证书id
        oldId: '' // 修改时用的Id
      },
      fileUrl: '',
      records: [],
      pathData: {
        id: this.$route.query.id
      }
    }
  },
  computed: {
    ...mapGetters(['info'])
  },
  created() {
    this.loadData()
  },
  methods: {
    // 加载数据
    loadData() {
      const data = {
        userId: this.info.id,
        applyId: this.projectData.applyId
      }
      this.loading = true
      if (this.projectData.type === 'declarationChange') {
        queryAllTBExpertCareerHistoryForUpdate({ ...data, ...this.paginationData })
          .then(res => {
            this.formData.projectBuyersList = res.data.records
            // this.formData.id = res.data.records[0].expertId
            this.paginationData.pageNo = res.data.current
            this.paginationData.pageSize = res.data.size
            this.paginationData.total = res.data.total
          })
          .catch(() => {
            this.loading = false
          })
      } else {
        const expertId = this.$route.query.id
        if (expertId) {
          data.expertId = expertId
          delete data.userId
          delete data.applyId
        }
        getExpertCareerById({ ...data, ...this.paginationData })
          .then(res => {
            this.formData.projectBuyersList = res.data.records
            // this.formData.id = res.data.records[0].expertId
            this.paginationData.pageNo = res.data.current
            this.paginationData.pageSize = res.data.size
            this.paginationData.total = res.data.total
          })
          .catch(() => {
            this.loading = false
          })
      }
    },
    // // 打开添加任职经历弹窗
    addWorkPerience(id) {
      this.workPerienceData = {
        dialog: true,
        id: id
      }
    },
    // 任职经历表格的编辑和保存
    editRow(row, index) {
      const data = {
        id: row.id
      }
      this.$refs.bidWorkExperienceDialog.formData = row
      this.$refs.bidWorkExperienceDialog.openOrganizationDialog('edit', data, index)
      this.loadData()
    },
    // 添加证书/编辑证书-回填
    workPerienceInfo(val) {
      this.paginationData.pageNo = 0
      this.paginationData.pageSize = 10
      this.loadData()// 获取数据
    },
    // 任职经历表格删除
    deleteRow(row, index, id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = this.formData.projectBuyersList[row].id
        const data = {
          id: ids
        }
        if (this.projectData.type === 'declarationChange') {
          deleteTBExpertCareerHistorById(data).then(res => {
            this.loadData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
        } else {
          deleteExptExpertCareer(data).then(res => {
            this.loadData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 上一步
    prevStep() {
      this.$emit('stepPage', 'prev', this.pathData) // 步骤条切换方法 prev 上一步
    },
    //  下一步
    nextStep() {
      this.$refs.form.validate((valid, field) => {
        if (valid) {
          this.confirmLoading = true
          this.$emit('stepPage', 'next', this.pathData)
        } else {
          this.$message.success('请填写任职经历')
        }
      })
    },
    // 返回
    backList() {
      this.$router.push({
        path: '/expert/expertQuitInfoAudit-list'
      })
    },
    //  提交
    allSubmit() {
      this.$refs['form'].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>
