<!--
  @description: 专家-完善基本信息-职称信息
  @Modifier: 徐媛媛
  @routerPath: /expert/expertTitleInfo
  @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="selectBuyersShowDialogBtn()"
          >
            添加
          </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.certificateName }}</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.certificateNo }}</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(titleLevelList, scope.row.titleLevel) }}</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.authorityName }}</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.acquisitionDate }}</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="editRow(scope.row,scope.$index)"
                >
                  [编辑]
                </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>
    <!-- 添加教育信息弹窗 -->
    <expertTitleInfoDialog
      ref="bidWorkExperienceDialog"
      @dialogDataBuyersInfo="dialogDataBuyersInfo"
    />
  </div>
</template>

<script>
/* 接口*/
import {
  deleteExptExpertTitles,
  getExpertTitlesById
} from '@/api/expert/expertInfo' //  职称信息接口
import { submitExpertApply } from '@/api/expert/expertInfo' //  提交所有信息接口
import { submitExpertChangeApply, delExpertTitleHistoryById, queryexpertTitlesLisByApplyId } from '@/api/expert/expertChange'//  职称信息提交、删除接口
/* 组件*/
import expertTitleInfoDialog from './expertTitleInfo-dialog' // 职称信息弹窗
import pagination from '@comp/common/pagination/pagination' // 分页
/* 公共方法*/
import { ListMixin } from '@/mixins/ListMixin'
import { mapGetters } from 'vuex'
import { filterDictText } from '@/assets/js/utils'
import { expertInfo } from '@/views/expert/expertInfo/mixins/expertInfo'
export default {
  name: 'ExpertWorkPerience',
  components: {
    expertTitleInfoDialog, // 选择职称信息弹窗
    pagination
  },
  mixins: [ListMixin, expertInfo],
  data() {
    return {
      type: this.$route.meta.type,
      confirmLoading: false, // 下一步Loding
      formData: {
        certificateNo: '', // 证书编号
        authorityName: '', // 颁发机构
        certificateName: '', // 证书名称
        acquisitionDate: '', // 颁发时间
        titleLevel: '', // 职称
        projectBuyersList: [], // 职称信息列表数据
        id: ''
      },
      fileUrl: '',
      pathData: {
        id: this.$route.query.id
      }
    }
  },
  computed: {
    ...mapGetters(['info'])
  },
  created() {
    this.loadData()
  },
  methods: {
    filterDictText,
    // 加载数据
    loadData() {
      const data = {
        userId: this.info.id,
        applyId: this.$route.params.id
      }
      if (this.type === 'declarationChange') {
        queryexpertTitlesLisByApplyId(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
          })
          .catch(() => {})
      } else {
        const expertId = this.$route.query.id
        if (expertId) {
          data.expertId = expertId
          delete data.userId
        }
        getExpertTitlesById(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
          })
          .catch(() => {})
      }
    },
    // 打开弹框挑选
    selectBuyersShowDialogBtn() {
      this.$refs.bidWorkExperienceDialog.openOrganizationDialog('add')
    },
    // 添加称信息到表格
    dialogDataBuyersInfo(data, type, index) {
      if (type === 'edit') {
        this.$set(this.formData.projectBuyersList, index, data)
        this.loadData()
      }
      if (type === 'add') {
        this.loadData()
      }
    },
    // 称信息表格的编辑和保存
    editRow(row, index, id) {
      this.$refs.bidWorkExperienceDialog.formData = row
      this.$refs.bidWorkExperienceDialog.openOrganizationDialog('edit', index)
      this.loadData()
    },
    // 称信息表格删除
    deleteRow(row, index, id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.type === 'declarationChange') {
          delExpertTitleHistoryById(this.formData.projectBuyersList[row].id).then(res => {
            this.loadData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
        } else {
          deleteExptExpertTitles(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.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>
