<!--
  @description: 专家-完善基本信息-职业资格
  @Modifier: 徐媛媛
  @routerPath: /expert/expertOccupation
  @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="addOccupationCondition('')"
          >
            添加
          </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.practiceQualificationName }}</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.acquisitionDate }}</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" align="center">
              <template slot="header">
                <span>颁发机构</span>
              </template>
              <template slot-scope="scope">
                <span>{{ scope.row.certificateCompany }}</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 v-if="scope.row.longTerm === '01'">长期</span>
                <span v-else>{{ scope.row.invalidDate }}</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="addOccupationCondition(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>
    <!-- 添加职业资格弹窗 -->
    <expertOccupationDialog
      v-if="dialogData.dialog"
      :dialog-data="dialogData"
      @dialogDataBuyersInfo="dialogDataBuyersInfo"
    />
  </div>
</template>

<script>
/* 接口*/
import {
  deleteExptExpertCertification,
  getExpertCertificationById
} from '@/api/expert/expertOccupation' //  添加职业资格接口
import { submitExpertApply } from '@/api/expert/expertInfo' //  提交所有信息接口
import { submitExpertChangeApply, deleteTBExpertCertificationHistoryById, queryAllTBExpertCertificationHistoryForUpdate } from '@/api/expert/expertChange'//  变更职业资格全部提交，单个删除接口
/* 组件*/
import expertOccupationDialog from './expertOccupation-dialog' // 添加职业资格弹窗
import pagination from '@comp/common/pagination/pagination' // 分页
// import { filterDictText } from '@/assets/js/utils'// 常用方法
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin'// 分页

/* 公共方法*/
// import { ListMixin } from '@/mixins/ListMixin'
import { mapGetters } from 'vuex'
export default {
  name: 'ExpertOccupationAdd',
  components: {
    expertOccupationDialog, // 添加职业资格弹窗
    pagination // 分页
  },
  mixins: [ListMixin],
  props: {
    projectData: {
      type: Object,
      default: function() {
        return {
          id: '',
          applyId: '',
          type: ''
        }
      }
    }
  },
  data() {
    return {
      confirmLoading: false, // 暂存、下一步Loding
      formData: {
        acquisitionDate: '', // 取得时间
        certificateCompany: '', // 颁发机构
        certificateNo: '', // 证书编号
        invalidDate: '', // 有效期至
        practiceQualificationName: '', // 职业资格id
        projectBuyersList: [],
        id: ''
      },
      dialogData: {
        dialog: false, // 展示状态
        id: '' // 编辑时才会有财务状况id
      },
      fileUrl: '',
      pathData: {
        id: this.$route.query.id
      }
    }
  },
  computed: {
    ...mapGetters(['info'])
  },
  created() {
    this.loadData()
  },
  methods: {
    // 加载数据
    loadData() {
      const data = {
        userId: this.info.id,
        applyId: this.projectData.applyId
      }
      if (this.projectData.type === 'declarationChange') {
        queryAllTBExpertCertificationHistoryForUpdate(data)
          .then(res => {
            this.formData.projectBuyersList = res.data.records
            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
          delete data.applyId
        }
        getExpertCertificationById(data)
          .then(res => {
            this.formData.projectBuyersList = res.data.records
            this.paginationData.pageNo = res.data.current
            this.paginationData.pageSize = res.data.size
            this.paginationData.total = res.data.total
          })
          .catch(() => {})
      }
    },
    // 打开弹框
    addOccupationCondition(id) {
      this.dialogData = {
        dialog: true,
        id: id
      }
    },
    // 添加职业资格到表格
    dialogDataBuyersInfo() {
      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
        if (this.projectData.type === 'declarationChange') {
          deleteTBExpertCertificationHistoryById(ids).then(res => {
            this.loadData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
        } else {
          deleteExptExpertCertification(ids).then(res => {
            this.loadData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 上一步
    prevStep() {
      // router携带参数
      this.$emit('stepPage', 'prev', this.pathData) // 步骤条切换方法 prev 上一步
    },

    //  下一步
    nextStep(type) {
      this.confirmLoading = false
      this.$emit('stepPage', 'next', this.pathData) //  next下一步
    },
    // 返回
    backList() {
      this.$router.push({
        path: '/expert/expertQuitInfoAudit-list'
      })
    },
    //  提交
    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/expertEssentialInfo' })
              }).catch(() => {})
            } else {
              submitExpertApply().then(res => {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.$router.push({ path: '/expert/perfectInfo/expertEssentialInfo' })
              }).catch(() => {
              })
            }
          }).catch(() => {})
        }
      })
    }
  }
}
</script>

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