<!--
  @description: 专家-完善基本信息-评标专业
  @Modifier: 徐媛媛
  @routerPath: /expert/expertEvaluationInfo
  @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.specialtyName }}</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 v-if="scope.row.specialtyType==='01'">主评</span>
                <span v-else-if="scope.row.specialtyType==='00'">辅评</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.beginYear }}</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 type="primary" @click="allSubmit">
            提交
          </el-button>
        </div>
      </div>
    </div>
    <!-- 添加评标专业弹窗 -->
    <expertEvaluationInfoDialog
      ref="bidWorkExperienceDialog"
      @dialogDataBuyersInfo="dialogDataBuyersInfo"
    />
  </div>
</template>

<script>
/* 接口*/
import {
  getExpertEvaluationSpecialById,
  deleteExptExpertEvaluationSpecial
} from '@/api/expert/expertEvaluationInfo' //  评标专业信息接口
import { submitExpertApply } from '@/api/expert/expertInfo' //  完善基本信息提交接口
import { submitExpertChangeApply, delExpertSpecialById, queryExpertSepcialLisByApplyId } from '@/api/expert/expertChange'//  变更任职经历信息全部提交、单个删除接口
/* 组件*/
import expertEvaluationInfoDialog from './expertEvaluationInfo-dialog' // 添加评标专业弹窗
import pagination from '@comp/common/pagination/pagination' // 分页
/* 公共方法*/
import { ListMixin } from '@/mixins/ListMixin'
import { mapGetters } from 'vuex'
export default {
  name: 'ExpertEvaluationInfoAdd',
  components: {
    expertEvaluationInfoDialog, // 添加评标专业弹窗
    pagination
  },
  mixins: [ListMixin],
  props: {
    projectData: {
      type: Object,
      default: function() {
        return {
          id: '',
          applyId: '',
          type: ''
        }
      }
    }
  },
  data() {
    return {
      confirmLoading: false, // 下一步Loding
      // 创建/增加的评标专业字段
      formData: {
        beginYear: '', // 从业起始年
        specialtyName: [], // 评标专业名称
        specialtyType: '', // 评标专业类型 01：主，00：辅
        acquisitionDate: '', // 颁发时间
        specialtyId: '', // 专业id
        projectBuyersList: [],
        id: ''
      },
      fileUrl: '',
      pathData: {
        id: this.$route.query.id
      },
      specialtyTypeList: []
    }
  },
  computed: {
    ...mapGetters(['info'])
  },
  created() {
    this.loadData()
  },
  methods: {
    // 加载数据
    loadData() {
      const data = {
        // userId: this.info.id,
        expertId: this.projectData.applyId || this.$route.query.id
      }
      data.pageNo = this.paginationData.pageNo
      data.pageSize = this.paginationData.pageSize
      if (this.projectData.type === 'declarationChange') {
        queryExpertSepcialLisByApplyId(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
            const records = res.data.records
            const arr = []
            records.forEach((item) => {
              arr.push(item.specialtyType)
              this.specialtyTypeList = arr
            })
          })
          .catch(() => {})
      } else {
        getExpertEvaluationSpecialById(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
            const records = res.data.records
            const arr = []
            records.forEach((item) => {
              arr.push(item.specialtyType)
              this.specialtyTypeList = arr
            })
          })
          .catch(() => {})
      }
    },
    // 打开弹框
    selectBuyersShowDialogBtn() {
      const expertId = this.projectData.applyId || this.$route.query.id // id
      this.$refs.bidWorkExperienceDialog.openOrganizationDialog('add', this.specialtyTypeList, expertId)
    },
    // 添加评标专业经历到表格
    dialogDataBuyersInfo(data, type, index) {
      if (type === 'edit') {
        this.$set(this.formData.projectBuyersList, index, data)
      }
      if (type === 'add') {
        this.loadData()
      }
    },
    // 评标专业表格的编辑和保存
    editRow(row, index) {
      const expertId = this.projectData.applyId || this.$route.query.id // id
      this.$refs.bidWorkExperienceDialog.formData = row
      this.$refs.bidWorkExperienceDialog.openOrganizationDialog('edit', index, expertId)
      this.loadData()
    },
    // 评标专业表格删除
    deleteRow(row, index, id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (this.projectData.type === 'declarationChange') {
          delExpertSpecialById(this.formData.projectBuyersList[row].id).then(res => {
            this.loadData()
          })
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        } else {
          deleteExptExpertEvaluationSpecial(this.formData.projectBuyersList[row].id).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.confirmLoading = true
      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/expertEssentialInfo' })
              }).catch(() => {})
            } else {
              submitExpertApply().then(res => {
                this.$message({
                  message: '提交成功',
                  type: 'success'
                })
                this.$router.push({
                  path: '/expert/perfectInfo',
                  query: {
                    type: this.projectData.type, // 类型
                    expertId: this.projectData.applyId || this.$route.query.id // 专家id
                  }
                })
                // this.$router.push({ path: '/expert/perfectInfo/expertEssentialInfo' })
              }).catch(() => {})
            }
          }).catch(() => {})
        }
      })
    }
  },
  provide() {
    const that = this
    return {
      getHasList() {
        return that.formData.projectBuyersList.map(item => item.specialtyId)
      }
    }
  }
}
</script>

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

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