<!--
  @description: 专家-完善基本信息-历史评标记录
  @Modifier: 徐媛媛
  @routerPath: /expert/expertHistorEvaluation
  @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.projectName }}</span>
              </template>
            </el-table-column>
            <el-table-column header-align="center" min-width="200" align="right">
              <template slot="header">
                <span>金额(万元)</span>
              </template>
              <template slot-scope="scope">
                <span>{{ scope.row.money }}</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.theDate }}</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.certifier }}</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.certifierPhone }}</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 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>
    <!-- 添加历史评标弹窗 -->
    <expertHistorEvaluationDialog
      ref="bidWorkExperienceDialog"
      @dialogDataBuyersInfo="dialogDataBuyersInfo"
    />
  </div>
</template>

<script>
/* 接口*/
import {
  getExpertEvaluationHistoryById,
  deleteExptEvaluationHistory
} from '@/api/expert/expertHistorEvaluation' //  历史评标接口
import { submitExpertApply } from '@/api/expert/expertInfo' //  完善基本信息提交接口
import { submitExpertChangeApply, deleteTBExpertEvaluationHistoryHistoryById, queryAllTBExpertEvaluationHistoryHistoryForUpdate } from '@/api/expert/expertChange'//  完善基本信息提交接口
/* 混入公共方法*/
import { ListMixin } from '@/mixins/ListMixin'
/* 组件*/
import expertHistorEvaluationDialog from './expertHistorEvaluation-dialog' // 历史评标记录弹窗
import pagination from '@comp/common/pagination/pagination' // 分页
/* 接口*/
import { mapGetters } from 'vuex'
export default {
  name: 'ExpertHistoryEvaluationAdd',
  components: {
    expertHistorEvaluationDialog, // 选择历史评标记录弹窗
    pagination
  },
  mixins: [ListMixin],
  props: {
    projectData: {
      type: Object,
      default: function() {
        return {
          id: '',
          applyId: '',
          type: ''
        }
      }
    }
  },
  data() {
    return {
      confirmLoading: false, // 暂存、下一步Loding
      // 创建/增加历史评标记录信息字段
      formData: {
        money: '', // 金额（单位万元）
        theDate: '', // 评标时间
        certifier: '', // 证明人
        projectName: '', // 项目名称
        certifierPhone: '', // 证明人联系方式
        projectBuyersList: [],
        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') {
        queryAllTBExpertEvaluationHistoryHistoryForUpdate(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 {
        getExpertEvaluationHistoryById(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) {
      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.projectData.type === 'declarationChange') {
          deleteTBExpertEvaluationHistoryHistoryById(this.formData.projectBuyersList[row].id).then(res => {
            this.formData.projectBuyersList.splice(index, 1)
            this.loadData()
            this.$message({
              type: 'success',
              message: '删除成功'
            })
          })
        } else {
          deleteExptEvaluationHistory(this.formData.projectBuyersList[row].id).then(res => {
            this.formData.projectBuyersList.splice(index, 1)
            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 上一步
    },
    /*
     * 保存数据接口 暂存，下一步
     *
     * */
    saveData(type) {
      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>
