<template>
  <div>
    <el-card>
      <span>项目名称：</span>
      <el-select v-model="entryNameValue" filterable placeholder="项目名称" style="margin-right: 10px" size="small" clearable>
        <el-option
          v-for="item in entryName"
          :key="item.value"
          :label="item.label"
          :value="item.value"/>
      </el-select>
      <span style="margin-left: 10px">开始日期：</span>
      <el-date-picker
        v-model="startTime"
        style="width: 200px;"
        value-format="yyyy-MM-dd"
        size="small"
        type="date"
        placeholder="请选择开始日期"/>
      <span style="margin-left: 10px">结束日期：</span>
      <el-date-picker
        v-model="endTime"
        style="width: 200px;"
        value-format="yyyy-MM-dd"
        size="small"
        type="date"
        placeholder="请选择结束日期"/>
      <el-button type="success" icon="el-icon-search" size="small" style="margin-left: 10px" @click="PageSize = 10;PageCurrent = 1;fetchTableData1()">查询</el-button>
    </el-card>

    <el-card>
      <el-table
        v-loading="listLoading1"
        :data="tableData1"
        style="width: 100%"
        height="400"
        border
        size="mini"
        highlight-current-row
        @row-click="fetchTableData2">
        <el-table-column align="center" label="合同编号" width="200">
          <template slot-scope="scope">
            <el-tag type="primary">{{ scope.row.合同编号 }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目名称" width="200">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="已完成进度" width="100">
          <template slot-scope="scope">
            {{ scope.row.执行进度名 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="合同签订日期">
          <template slot-scope="scope">
            {{ scope.row.合同签订日期 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="销售经理" width="80">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="客户名称">
          <template slot-scope="scope">
            {{ scope.row.客户名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="联系人" width="80">
          <template slot-scope="scope">
            {{ scope.row.联系人 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="联系电话" width="100">
          <template slot-scope="scope">
            {{ scope.row.电话 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="合同信息备注">
          <template slot-scope="scope">
            {{ scope.row.合同信息备注 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="260px">
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.是否禁用增加履约保证金"
              size="mini"
              type="primary"
              icon="el-icon-circle-plus-outline"
              @click.stop="openPerformanceBondDialog(scope.row); flag = 1">履约保证金</el-button>
            <el-button
              :disabled="scope.row.是否禁用增加履约保函"
              size="mini"
              type="primary"
              icon="el-icon-circle-plus-outline"
              @click.stop="addPerformanceGuarantee(scope.row)">履约保函</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="PageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="PageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
    <el-card>
      <template>
        <el-tabs v-model="activeName" type="card">
          <el-tab-pane :disabled="performanceBondDisabled" label="履约保证金" name="first">
            <el-table
              v-loading="listLoading2"
              :data="tableData2"
              highlight-current-row
              style="width: 100%"
              height="100"
              border
              size="mini">
              <el-table-column align="center" label="履约保证金金额">
                <template slot-scope="scope">
                  <el-tag type="primary" size="small">{{ scope.row.履约保证金金额 }}</el-tag>
                </template>
              </el-table-column>
              <el-table-column align="center" label="保证金类型">
                <template slot-scope="scope">
                  {{ scope.row.保证金类型 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="收回条件">
                <template slot-scope="scope">
                  {{ scope.row.执行进度名 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="截止时间">
                <template slot-scope="scope">
                  后{{ scope.row.提前X天提醒我 }}天内收回
                </template>
              </el-table-column>
              <el-table-column align="center" label="是否收回">
                <template slot-scope="scope">
                  <el-tag v-if="parseInt(scope.row.是否收回) === 1" slot="reference" size="small" type="success" style="width: 66px">已收回</el-tag>
                  <el-tag v-else-if="parseInt(scope.row.是否收回) === 0" slot="reference" type="danger" size="small" style="width: 66px">未收回</el-tag>
                </template>
              </el-table-column>
              <el-table-column align="center" label="实际收回日期">
                <template slot-scope="scope">
                  {{ scope.row.实际收回日期 === '' || scope.row.实际收回日期 === null ? scope.row.实际收回日期 = '暂未收回' : scope.row.实际收回日期 }}
                </template>
              </el-table-column>
              <el-table-column align="center" label="状态">
                <template slot-scope="scope">
                  <el-popover
                    placement="top-start"
                    title="收回计划"
                    width="200"
                    trigger="hover">
                    <div v-show="(scope.row.计划收款日期 === '' || scope.row.计划收款日期 === null) && parseInt(scope.row.是否收回)!== 1 " style="text-align: center">
                      <h4>{{ scope.row.执行进度名 }} 未完成</h4>
                    </div>
                    <div v-show="scope.row.计划收款日期 !== '' && scope.row.计划收款日期 !== null && parseInt(scope.row.是否收回)!== 1">
                      <h4>{{ scope.row.执行进度名 }}于{{ scope.row.节点日期 }}完成，计划于{{ scope.row.计划收款日期 }}进行收款，距离计划截止还剩{{ parseInt(scope.row.剩余天数) > 0 ? scope.row.剩余天数 : 0 }}天</h4>
                    </div>
                    <div v-show="parseInt(scope.row.是否收回)=== 1 " style="text-align: center">
                      <h4>已于{{ scope.row.实际收回日期 }}收回</h4>
                    </div>
                    <el-tag v-if="scope.row.计划收款日期!==''&& scope.row.计划收款日期!==null && parseInt(scope.row.是否收回) === 0 && parseInt(scope.row.状态) === 1" slot="reference" size="small" type="warning" style="width: 66px">即将拖期</el-tag>
                    <el-tag v-else-if="scope.row.计划收款日期!==''&& scope.row.计划收款日期!==null &&parseInt(scope.row.是否收回) === 0 && parseInt(scope.row.状态) === 2" slot="reference" type="danger" size="small" style="width: 66px">已经拖期</el-tag>
                    <el-tag v-else-if="scope.row.计划收款日期!==''&& scope.row.计划收款日期!==null &&parseInt(scope.row.是否收回) === 0 && parseInt(scope.row.状态) === 0" slot="reference" type="primer" size="small" style="width: 66px">无计划</el-tag>
                    <el-tag v-else-if="scope.row.计划收款日期!==''&& scope.row.计划收款日期!==null &&parseInt(scope.row.是否收回) === 1" slot="reference" type="success" size="small" style="width: 66px">已完成</el-tag>
                    <el-tag v-else-if="scope.row.计划收款日期===''||scope.row.计划收款日期===null" slot="reference" size="small" style="width: 66px">进度未完</el-tag>
                  </el-popover>
                </template>
              </el-table-column>
              <el-table-column label="操作" fixed="right" align="center" width="260px">
                <template slot-scope="scope">
                  <el-button
                    :disabled="scope.row.是否禁用收回"
                    size="mini"
                    type="success"
                    @click.stop="takeBack(scope.row)">收回</el-button>
                  <el-button
                    :disabled="scope.row.是否禁用编辑"
                    size="mini"
                    type="warning"
                    @click.stop="openEditPerformanceBondDialog(scope.row); flag = 2">编辑</el-button>
                  <el-button
                    :disabled="scope.row.是否禁用删除"
                    size="mini"
                    type="danger"
                    @click.stop="delPerformanceBond(scope.row)">删除</el-button >
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <el-tab-pane :disabled="performanceGuaranteeDisabled" label="履约保函" name="second">
            <div v-show="performanceGuaranteeShow">
              <el-button type="danger" icon="el-icon-delete" size="small" style="float: right;display: inline-block" @click="delPerformanceGuarantee">删除</el-button>
              <div style="width: 150px; height: 150px; margin: auto;left: 50%;right: 50%">
                <el-upload
                  :on-preview="handlePictureCardPreview"
                  :on-remove="handleRemove"
                  :file-list="fileList"
                  :before-upload="beforeUpload"
                  :on-success="onSuccess"
                  :action="upload3 + '?ProjectId=' + projectId"
                  list-type="picture-card">
                  <i class="el-icon-plus"/>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                  <img :src="dialogImageUrl" width="100%" alt="">
                </el-dialog>
              </div>
            </div>
          </el-tab-pane>
        </el-tabs>
      </template>
    </el-card>

    <el-dialog :visible.sync="dialogTimeVisible" title="实际收回日期" center style="min-height: 300px" width="23%">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="实际收回日期" prop="实际收回日期">
          <el-date-picker
            v-model="form.实际收回日期"
            style="width: 200px;"
            value-format="yyyy-MM-dd"
            size="small"
            type="date"
            placeholder="请选择实际收回日期"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOffTime('form')">取消</el-button>
        <el-button type="primary" @click="submitTime('form')">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title1" :visible.sync="dialogFormVisible1" center style="min-height: 300px" width="40%">
      <el-form ref="form2" :model="form2" :rules="rules2" label-position="left" label-width="120px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="保证金金额" prop="保证金金额">
              <el-input-number v-model="form2.保证金金额" :precision="2" :step="0.1" style="width: 200px" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保证金类型" prop="保证金类型">
              <el-select v-model="form2.保证金类型" filterable placeholder="请选择保证金类型" style="width: 200px" size="small">
                <el-option
                  v-for="item in performanceBondType"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收回条件" prop="收回条件">
              <el-select v-model="form2.收回条件" filterable placeholder="请选择收回条件" style="width: 200px" size="small">
                <el-option
                  v-for="item in plannedTime"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="截止时间(天)" prop="截止时间">
              <el-input-number v-model="form2.截止时间" placeholder="请输入截止时间" style="width: 200px" size="small"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOffPerformanceBondDialog('form2')">取消</el-button>
        <el-button type="primary" @click="submitPerformanceBondDialog('form2')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import request from '@/utils/request'
import elInput from 'element-ui/packages/input'
import { upload3, readImg } from '@/utils/request'
import { initProject, searchTableData1, typeOfPerformanceBond, searchTableData2, takeBack, executionProgressID, initType, addPerformanceBond, editPerformanceBond, delPerformanceBond, addPerformanceGuarantee, delPerformanceGuarantee } from '@/api/MarketingCenter/PerformanceBond'
export default {
  components: {
    elInput
  },
  data() {
    const validateValue = (rule, value, callback) => {
      if (value === 0) {
        callback(new Error('金额不能为0'))
      } else {
        callback()
      }
    }
    return {
      upload3: upload3,
      title1: '', //   履约保证金新增或编辑对话框
      entryNameValue: '', // 查询条件_项目名称
      entryName: [], // 查询条件_项目名称数组
      startTime: '', // 查询条件_开始时间
      endTime: '', // 查询条件_结束时间
      PageSize: 10, // 项目信息表格初始一页显示十条
      PageCurrent: 1, // 项目信息表格一次翻一页
      total: null,
      tableData1: [], // 项目信息表格数据
      listLoading1: false, // 项目信息表格加载动画
      flag: '', // 旗帜变量
      activeName: 'first', // 标签页初始
      projectId: '', // 项目流水号中间变量
      tableData2: [], // 履约保证金信息
      listLoading2: false,
      dialogTimeVisible: false,
      performanceBondCode: '', // 履约保证金流水号
      dialogFormVisible1: false, // 履约保证金对话框
      plannedTime: [],
      performanceBondType: [],
      performanceBondDisabled: true, // 履约保证金标签页是否禁用
      performanceGuaranteeDisabled: true, // 履约保函是否禁用
      dialogVisible: false, // 履约保函复印件
      dialogImageUrl: '',
      fileList: [],
      performanceGuaranteeShow: true,
      form: {
        实际收回日期: ''
      },
      form2: {
        保证金金额: '',
        保证金类型: 1,
        收回条件: 1,
        截止时间: 15
      },
      rules: { // 表单验证规则
        实际收回日期: [{ required: true, message: '请选择实际收回日期', trigger: 'change' }]
      },
      rules2: { // 表单验证规则
        保证金金额: [
          { required: true, message: '请输入保证金金额', trigger: 'change' },
          { required: true, trigger: 'blur', validator: validateValue }],
        保证金类型: [{ required: true, message: '请选择保证金类型', trigger: 'change' }],
        收回条件: [{ required: true, message: '请选择收回条件', trigger: 'change' }],
        截止时间: [{ required: true, message: '请输入截止时间', trigger: 'change' }]
      }
    }
  },
  created() {
    this.init()
    this.fetchTableData1()
  },
  methods: {
    // 初始化条件
    init() {
      this.getSelect(initProject, ['项目名称', '项目流水号'], 'entryName')
      this.getSelect(executionProgressID, ['执行进度名', '执行进度ID'], 'plannedTime')
      this.getSelect(initType, ['保证金类型', '保证金类型流水号'], 'performanceBondType')
    },
    // 查询合同(项目)信息
    fetchTableData1() {
      if (this.entryNameValue === '' || this.entryNameValue === null) {
        this.entryNameValue_check = 0
      } else {
        this.entryNameValue_check = 1
      }
      if (this.startTime === '' || this.startTime === null) {
        this.startTime_check = 0
      } else {
        this.startTime_check = 1
      }
      if (this.endTime === '' || this.endTime === null) {
        this.endTime_check = 0
      } else {
        this.endTime_check = 1
      }
      this.listLoading1 = true
      searchTableData1(this.PageCurrent, this.PageSize, this.entryNameValue, this.entryNameValue_check, this.startTime_check, this.endTime_check, this.startTime, this.endTime).then(response => {
        this.tableData2 = []
        this.listLoading1 = false
        const data = response.data
        for (let i = 0; i < data.rows.length; i++) {
          data.rows[i].合同签订日期 = data.rows[i].合同签订日期.substring(0, data.rows[i].合同签订日期.length - 8)
          parseInt(data.rows[i].履约保证金类型) !== 0 ? data.rows[i].是否禁用增加履约保证金 = true : data.rows[i].是否禁用增加履约保证金 = false
          parseInt(data.rows[i].履约保证金类型) !== 0 ? data.rows[i].是否禁用增加履约保函 = true : data.rows[i].是否禁用增加履约保函 = false
        }
        this.tableData1 = data.rows
        this.total = data.total
      })
    },
    // 项目信息表格改变每页显示条数
    handleSizeChange(val) {
      this.PageCurrent = 1
      this.PageSize = val
      this.tableData2 = []
      this.fetchTableData1()
    },
    // 项目信息表格翻页
    handleCurrentChange(val) {
      this.PageCurrent = val
      this.tableData2 = []
      this.fetchTableData1()
    },
    // 履约保证金查询
    fetchTableData2(row) {
      this.projectId = row.项目流水号
      typeOfPerformanceBond(this.projectId).then(response => {
        const data = response.data[0].履约保证金类型
        return data
      }).then(data => {
        if (data === 1) {
          this.activeName = 'first'
          this.listLoading2 = true
          this.performanceBondDisabled = false
          this.performanceGuaranteeDisabled = true
          searchTableData2(this.projectId).then(response => {
            this.listLoading2 = false
            const data = response.data
            for (let i = 0; i < data.length; i++) {
              data[i].实际收回日期 !== '' && data[i].实际收回日期 !== null ? data[i].实际收回日期 = data[i].实际收回日期.substring(0, data[i].实际收回日期.length - 8) : data[i].实际收回日期
              data[i].节点日期 !== '' && data[i].节点日期 !== null ? data[i].节点日期 = data[i].节点日期.substring(0, data[i].节点日期.length - 8) : data[i].节点日期
              data[i].计划收款日期 !== '' && data[i].计划收款日期 !== null ? data[i].计划收款日期 = data[i].计划收款日期.substring(0, data[i].计划收款日期.length - 8) : data[i].计划收款日期
              data[i].计划收款日期 === '' || data[i].计划收款日期 === null || parseInt(data[i].是否收回) === 1 ? data[i].是否禁用收回 = true : data[i].是否禁用收回 = false
              parseInt(data[i].是否收回) === 1 ? data[i].是否禁用编辑 = true : data[i].是否禁用编辑 = false
              parseInt(data[i].是否收回) === 1 ? data[i].是否禁用删除 = true : data[i].是否禁用删除 = false
            }
            this.tableData2 = data
          })
        } else if (data === 2) {
          this.activeName = 'second'
          this.performanceBondDisabled = true
          this.performanceGuaranteeDisabled = false
          this.performanceGuaranteeShow = true
          this.getFileList()
        } else {
          this.$message.warning('未缴纳履约保证金')
          this.tableData2 = []
          this.performanceGuaranteeShow = false
          this.performanceBondDisabled = true
          this.performanceGuaranteeDisabled = true
        }
      })
    },
    takeBack(row) {
      this.addForm('form')
      this.performanceBondCode = row.履约保证金流水号
      this.dialogTimeVisible = true
    },
    callOffTime() {
      this.dialogTimeVisible = false
    },
    submitTime(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          takeBack(this.performanceBondCode, this.form.实际收回日期).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('操作成功')
              this.dialogTimeVisible = false
              typeOfPerformanceBond(this.projectId).then(response => {
                const data = response.data[0].履约保证金类型
                return data
              }).then(data => {
                if (data === 1) {
                  this.activeName = 'first'
                  this.listLoading2 = true
                  this.performanceBondDisabled = false
                  this.performanceGuaranteeDisabled = true
                  searchTableData2(this.projectId).then(response => {
                    this.listLoading2 = false
                    const data = response.data
                    for (let i = 0; i < data.length; i++) {
                      data[i].实际收回日期 !== '' && data[i].实际收回日期 !== null ? data[i].实际收回日期 = data[i].实际收回日期.substring(0, data[i].实际收回日期.length - 8) : data[i].实际收回日期
                      data[i].节点日期 !== '' && data[i].节点日期 !== null ? data[i].节点日期 = data[i].节点日期.substring(0, data[i].节点日期.length - 8) : data[i].节点日期
                      data[i].计划收款日期 !== '' && data[i].计划收款日期 !== null ? data[i].计划收款日期 = data[i].计划收款日期.substring(0, data[i].计划收款日期.length - 8) : data[i].计划收款日期
                      data[i].计划收款日期 === '' || data[i].计划收款日期 === null || parseInt(data[i].是否收回) === 1 ? data[i].是否禁用收回 = true : data[i].是否禁用收回 = false
                      parseInt(data[i].是否收回) === 1 ? data[i].是否禁用编辑 = true : data[i].是否禁用编辑 = false
                      parseInt(data[i].是否收回) === 1 ? data[i].是否禁用删除 = true : data[i].是否禁用删除 = false
                    }
                    this.tableData2 = data
                  })
                } else if (data === 2) {
                  this.activeName = 'second'
                  this.performanceBondDisabled = true
                  this.performanceGuaranteeDisabled = false
                  this.performanceGuaranteeShow = true
                  this.getFileList()
                } else {
                  this.$message.warning('未缴纳履约保证金')
                  this.performanceBondDisabled = true
                  this.performanceGuaranteeDisabled = true
                  this.tableData2 = []
                  this.performanceGuaranteeShow = false
                }
              })
            } else {
              this.$message.error('操作失败')
            }
          })
        }
      })
    },
    openPerformanceBondDialog(row) {
      this.addForm('form2')
      this.title1 = '增加履约保证金'
      this.dialogFormVisible1 = true
      this.projectId = row.项目流水号
      this.tableData2 = []
    },
    callOffPerformanceBondDialog() {
      this.dialogFormVisible1 = false
    },
    submitPerformanceBondDialog(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.flag === 1) {
            addPerformanceBond(this.projectId, this.form2.保证金金额, this.form2.保证金类型, this.form2.收回条件, this.form2.截止时间).then(response => {
              if (response.data[0].result === '1') {
                this.$message.success('操作成功')
                this.fetchTableData1()
                this.dialogFormVisible1 = false
                typeOfPerformanceBond(this.projectId).then(response => {
                  const data = response.data[0].履约保证金类型
                  return data
                }).then(data => {
                  if (data === 1) {
                    this.activeName = 'first'
                    this.listLoading2 = true
                    this.performanceBondDisabled = false
                    this.performanceGuaranteeDisabled = true
                    searchTableData2(this.projectId).then(response => {
                      this.listLoading2 = false
                      const data = response.data
                      for (let i = 0; i < data.length; i++) {
                        data[i].实际收回日期 !== '' && data[i].实际收回日期 !== null ? data[i].实际收回日期 = data[i].实际收回日期.substring(0, data[i].实际收回日期.length - 8) : data[i].实际收回日期
                        data[i].节点日期 !== '' && data[i].节点日期 !== null ? data[i].节点日期 = data[i].节点日期.substring(0, data[i].节点日期.length - 8) : data[i].节点日期
                        data[i].计划收款日期 !== '' && data[i].计划收款日期 !== null ? data[i].计划收款日期 = data[i].计划收款日期.substring(0, data[i].计划收款日期.length - 8) : data[i].计划收款日期
                        data[i].计划收款日期 === '' || data[i].计划收款日期 === null || parseInt(data[i].是否收回) === 1 ? data[i].是否禁用收回 = true : data[i].是否禁用收回 = false
                        parseInt(data[i].是否收回) === 1 ? data[i].是否禁用编辑 = true : data[i].是否禁用编辑 = false
                        parseInt(data[i].是否收回) === 1 ? data[i].是否禁用删除 = true : data[i].是否禁用删除 = false
                      }
                      this.tableData2 = data
                    })
                  } else if (data === 2) {
                    this.activeName = 'second'
                    this.performanceBondDisabled = true
                    this.performanceGuaranteeDisabled = false
                    this.performanceGuaranteeShow = true
                    this.getFileList()
                  } else {
                    this.$message.warning('未缴纳履约保证金')
                    this.performanceBondDisabled = true
                    this.performanceGuaranteeDisabled = true
                    this.tableData2 = []
                    this.performanceGuaranteeShow = false
                  }
                })
              } else {
                this.$message.error('操作失败')
              }
            })
          } else if (this.flag === 2) {
            editPerformanceBond(this.projectId, this.form2.保证金金额, this.form2.保证金类型, this.form2.收回条件, this.form2.截止时间, this.performanceBondCode).then(response => {
              if (response.data[0].result === '1') {
                this.$message.success('操作成功')
                this.dialogFormVisible1 = false
                typeOfPerformanceBond(this.projectId).then(response => {
                  const data = response.data[0].履约保证金类型
                  return data
                }).then(data => {
                  if (data === 1) {
                    this.activeName = 'first'
                    this.listLoading2 = true
                    this.performanceBondDisabled = false
                    this.performanceGuaranteeDisabled = true
                    searchTableData2(this.projectId).then(response => {
                      this.listLoading2 = false
                      const data = response.data
                      for (let i = 0; i < data.length; i++) {
                        data[i].实际收回日期 !== '' && data[i].实际收回日期 !== null ? data[i].实际收回日期 = data[i].实际收回日期.substring(0, data[i].实际收回日期.length - 8) : data[i].实际收回日期
                        data[i].节点日期 !== '' && data[i].节点日期 !== null ? data[i].节点日期 = data[i].节点日期.substring(0, data[i].节点日期.length - 8) : data[i].节点日期
                        data[i].计划收款日期 !== '' && data[i].计划收款日期 !== null ? data[i].计划收款日期 = data[i].计划收款日期.substring(0, data[i].计划收款日期.length - 8) : data[i].计划收款日期
                        data[i].计划收款日期 === '' || data[i].计划收款日期 === null || parseInt(data[i].是否收回) === 1 ? data[i].是否禁用收回 = true : data[i].是否禁用收回 = false
                        parseInt(data[i].是否收回) === 1 ? data[i].是否禁用编辑 = true : data[i].是否禁用编辑 = false
                        parseInt(data[i].是否收回) === 1 ? data[i].是否禁用删除 = true : data[i].是否禁用删除 = false
                      }
                      this.tableData2 = data
                    })
                  } else if (data === 2) {
                    this.activeName = 'second'
                    this.performanceBondDisabled = true
                    this.performanceGuaranteeDisabled = false
                    this.performanceGuaranteeShow = true
                    this.getFileList()
                  } else {
                    this.$message.warning('未缴纳履约保证金')
                    this.performanceBondDisabled = true
                    this.performanceGuaranteeDisabled = true
                    this.tableData2 = []
                    this.performanceGuaranteeShow = false
                  }
                })
              } else {
                this.$message.error('操作失败')
              }
            })
          }
        }
      })
    },
    openEditPerformanceBondDialog(row) {
      this.dialogFormVisible1 = true
      this.performanceBondCode = row.履约保证金流水号
      this.form2.保证金金额 = row.履约保证金金额
      this.form2.保证金类型 = row.保金类型
      this.form2.收回条件 = parseInt(row.执行进度流水号)
      this.title1 = '编辑履约保证金'
    },
    delPerformanceBond(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPerformanceBond(this.projectId, row.履约保证金流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.activeName = 'first'
            this.listLoading2 = true
            this.fetchTableData1()
            searchTableData2(this.projectId).then(response => {
              this.listLoading2 = false
              const data = response.data
              for (let i = 0; i < data.length; i++) {
                data[i].实际收回日期 !== '' && data[i].实际收回日期 !== null ? data[i].实际收回日期 = data[i].实际收回日期.substring(0, data[i].实际收回日期.length - 8) : data[i].实际收回日期
                data[i].节点日期 !== '' && data[i].节点日期 !== null ? data[i].节点日期 = data[i].节点日期.substring(0, data[i].节点日期.length - 8) : data[i].节点日期
                data[i].计划收款日期 !== '' && data[i].计划收款日期 !== null ? data[i].计划收款日期 = data[i].计划收款日期.substring(0, data[i].计划收款日期.length - 8) : data[i].计划收款日期
                data[i].计划收款日期 === '' || data[i].计划收款日期 === null || parseInt(data[i].是否收回) === 1 ? data[i].是否禁用收回 = true : data[i].是否禁用收回 = false
                parseInt(data[i].是否收回) === 1 ? data[i].是否禁用编辑 = true : data[i].是否禁用编辑 = false
                parseInt(data[i].是否收回) === 1 ? data[i].是否禁用删除 = true : data[i].是否禁用删除 = false
              }
              this.tableData2 = data
            })
          } else {
            this.$message.error('数据占用')
          }
        })
      })
    },
    addPerformanceGuarantee(row) {
      this.projectId = row.项目流水号
      this.$confirm('是否新增履约保函', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        addPerformanceGuarantee(this.projectId).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('添加成功')
            this.activeName = 'second'
            this.performanceBondDisabled = true
            this.performanceGuaranteeDisabled = false
            this.fetchTableData1()
            typeOfPerformanceBond(this.projectId).then(response => {
              const data = response.data[0].履约保证金类型
              return data
            }).then(data => {
              if (data === 1) {
                this.activeName = 'first'
                this.listLoading2 = true
                this.performanceBondDisabled = false
                this.performanceGuaranteeDisabled = true
                searchTableData2(this.projectId).then(response => {
                  this.listLoading2 = false
                  const data = response.data
                  for (let i = 0; i < data.length; i++) {
                    data[i].实际收回日期 !== '' && data[i].实际收回日期 !== null ? data[i].实际收回日期 = data[i].实际收回日期.substring(0, data[i].实际收回日期.length - 8) : data[i].实际收回日期
                    data[i].节点日期 !== '' && data[i].节点日期 !== null ? data[i].节点日期 = data[i].节点日期.substring(0, data[i].节点日期.length - 8) : data[i].节点日期
                    data[i].计划收款日期 !== '' && data[i].计划收款日期 !== null ? data[i].计划收款日期 = data[i].计划收款日期.substring(0, data[i].计划收款日期.length - 8) : data[i].计划收款日期
                    data[i].计划收款日期 === '' || data[i].计划收款日期 === null || parseInt(data[i].是否收回) === 1 ? data[i].是否禁用收回 = true : data[i].是否禁用收回 = false
                    parseInt(data[i].是否收回) === 1 ? data[i].是否禁用编辑 = true : data[i].是否禁用编辑 = false
                    parseInt(data[i].是否收回) === 1 ? data[i].是否禁用删除 = true : data[i].是否禁用删除 = false
                  }
                  this.tableData2 = data
                })
              } else if (data === 2) {
                this.activeName = 'second'
                this.performanceBondDisabled = true
                this.performanceGuaranteeDisabled = false
                this.performanceGuaranteeShow = true
                this.getFileList()
              } else {
                this.$message.warning('未缴纳履约保证金')
                this.tableData2 = []
                this.performanceGuaranteeShow = false
                this.performanceBondDisabled = true
                this.performanceGuaranteeDisabled = true
              }
            })
          } else {
            this.$message.error('添加失败')
          }
        })
      })
    },
    async getFileList() {
      const { data } = await request({
        url: '',
        method: 'post',
        data: {
          type: '3',
          name: `select * from 合同信息管理_履约保证金_履约保函 where 是否启用 = 1 and 项目流水号 = ${this.projectId}`
        }
      })
      this.fileList = data.map(item => {
        return {
          _id: item.履约保函流水号,
          name: item.文件标识,
          url: readImg + item.文件标识
        }
      })
    },
    async handleRemove(file, fileList) {
      const { data } = await request({
        url: '',
        method: 'post',
        data: {
          type: '4',
          name: `update 合同信息管理_履约保证金_履约保函 set 是否启用 = 0 where 履约保函流水号 = ${file._id}`
        }
      })
      if (data[0].result === '0') {
        this.$message.error('上传失败')
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    beforeUpload(file) {
      const isJPG = /^image\/.*/.test(file.type)
      const isLt4M = file.size / 1024 / 1024 < 4
      const isInvoiceNum = !!this.projectId

      if (!isJPG) {
        this.$message.error('只能上传图片。。')
      }
      if (!isLt4M) {
        this.$message.error('上传图片大小不能超过 4MB!')
      }

      return isJPG && isLt4M && isInvoiceNum
    },
    onSuccess(res, file, fileList) {
      fileList = []
      if (res[0].result === '1') {
        this.getFileList()
      } else {
        this.$message.error('上传失败')
      }
    },
    delPerformanceGuarantee() {
      this.$confirm('是否删除履约保函', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delPerformanceGuarantee(this.projectId).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.fetchTableData1()
            typeOfPerformanceBond(this.projectId).then(response => {
              const data = response.data[0].履约保证金类型
              return data
            }).then(data => {
              if (data === 1) {
                this.activeName = 'first'
                this.listLoading2 = true
                this.performanceBondDisabled = false
                this.performanceGuaranteeDisabled = true
                searchTableData2(this.projectId).then(response => {
                  this.listLoading2 = false
                  const data = response.data
                  for (let i = 0; i < data.length; i++) {
                    data[i].实际收回日期 !== '' && data[i].实际收回日期 !== null ? data[i].实际收回日期 = data[i].实际收回日期.substring(0, data[i].实际收回日期.length - 8) : data[i].实际收回日期
                    data[i].节点日期 !== '' && data[i].节点日期 !== null ? data[i].节点日期 = data[i].节点日期.substring(0, data[i].节点日期.length - 8) : data[i].节点日期
                    data[i].计划收款日期 !== '' && data[i].计划收款日期 !== null ? data[i].计划收款日期 = data[i].计划收款日期.substring(0, data[i].计划收款日期.length - 8) : data[i].计划收款日期
                    data[i].计划收款日期 === '' || data[i].计划收款日期 === null || parseInt(data[i].是否收回) === 1 ? data[i].是否禁用收回 = true : data[i].是否禁用收回 = false
                    parseInt(data[i].是否收回) === 1 ? data[i].是否禁用编辑 = true : data[i].是否禁用编辑 = false
                    parseInt(data[i].是否收回) === 1 ? data[i].是否禁用删除 = true : data[i].是否禁用删除 = false
                  }
                  this.tableData2 = data
                })
              } else if (data === 2) {
                this.activeName = 'second'
                this.performanceBondDisabled = true
                this.performanceGuaranteeDisabled = false
                this.performanceGuaranteeShow = true
                this.getFileList()
              } else {
                this.$message.warning('未缴纳履约保证金')
                this.tableData2 = []
                this.performanceGuaranteeShow = false
                this.performanceBondDisabled = true
                this.performanceGuaranteeDisabled = true
              }
            })
          } else {
            this.$message.error('删除失败')
          }
        })
      })
    }
  }
}
</script>

<style scoped>

</style>
