<template>
  <div class="app-container">
    <!--顶栏结构-->
    <el-card>
      <!--<el-select v-model="entryNameValue" clearable filterable placeholder="项目名称:" size="small" style="margin:3px;">-->
      <!--<el-option-->
      <!--v-for="item in entryName"-->
      <!--:key="item.value"-->
      <!--:label="item.label"-->
      <!--:value="item.value"/>-->
      <!--</el-select>-->
      <el-input v-model="customerName" clearable size="small" style="width:220px;margin: 3px 0" placeholder="客户名称\项目名称\合同编号"/>
      <span style="font-size: 13px;margin-left: 10px">签订日期</span>
      <el-date-picker
        v-model="dateRange"
        :picker-options="pickerOptions"
        size="small"
        type="daterange"
        align="center"
        style="width: 240px;margin: 3px 0"
        value-format="yyyy-MM-dd"
        format="yyyy-MM-dd"
        range-separator="～"
        start-placeholder="开始日期"
        end-placeholder="结束日期"/>
      <!--      <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="primary" icon="el-icon-search" size="small" style="margin-left: 10px" plain @click="PageSizeProject = 30;PageCurrentProject = 1;searchTableDataProject()">查询</el-button>
      <el-tooltip :open-delay="1200" effect="dark" content="导出" placement="top">
        <div style="display: inline-block">
          <el-button type="primary" plain class="el-icon-download" size="small" style="margin: 10px 0 0 630px" @click="exportExcel">导出</el-button>
        </div>
      </el-tooltip>
    </el-card>
    <el-card>
      <el-table
        v-loading="listLoadingProject"
        :data="projectTableData"
        style="width: 1360px;margin-top: 3px;"
        height="650"
        border
        stripe
        size="mini"
        highlight-current-row
        @row-click="fetchTableData1">
        <el-table-column align="center" label="合同编号" prop="合同编号" width="120">
          <template slot-scope="scope">
            <span size="mini" type="primary">{{ scope.row.合同编号 }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目名称" prop="项目名称" width="120" show-overflow-tooltip>
          <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="合同签订日期" width="110">-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.合同签订日期 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <!--<el-table-column align="center" label="销售经理" prop="姓名" width="90">-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.姓名 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <el-table-column align="center" label="客户名称" prop="客户名称" width="220" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.客户名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="安全金(万元)" prop="安全金" width="110" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ Number(scope.row.保证金金额).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="缴纳时间" width="87" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.交付日期 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="扣款金额(万元)" prop="扣款金额" width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ Number(scope.row.扣款金额).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="扣款原因" prop="扣款原因" width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.扣款原因 }}
          </template>
        </el-table-column>
        <el-table-column label="应收(万元)" align="center" width="100">
          <template slot-scope="scope">
            {{ Number(scope.row.应收回金额).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="已收(万元)" align="center" width="100">
          <template slot-scope="scope">
            {{ Number(scope.row.已收金额).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="未收(万元)" align="center" width="100">
          <template slot-scope="scope">
            {{ Number(scope.row.未收金额).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="营销经理" align="center" width="80">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
        <!--<el-table-column align="center" label="联系人" width="90" show-overflow-tooltip>-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.联系人 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <!--<el-table-column align="center" label="联系电话" width="120" show-overflow-tooltip>-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.电话 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <el-table-column align="center" label="合同信息备注" width="120" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.合同信息备注 }}
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" label="备注">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180px">
          <template slot-scope="scope">
            <el-button
              :disabled="scope.row.是否禁用增加安全保证金"
              size="mini"
              type="text"
              icon="el-icon-circle-plus-outline"
              @click.stop="addSafetyMargin(scope.row);flag1 = 1">安全保证金
            </el-button>
            <el-tooltip :open-delay="1000" class="item" effect="dark" content="收回" placement="top">
              <div style="display: inline-block">
                <el-button
                  :disabled="scope.row.是否禁用收回 || !scope.row.是否禁用增加安全保证金"
                  size="mini"
                  type="text"
                  icon="el-icon-money"
                  @click.stop="TakeBack(scope.row);flag1 = 5"/>
              </div>
            </el-tooltip>
            <el-tooltip :open-delay="1000" class="item" effect="dark" content="扣款" placement="top">
              <div style="display: inline-block">
                <el-button
                  :disabled="scope.row.是否禁用扣款|| !scope.row.是否禁用增加安全保证金"
                  size="mini"
                  type="text"
                  style="margin-left: 8px"
                  icon="el-icon-coin"
                  @click.stop="Withdrawing(scope.row);flag1 = 3"/>
              </div>
            </el-tooltip>
            <el-tooltip :open-delay="1000" class="item" effect="dark" content="编辑" placement="top">
              <div style="display: inline-block">
                <el-button
                  :disabled="scope.row.是否禁用编辑|| !scope.row.是否禁用增加安全保证金"
                  size="mini"
                  type="text"
                  style="margin-left: 8px"
                  icon="el-icon-edit-outline"
                  @click.stop="editSafetyMargin(scope.row);flag1 = 2"/>
              </div>
            </el-tooltip>
            <!--<el-tooltip :open-delay="1000" class="item" effect="dark" content="删除" placement="top">-->
            <!--<div style="display: inline-block">-->
            <!--<el-button-->
            <!--:disabled="scope.row.是否禁用删除|| !scope.row.是否禁用增加安全保证金"-->
            <!--size="mini"-->
            <!--type="text"-->
            <!--style="margin-left: 8px"-->
            <!--icon="el-icon-delete"-->
            <!--@click.stop="delSafetyMargin(scope.row)"/>-->
            <!--</div>-->
            <!--</el-tooltip>-->
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 3px;">
        <el-pagination
          :current-page="PageCurrentProject"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="PageSizeProject"
          :total="totalProject"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChangeProject"
          @current-change="handleCurrentChangeProject"/>
      </div>
    </el-card>
    <!--安全保证金表格-->
    <el-card>
      <el-table
        v-loading="tableData1Loading"
        v-show="false"
        :data="tableData1"
        style="width: 100%"
        highlight-current-row
        border
        stripe
        size="mini"
        @row-click="fetchTableData2">
        <!--<el-table-column align="center" label="项目名称" width="300">-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.项目名称 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <el-table-column align="center" label="保证金(万元)" width="110">
          <template slot-scope="scope">
            <el-tag type="primary" size="mini">{{ Number(scope.row.保证金金额).toFixed(2) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="交付日期" width="86">
          <template slot-scope="scope">
            {{ scope.row.交付日期 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="收回条件" width="86">
          <template slot-scope="scope">
            {{ scope.row.执行进度名 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="截止时间(天)" width="120">
          <template slot-scope="scope">
            后{{ scope.row.提前x天提醒 }}天内收回
          </template>
        </el-table-column>
        <el-table-column align="center" label="扣款金额(万元)" width="120">
          <template slot-scope="scope">
            {{ Number(scope.row.扣款金额).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column :label="AmountReceivable" align="center" width="110">
          <template slot-scope="scope">
            {{ Number(scope.row.应收回金额).toFixed(2) }}
          </template>
        </el-table-column>
        <el-table-column label="已收(万元)" align="center" width="110">
          <template slot-scope="scope">
            {{ Number(scope.row.已收金额).toFixed(2) }}
          </template>
        </el-table-column>
        <!--<el-table-column align="center" label="计划收回日期" width="150">-->
        <!--<template slot-scope="scope">-->
        <!--{{ scope.row.预计收回日期 }}-->
        <!--</template>-->
        <!--</el-table-column>-->
        <el-table-column align="center" label="是否收回" width="90">
          <template slot-scope="scope">
            <el-tag v-if="parseInt(scope.row.是否收回) === 2" slot="reference" size="mini" type="success" style="width: 66px">已收回</el-tag>
            <el-tag v-else-if="parseInt(scope.row.是否收回) === 1" slot="reference" type="danger" size="mini" style="width: 66px">未收回</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" label="收回日期" width="86">
          <template slot-scope="scope">
            {{ scope.row.实际收回日期 === '' || scope.row.实际收回日期 === null ? '未收回' : scope.row.实际收回日期 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="状态" width="100">
          <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.是否收回)!== 2 " style="text-align: center">
                <h4>{{ scope.row.执行进度名 }} 未完成</h4>
              </div>
              <div v-show="scope.row.预计收回日期 !== '' && scope.row.预计收回日期 !== null && parseInt(scope.row.是否收回)!== 2">
                <h4>{{ scope.row.执行进度名 }}于{{ scope.row.节点日期 }}完成，计划于{{ scope.row.预计收回日期 }}进行收款，距离计划截止还剩{{ parseInt(scope.row.剩余天数) > 0 ? scope.row.剩余天数 : 0 }}天</h4>
              </div>
              <div v-show="parseInt(scope.row.是否收回)=== 2 " style="text-align: center">
                <h4>已于{{ scope.row.实际收回日期 }}收回</h4>
              </div>
              <el-tag v-if="scope.row.预计收回日期!==''&& scope.row.预计收回日期!==null && parseInt(scope.row.是否收回) === 1 && parseInt(scope.row.状态) === 1" slot="reference" size="mini" type="warning" style="width: 66px">即将拖期</el-tag>
              <el-tag v-else-if="scope.row.预计收回日期!==''&& scope.row.预计收回日期!==null &&parseInt(scope.row.是否收回) === 1 && parseInt(scope.row.状态) === 2" slot="reference" type="danger" size="mini" style="width: 66px">已经拖期</el-tag>
              <el-tag v-else-if="scope.row.预计收回日期!==''&& scope.row.预计收回日期!==null &&parseInt(scope.row.是否收回) === 1 && parseInt(scope.row.状态) === 0" slot="reference" type="primer" size="mini" style="width: 66px">无计划</el-tag>
              <el-tag v-else-if="scope.row.预计收回日期!==''&& scope.row.预计收回日期!==null &&parseInt(scope.row.是否收回) === 2" slot="reference" type="success" size="mini" style="width: 66px">已完成</el-tag>
              <el-tag v-else-if="scope.row.预计收回日期===''||scope.row.预计收回日期===null" slot="reference" size="small" style="width: 66px">进度未完</el-tag>
              <div v-show="false">{{ scope.row.预计收回日期===''||scope.row.预计收回日期===null || parseInt(scope.row.是否收回) === 2 ? scope.row.是否禁用收回 = true : scope.row.是否禁用收回 = false }}</div>
              <div v-show="false">{{ parseInt(scope.row.是否收回) === 2 ? scope.row.是否禁用扣款 = true : scope.row.是否禁用扣款 = false }}</div>
              <div v-show="false">{{ parseInt(scope.row.是否收回) === 2 ? scope.row.是否禁用编辑 = true : scope.row.是否禁用编辑 = false }}</div>
              <div v-show="false">{{ parseInt(scope.row.是否收回) === 2 ? scope.row.是否禁用删除 = true : scope.row.是否禁用删除 = false }}</div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column align="center" width="190" label="备注">
          <template slot-scope="scope">
            {{ scope.row.备注 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="130">
          <template slot-scope="scope">
            <el-tooltip :open-delay="1000" class="item" effect="dark" content="收回" placement="top">
              <div style="display: inline-block">
                <el-button
                  :disabled="scope.row.是否禁用收回"
                  size="mini"
                  type="text"
                  icon="el-icon-money"
                  @click.stop="TakeBack(scope.row);flag1 = 5"/>
              </div>
            </el-tooltip>
            <el-tooltip :open-delay="1000" class="item" effect="dark" content="扣款" placement="top">
              <div style="display: inline-block">
                <el-button
                  :disabled="scope.row.是否禁用扣款"
                  size="mini"
                  type="text"
                  style="margin-left: 8px"
                  icon="el-icon-coin"
                  @click.stop="Withdrawing(scope.row);flag1 = 3"/>
              </div>
            </el-tooltip>
            <el-tooltip :open-delay="1000" class="item" effect="dark" content="编辑" placement="top">
              <div style="display: inline-block">
                <el-button
                  :disabled="scope.row.是否禁用编辑"
                  size="mini"
                  type="text"
                  style="margin-left: 8px"
                  icon="el-icon-edit-outline"
                  @click.stop="editSafetyMargin(scope.row);flag1 = 2"/>
              </div>
            </el-tooltip>
            <el-tooltip :open-delay="1000" class="item" effect="dark" content="删除" placement="top">
              <div style="display: inline-block">
                <el-button
                  :disabled="scope.row.是否禁用删除"
                  size="mini"
                  type="text"
                  style="margin-left: 8px"
                  icon="el-icon-delete"
                  @click.stop="delSafetyMargin(scope.row)"/>
              </div>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <div v-show="false" 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-dialog v-el-drag-dialog :title="title" :visible.sync="dialogFormVisible1" center style="min-height: 300px" width="600px">
      <el-form ref="form" :model="form" :rules="rules1" label-position="left" label-width="105px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="保证金(万元)" prop="保证金金额">
              <el-input-number v-model="form.保证金金额" :precision="2" :step="1" :min="0" style="width: 150px" size="small"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="缴纳日期" prop="交付日期">
              <el-date-picker
                v-model="form.交付日期"
                :picker-options="pickerOptions"
                style="width: 150px;"
                value-format="yyyy-MM-dd"
                size="small"
                type="date"
                placeholder="请选择交付日期"/>
            </el-form-item>
          </el-col>
        </el-row>
        <!--<el-col :span="12">-->
        <!--<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-col>-->
        <!--<el-row>-->
        <!--<el-col :span="12">-->
        <!--<el-form-item label="收回条件" prop="收回条件">-->
        <!--<el-select v-model="form.收回条件" filterable placeholder="请选择收回条件" style="width: 150px" 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="提前X天提醒我">-->
        <!--<el-input-number v-model="form.提前X天提醒我" placeholder="请输入截止时间" style="width: 150px" size="small"/>-->
        <!--</el-form-item>-->
        <!--</el-col>-->
        <!--</el-row>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="备注" prop="备注">
              <el-input v-model="form.备注" type="textarea" autosize size="small" placeholder="请输入备注" style="width:430px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" icon="el-icon-circle-close" @click="callOff('form')">取消</el-button>
        <el-button type="distribution" size="small" icon="el-icon-circle-check" @click="submit1('form')">确 定</el-button>
        <!--<el-button @click="callOff('form')">取消</el-button>-->
        <!--<el-button type="primary" @click="submit1('form')">确 定</el-button>-->
      </div>
    </el-dialog>

    <!--扣款记录增加-->
    <el-dialog v-el-drag-dialog :title="title2" :visible.sync="dialogFormVisible2" center style="min-height: 300px" width="600px">
      <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 v-model="projectName" size="small" placeholder="请输入项目名称" style="width:140px" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="被扣款人" prop="被扣款人">
              <el-input v-model="form2.被扣款人" size="small" placeholder="请输入被扣款人" style="width:140px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="剩余金额(万元)" prop="剩余金额">
              <el-input v-model="SurplusAmount" size="small" placeholder="请输入剩余金额" style="width:140px" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="扣款金额(万元)" prop="扣款金额">
              <el-input-number v-model="form2.扣款金额" :min="0" :precision="2" :step="1" size="small" style="width: 140px" placeholder="请输入本次付款金额"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="扣款日期" prop="扣款日期">
              <el-date-picker
                v-model="form2.扣款日期"
                :picker-options="pickerOptions1"
                style="width: 140px;"
                value-format="yyyy-MM-dd"
                size="small"
                type="date"
                placeholder="扣款日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="扣款原因" prop="扣款原因">
              <el-input v-model="form2.扣款原因" size="small" placeholder="扣款原因" style="width: 140px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" icon="el-icon-circle-close" @click="callOff('form2')">取消</el-button>
        <el-button type="distribution" size="small" icon="el-icon-circle-check" @click="submit1('form2')">确 定</el-button>
      </div>
    </el-dialog>

    <!--扣款记录编辑-->
    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible3" title="编辑" center style="min-height: 300px" width="800px">
      <el-form ref="form3" :model="form3" :rules="rules3" label-position="left" label-width="120px" class="demo-ruleForm">
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="项目名称">
              <el-input v-model="projectName" size="small" placeholder="请输入项目名称" style="width:200px" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="被扣款人" prop="被扣款人">
              <el-input v-model="form3.被扣款人" size="small" placeholder="请输入被扣款人" style="width:200px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="扣款日期" prop="扣款日期">
              <el-date-picker
                v-model="form3.扣款日期"
                :picker-options="pickerOptions1"
                style="width: 200px;"
                value-format="yyyy-MM-dd"
                size="small"
                type="date"
                placeholder="扣款日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="扣款金额" prop="扣款金额">
              <el-input-number v-model="form3.扣款金额" :min="0" :max="maxMoney1" :precision="2" :step="1" size="small" style="width: 200px" placeholder="请输入本次付款金额"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="扣款原因" prop="扣款原因">
              <el-input v-model="form3.扣款原因" size="small" placeholder="扣款原因" style="width: 580px"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" icon="el-icon-circle-close" @click="callOff('form3')">取消</el-button>
        <el-button type="distribution" size="small" icon="el-icon-circle-check" @click="submit1('form3')">确 定</el-button>
        <!--<el-button @click="callOff('form3')">取消</el-button>-->
        <!--<el-button type="primary" @click="submit1('form3')">确 定</el-button>-->
      </div>
    </el-dialog>

    <!--收回安全保证金-->
    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible4" title="收回" center style="min-height: 300px" width="400px">
      <el-form ref="form4" :model="form4" :rules="rules4" label-position="left" label-width="120px" class="demo-ruleForm">
        <el-form-item label="项目名称" prop="项目名称">
          <el-input v-model="projectName" size="small" placeholder="请输入项目名称" style="width:200px" disabled/>
        </el-form-item>
        <el-form-item label="应收金额(万元)" prop="剩余金额">
          <el-input v-model="form4.剩余金额" size="small" placeholder="请输入项目名称" style="width:200px" disabled/>
        </el-form-item>
        <el-form-item label="回收金额(万元)" prop="回收金额">
          <el-input-number v-model="form4.回收金额" :min="0" :max="form4.剩余金额" :precision="2" :step="1" size="small" style="width: 200px" placeholder="请输入回收金额"/>
        </el-form-item>
        <el-form-item label="收回日期" prop="收回日期">
          <el-date-picker
            v-model="form4.收回日期"
            :picker-options="pickerOptions2"
            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 size="small" icon="el-icon-circle-close" @click="callOff('form4')">取消</el-button>
        <el-button type="distribution" size="small" icon="el-icon-circle-check" @click="submit1('form4')">确 定</el-button>
        <!--<el-button @click="callOff('form4')">取消</el-button>-->
        <!--<el-button type="primary" @click="submit1('form4')">确 定</el-button>-->
      </div>
    </el-dialog>

    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible5" title="扣款" center style="min-height: 300px" width="800px">
      <!--扣款记录表格-->
      <el-card>
        <el-table
          v-loading="tableData2Loading"
          :data="tableData2"
          style="width: 100%;"
          height="230"
          highlight-current-row
          border
          stripe
          size="mini">
          <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.扣款原因 }}
            </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="200">
            <template slot-scope="scope">
              <el-button
                :disabled="disabled"
                size="mini"
                type="text"
                icon="el-icon-edit-outline"
                @click.stop="editWithdrawing(scope.row);flag1 = 4">编辑</el-button>
              <el-button
                :disabled="disabled"
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click.stop="delWithdrawing(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="block" style="margin-top: 3px;">
          <el-pagination
            :current-page="PageCurrent2"
            :page-sizes="[10, 20, 30, 40]"
            :page-size="PageSize2"
            :total="total2"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange2"
            @current-change="handleCurrentChange2"/>
        </div>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import elInput from 'element-ui/packages/input'
import { getNowShotTime } from '@/utils/tool'
import { initProject, searchTable1, searchTable2, addSafetyMargin, editSafetyMargin, delSafetyMargin, Withdrawing, delWithdrawing, edtWithdrawing, TakeBack, searchTableDataProject, searchTableDataProject_excel, executionProgressID } from '@/api/MarketingCenter/SafetyMargin'
export default {
  components: {
    elInput
  },
  data() {
    const validateValue = (rule, value, callback) => {
      if (value === 0) {
        callback(new Error('金额不能为0'))
      } else {
        callback()
      }
    }
    return {
      dateRange: '',
      check1: '',
      pickerOptions: {
        shortcuts: [{
          text: '上季度',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 30 * 3)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '过去半年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 366 / 2)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '过去一年',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(end.getTime() - 3600 * 1000 * 24 * 365)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      customerName: '',
      customerName_check: '',
      pickerOptions1: {},
      pickerOptions2: {},
      timetime: '',
      timetime1: '',
      timetime2: '',
      listLoadingProject: false, // 项目信息加载动画
      projectTableData: [], // 项目信息表格
      startTime: '', // 开始时间
      endTime: '', // 结束时间
      PageCurrentProject: 1, // 项目信息表格翻页
      PageSizeProject: 30, // 项目信息表格每页显示条数
      totalProject: null, // 总条数
      plannedTime: [], // 执行情况
      entryNameValue_check: '',
      startTime_check: '',
      endTime_check: '',
      disabled: false,
      AmountReceivable: '应收回(万元)',
      WithdrawingCode: '',
      SurplusAmount: '',
      maxMoney: null,
      maxMoney1: null,
      code: '', // 安全保证金流水号
      projectCode: '',
      projectName: '',
      flag1: null,
      entryName: [], // 项目名称
      entryNameValue: '', // 项目名称数组
      tableData1: [], // 安全保证金信息表数组
      tableData2: [], // 安全保证金信息表数组
      tableData1Loading: false, // 安全保证金信息表加载动画
      tableData2Loading: false, // 安全保证金信息表加载动画
      PageCurrent: 1,
      PageSize: 30,
      total: null,
      PageCurrent2: 1,
      PageSize2: 30,
      total2: null,
      radio: '1',
      title: '',
      title2: '',
      entryName1: [],
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      dialogFormVisible4: false,
      dialogFormVisible5: false,
      form: {
        备注: '',
        项目名称: '',
        保证金金额: '',
        交付日期: '',
        预计收回日期: '',
        提前X天提醒我: 15,
        收回条件: 1
      },
      form2: {
        扣款金额: '',
        被扣款人: '',
        扣款日期: '',
        扣款原因: ''
      },
      form3: {
        扣款金额: '',
        被扣款人: '',
        扣款日期: '',
        扣款原因: ''
      },
      form4: {
        收回日期: '',
        剩余金额: '',
        回收金额: ''
      },
      rules1: { // 表单验证规则
        项目名称: [{ required: true, message: '请选择项目', trigger: 'change' }],
        保证金金额: [{ required: true, message: '请输入保证金金额', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validateValue }],
        交付日期: [{ required: true, message: '请选择交付日期', trigger: 'change' }],
        预计收回日期: [{ required: true, message: '请选择预计收回日期', trigger: 'change' }],
        收回条件: [{ required: true, message: '请选择收回条件', trigger: 'change' }],
        提前X天提醒我: [{ required: true, message: '提前X天提醒我', trigger: 'blur' }]
      },
      rules2: { // 表单验证规则
        扣款金额: [
          { required: true, message: '请输入本次付款金额', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validateValue }],
        被扣款人: [{ required: true, message: '请输入被扣款人', trigger: 'blur' }],
        扣款日期: [{ required: true, message: '请选择扣款日期', trigger: 'change' }]
      },
      rules3: { // 表单验证规则
        扣款金额: [
          { required: true, message: '请输入本次付款金额', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validateValue }],
        被扣款人: [{ required: true, message: '请输入被扣款人', trigger: 'blur' }],
        扣款日期: [{ required: true, message: '请选择扣款日期', trigger: 'change' }]
      },
      rules4: { // 表单验证规则
        收回日期: [{ required: true, message: '请选择收回日期', trigger: 'change' }],
        回收金额: [{ required: true, message: '请输入回收金额', trigger: 'blur' },
          { required: true, trigger: 'blur', validator: validateValue }]
      }
    }
  },
  created() {
    this.init()
    this.searchTableDataProject()
  },
  methods: {
    formatJson(filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    exportExcel() {
      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
      }
      if (this.customerName === '' || this.customerName === null) {
        this.customerName_check = 0
      } else {
        this.customerName_check = 1
      }
      // llj新增
      this.dateRange ? this.check1 = 1 : (this.check1 = 0, this.dateRange = '')
      searchTableDataProject_excel(this.entryNameValue, this.entryNameValue_check, this.check1, this.check1, this.dateRange[0], this.dateRange[1], this.customerName_check, this.customerName).then(val => {
        console.log(val.data[0])
        for (let i = 0; i < val.data.length; i++) {
          val.data[i].保证金金额 === null ? val.data[i].保证金金额 = '0.00' : val.data[i].保证金金额 = (val.data[i].保证金金额).toFixed(2)
          val.data[i].扣款金额 === null ? val.data[i].扣款金额 = '0.00' : val.data[i].扣款金额 = (val.data[i].扣款金额).toFixed(2)
          val.data[i].应收回金额 === null ? val.data[i].应收回金额 = '0.00' : val.data[i].应收回金额 = (val.data[i].应收回金额).toFixed(2)
          val.data[i].已收金额 === null ? val.data[i].已收金额 = '0.00' : val.data[i].已收金额 = (val.data[i].已收金额).toFixed(2)
          val.data[i].未收金额 === null ? val.data[i].未收金额 = '0.00' : val.data[i].未收金额 = (val.data[i].未收金额).toFixed(2)
          // val.data[i].扣款金额 === null ? val.data[i].扣款金额 = 0.00 : (val.data[i].扣款金额).toFixed(2)
          // val.data[i].应收回金额 === null ? 0.00 : (val.data[i].应收回金额).toFixed(2)
          // val.data[i].已收金额 === null ? 0.00 : (val.data[i].已收金额).toFixed(2)
          if (val.data[i].交付日期 !== '' && val.data[i].交付日期 !== null) {
            val.data[i].交付日期 = val.data[i].交付日期.substring(0, val.data[i].交付日期.length - 8)
          }
        }
        if (val.length !== 0) {
          import('@/vendor/Export2Excel').then(excel => {
            const tHeader = ['合同编号', '项目名称', '客户名称', '安全金(万元)', '缴纳时间', '扣款金额(万元)', '扣款原因', '应收(万元)', '已收(万元)', '未收(万元)', '营销经理', '合同信息备注', '备注']
            const filterVal = ['合同编号', '项目名称', '客户名称', '保证金金额', '交付日期', '扣款金额', '扣款原因', '应收回金额', '已收金额', '未收金额', '姓名', '合同信息备注', '备注']
            const list = val.data
            const data = this.formatJson(filterVal, list)
            excel.export_json_to_excel({
              header: tHeader,
              data,
              filename: getNowShotTime() + '安全保证金',
              autoWidth: true,
              bookType: 'xlsx'
            })
          })
        } else {
          this.$message.warning('没有导出内容')
        }
      })
    },
    changeTime() {
      this.pickerOptions = Object.assign({}, this.pickerOptions, {
        disabledDate: (time) => {
          console.log(this.timetime)
          return time.getTime() < Date.parse(this.timetime)
        }
      })
    },
    changeTime1() {
      this.pickerOptions1 = Object.assign({}, this.pickerOptions1, {
        disabledDate: (time) => {
          console.log(this.timetime1)
          return time.getTime() < Date.parse(this.timetime1)
        }
      })
    },
    changeTime2() {
      this.pickerOptions2 = Object.assign({}, this.pickerOptions2, {
        disabledDate: (time) => {
          console.log(this.timetime2)
          return time.getTime() < Date.parse(this.timetime2)
        }
      })
    },
    init() {
      this.getSelect(initProject, ['项目名称', '项目流水号'], 'entryName')
      this.getSelect(initProject, ['项目名称', '项目流水号'], 'entryName1')
      this.getSelect(executionProgressID, ['执行进度名', '执行进度ID'], 'plannedTime')
    },
    // 查询合同(项目)信息
    searchTableDataProject() {
      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
      }
      if (this.customerName === '' || this.customerName === null) {
        this.customerName_check = 0
      } else {
        this.customerName_check = 1
      }
      // llj新增
      this.dateRange ? this.check1 = 1 : (this.check1 = 0, this.dateRange = '')
      this.listLoadingProject = true
      searchTableDataProject(this.PageCurrentProject, this.PageSizeProject, this.entryNameValue, this.entryNameValue_check, this.check1, this.check1, this.dateRange[0], this.dateRange[1], this.customerName_check, this.customerName).then(response => {
        this.tableData1 = []
        this.tableData2 = []
        this.listLoadingProject = 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].安全保证金类型) === 1 ? data.rows[i].是否禁用增加安全保证金 = true : data.rows[i].是否禁用增加安全保证金 = false
          data.rows[i].交付日期 = data.rows[i].交付日期.substring(0, data.rows[i].交付日期.length - 8)
        }

        this.projectTableData = data.rows
        console.log(this.projectTableData, 909090)
        this.totalProject = data.total
      })
    },
    // 项目信息表格改变每页显示条数
    handleSizeChangeProject(val) {
      this.PageCurrentProject = 1
      this.PageSizeProject = val
      this.searchTableDataProject()
    },
    // 项目信息表格翻页
    handleCurrentChangeProject(val) {
      this.PageCurrentProject = val
      this.searchTableDataProject()
    },
    fetchTableData1(row) {
      this.projectCode = row.项目流水号
      this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.projectCode], ['tableData1', 'total', 'tableData1Loading'])
      this.tableData1 = []
      this.tableData2 = []
    },
    fetchTableData2(row) {
      this.code = row.安全保证金流水号
      this.SurplusAmount = row.应收回金额
      this.projectName = row.项目名称
      this.getTable(searchTable2, [this.PageCurrent2, this.PageSize2, this.code], ['tableData2', 'total2', 'tableData2Loading'])
      this.dialogFormVisible5 = true
    },
    handleSizeChange(val) {
      this.PageCurrent = 1
      this.PageSize = val
      this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.projectCode], ['tableData1', 'total', 'tableData1Loading'])
      this.tableData1 = []
      this.tableData2 = []
    },
    handleCurrentChange(val) {
      this.PageCurrent = val
      this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.projectCode], ['tableData1', 'total', 'tableData1Loading'])
      this.tableData1 = []
      this.tableData2 = []
    },
    handleSizeChange2(val) {
      this.PageCurrent2 = 1
      this.PageSize2 = val
      this.fetchTableData2()
    },
    handleCurrentChange2(val) {
      this.PageCurrent2 = val
      this.fetchTableData2()
    },
    addSafetyMargin(row) {
      this.title = '新增安全保证金'
      this.timetime = row.合同签订日期
      this.changeTime()
      this.dialogFormVisible1 = true
      this.projectCode = row.项目流水号
      this.addForm('form')
    },
    editSafetyMargin(row) {
      this.editForm(row, 'form')
      this.title = '编辑安全保证金'
      this.dialogFormVisible1 = true
      this.form.保证金金额 = row.保证金金额
      this.form.交付日期 = row.交付日期
      this.form.收回条件 = parseInt(row.执行进度流水号)
      this.form.提前X天提醒我 = row.提前x天提醒
      this.form.备注 = row.备注
      this.code = row.安全保证金流水号
      this.projectCode = row.项目流水号
    },
    addTableData1() {
      addSafetyMargin(this.projectCode, this.form.保证金金额, this.form.交付日期, this.form.提前X天提醒我, this.form.收回条件, this.form.备注).then(response => {
        this.dialogFormVisible1 = false
        if (response.data[0].result === '1') {
          this.$message.success('添加成功')
          this.title = ''
          this.searchTableDataProject()
          this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.projectCode], ['tableData1', 'total', 'tableData1Loading'])
          this.tableData2 = []
        } else {
          this.$message.error('添加失败')
        }
      })
    },
    editTableData1() {
      editSafetyMargin(this.projectCode, this.form.保证金金额, this.form.交付日期, this.form.提前X天提醒我, this.form.收回条件, this.form.备注, this.code).then(response => {
        this.dialogFormVisible1 = false
        if (response.data[0].result === '1') {
          this.$message.success('信息更新成功')
          this.searchTableDataProject()
          this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.projectCode], ['tableData1', 'total', 'tableData1Loading'])
          this.tableData2 = []
        } else {
          this.$message.error('信息更新失败')
        }
      })
    },
    // 提交保证金信息添加或者修改
    submit1(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.flag1 === 1) {
            this.addTableData1()
            this.flag1 = null
          } else if (this.flag1 === 2) {
            this.editTableData1()
            this.flag1 = null
          } else if (this.flag1 === 3) {
            this.addTableData2()
            this.flag1 = null
          } else if (this.flag1 === 4) {
            this.edtTableData2()
            this.flag1 = null
          } else if (this.flag1 === 5) {
            this.submitTakeBack()
            this.flag1 = null
          }
        }
      })
    },
    callOff() {
      this.dialogFormVisible1 = false
      this.dialogFormVisible2 = false
      this.dialogFormVisible3 = false
      this.dialogFormVisible4 = false
    },
    delSafetyMargin(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delSafetyMargin(row.安全保证金流水号, this.projectCode).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTableDataProject()
            this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.projectCode], ['tableData1', 'total', 'tableData1Loading'])
            this.tableData2 = []
          } else {
            this.$message.error('数据占用')
          }
        })
      })
    },
    delWithdrawing(row) {
      function a() {
        this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.projectCode], ['tableData1', 'total', 'tableData1Loading'])
        this.getTable(searchTable2, [this.PageCurrent2, this.PageSize2, this.code], ['tableData2', 'total2', 'tableData2Loading'])
        this.dialogFormVisible2 = false
      }
      this.deleteRow(delWithdrawing, row.扣款流水号, a)
    },
    Withdrawing(row) {
      this.title2 = '扣款'
      this.timetime1 = row.交付日期
      console.log(row)
      this.changeTime1()
      this.dialogFormVisible2 = true
      this.code = row.安全保证金流水号
      this.projectName = row.项目名称
      this.maxMoney = parseFloat(row.应收回金额)
      this.SurplusAmount = row.应收回金额
      this.addForm('form2')
    },
    addTableData2() {
      Withdrawing(this.code, this.form2.扣款金额, this.form2.被扣款人, this.form2.扣款原因, this.form2.扣款日期).then(response => {
        this.dialogFormVisible2 = false
        if (response.data[0].result === '1') {
          this.$message.success('扣款成功')
          this.title = ''
          this.searchTableDataProject()
          this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.projectCode], ['tableData1', 'total', 'tableData1Loading'])
          this.getTable(searchTable2, [this.PageCurrent2, this.PageSize2, this.code], ['tableData2', 'total2', 'tableData2Loading'])
        } else {
          this.$message.error('扣款失败')
        }
      })
    },
    editWithdrawing(row) {
      this.editForm(row, 'form3')
      this.dialogFormVisible3 = true
      this.maxMoney1 = parseFloat(this.SurplusAmount) + parseFloat(row.扣款金额)
      this.form3.扣款金额 = row.扣款金额
      this.form3.被扣款人 = row.被扣款人
      this.form3.扣款原因 = row.扣款原因
      this.form3.扣款日期 = row.扣款日期
      this.WithdrawingCode = row.扣款流水号
    },
    edtTableData2() {
      edtWithdrawing(this.WithdrawingCode, this.form3.扣款金额, this.form3.被扣款人, this.form3.扣款原因, this.form3.扣款日期).then(response => {
        this.dialogFormVisible3 = false
        if (response.data[0].result === '1') {
          this.$message.success('信息更新成功')
          this.searchTableDataProject()
          this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.projectCode], ['tableData1', 'total', 'tableData1Loading'])
          this.getTable(searchTable2, [this.PageCurrent2, this.PageSize2, this.code], ['tableData2', 'total2', 'tableData2Loading'])
        } else {
          this.$message.error('信息更新失败')
        }
      })
    },
    TakeBack(row) {
      this.code = row.安全保证金流水号
      this.projectName = row.项目名称
      this.timetime2 = row.交付日期
      this.changeTime2()
      this.form4.剩余金额 = parseFloat(row.应收回金额)
      this.dialogFormVisible4 = true
      this.form4.回收金额 = 0
    },
    submitTakeBack() {
      TakeBack(this.code, this.form4.收回日期, this.form4.回收金额).then(response => {
        this.dialogFormVisible4 = false
        if (response.data[0].result === '1') {
          this.$message.success('信息更新成功')
          this.searchTableDataProject()
          this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.projectCode], ['tableData1', 'total', 'tableData1Loading'])
        } else {
          this.$message.error('信息更新失败')
        }
      })
    }
  }
}
</script>

<style scoped>
  .el-date-picker{
    width:50px
  }
  .el-form--label-left >>> .el-form-item__label {
    text-align: right;
  }
  >>>.el-dialog--center .el-dialog__body {
    text-align: initial;
    padding: 20px 20px 20px
  }
  >>>.el-dialog__header {
    padding: 20px 20px 0px;
  }
  >>>.el-dialog__footer {
    padding: 0px 20px 20px;
    /*text-align: right;*/
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  >>>.el-form-item {
    margin-bottom: 13px;
  }
</style>
