<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="108px">
      <el-form-item label="合同名称" prop="contractId">
        <el-select v-model="queryParams.contractId" clearable filterable remote reserve-keyword placeholder="请输入合同名称"
          :remote-method="remoteMethod" :loading="loading" style="width: 400px;" @change="handleQuery">
          <el-option v-for="contract in contractList" :key="contract.contractId"
            :label="contract.contractName" :value="contract.contractId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="预计退还日期" prop="returnTime">
        <el-date-picker clearable v-model="queryParams.returnTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择预计退还日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="申请人" prop="applier">
        <el-select v-model="queryParams.applier" filterable clearable @change="handleQuery">
          <el-option v-for="item in $store.state.user.userList" :key="item.userId" :label="item.nickName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['oa:deposit:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="depositList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <!-- <el-table-column label="保证金类型" align="center" prop="depositType" >
        <template slot-scope="scope">
          <span>{{ scope.row.depositType == '0' ? '投标保证金' : '合同保证金' }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="合同名称" align="center" prop="contract.contractName" />
      <!-- <el-table-column label="开户名称" align="center" prop="accountName" />
      <el-table-column label="开户银行" align="center" prop="accountBank" />
      <el-table-column label="银行卡号" align="center" prop="cardNumber" /> -->
      <el-table-column label="保证金形式" align="center" prop="depositWay" >
        <template slot-scope="scope">
          <span>{{ scope.row.depositWay == '1' ? '电汇' : (scope.row.depositWay == '2' ? '现金' : '汇票') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请人" align="center" prop="applierUser.nickName" />
      <el-table-column label="申请日期" align="center" prop="applyTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="申请说明" align="center" prop="applyReason" />
      <el-table-column label="相关附件" align="center" prop="applyDocument" /> -->
      <!-- <el-table-column label="分管审批意见" align="center" prop="managerComment" />
      <el-table-column label="分管审批时间" align="center" prop="managerTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.managerTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分管审批人" align="center" prop="managerUserId" />
      
      <el-table-column label="财务处理时间" align="center" prop="cwTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.cwTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="汇款日期" align="center" prop="remitTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.remitTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计退还日期" align="center" prop="returnTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="退还时间" align="center" prop="backTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.backTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="财务打款备注" align="center" prop="cwComment" />
      <el-table-column label="财务处理人" align="center" prop="cwUserId" />
      <!-- <el-table-column label="汇款凭证" align="center" prop="remitDocument" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)"
            v-hasPermi="['oa:deposit:query']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改cmc保证金审批对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同id" prop="contractId">
          <el-input v-model="form.contractId" placeholder="请输入合同id" />
        </el-form-item>
        <el-form-item label="预计退还日期" prop="returnTime">
          <el-date-picker clearable v-model="form.returnTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择预计退还日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="开户名称" prop="accountName">
          <el-input v-model="form.accountName" placeholder="请输入开户名称" />
        </el-form-item>
        <el-form-item label="开户银行" prop="accountBank">
          <el-input v-model="form.accountBank" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="cardNumber">
          <el-input v-model="form.cardNumber" placeholder="请输入银行卡号" />
        </el-form-item>
        <el-form-item label="保证金形式" prop="depositWay">
          <el-input v-model="form.depositWay" placeholder="请输入保证金形式" />
        </el-form-item>
        <el-form-item label="申请人" prop="applier">
          <el-input v-model="form.applier" placeholder="请输入申请人" />
        </el-form-item>
        <el-form-item label="申请日期" prop="applyTime">
          <el-date-picker clearable v-model="form.applyTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择申请日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请说明" prop="applyReason">
          <el-input v-model="form.applyReason" placeholder="请输入申请说明" />
        </el-form-item>
        <el-form-item label="相关附件" prop="applyDocument">
          <el-input v-model="form.applyDocument" placeholder="请输入相关附件" />
        </el-form-item>
        <el-form-item label="分管审批意见" prop="managerComment">
          <el-input v-model="form.managerComment" placeholder="请输入分管审批意见" />
        </el-form-item>
        <el-form-item label="分管审批时间" prop="managerTime">
          <el-date-picker clearable v-model="form.managerTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择分管审批时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="分管审批人" prop="managerUserId">
          <el-input v-model="form.managerUserId" placeholder="请输入分管审批人" />
        </el-form-item>
        <el-form-item label="汇款日期" prop="remitTime">
          <el-date-picker clearable v-model="form.remitTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择汇款日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="财务处理时间" prop="cwTime">
          <el-date-picker clearable v-model="form.cwTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择财务处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="退还时间" prop="backTime">
          <el-date-picker clearable v-model="form.backTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择退还时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="财务打款备注" prop="cwComment">
          <el-input v-model="form.cwComment" placeholder="请输入财务打款备注" />
        </el-form-item>
        <el-form-item label="财务处理人" prop="cwUserId">
          <el-input v-model="form.cwUserId" placeholder="请输入财务处理人" />
        </el-form-item>
        <el-form-item label="汇款凭证" prop="remitDocument">
          <el-input v-model="form.remitDocument" placeholder="请输入汇款凭证" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="infoOpen" width="50%" append-to-body>
      <deposit-form :taskForm="taskForm" :taskName="''" :flowDisabled="false"></deposit-form>
    </el-dialog>
  </div>
</template>

<script>
import { listDeposit, getDeposit, delDeposit, addDeposit, updateDeposit } from "@/api/oa/deposit/deposit";
import { listContract } from "@/api/oa/contract/contract";
import depositForm from '../../flowable/form/finance/depositForm.vue';

export default {
  name: "Deposit",
  components: { depositForm, },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc保证金审批表格数据
      depositList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      infoOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        depositType: null,
        contractId: null,
        returnTime: null,
        accountName: null,
        accountBank: null,
        cardNumber: null,
        depositWay: null,
        applier: null,
        applyTime: null,
        applyReason: null,
        applyDocument: null,
        managerComment: null,
        managerTime: null,
        managerUserId: null,
        remitTime: null,
        cwTime: null,
        backTime: null,
        cwComment: null,
        cwUserId: null,
        remitDocument: null
      },
      // 表单参数
      form: {},
      taskForm: {
        formId:''
      },
      // 表单校验
      rules: {
      },
      contractList:[]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc保证金审批列表 */
    getList() {
      this.loading = true;
      this.queryParams.depositType = '1';
      listDeposit(this.queryParams).then(response => {
        this.depositList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        depositId: null,
        depositType: null,
        contractId: null,
        returnTime: null,
        accountName: null,
        accountBank: null,
        cardNumber: null,
        depositWay: null,
        applier: null,
        applyTime: null,
        applyReason: null,
        applyDocument: null,
        managerComment: null,
        managerTime: null,
        managerUserId: null,
        remitTime: null,
        cwTime: null,
        backTime: null,
        cwComment: null,
        cwUserId: null,
        remitDocument: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.depositId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc保证金审批";
    },
    handleView(row){
      this.taskForm.formId = row.depositId;
      this.infoOpen = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const depositId = row.depositId || this.ids
      getDeposit(depositId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc保证金审批";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.depositId != null) {
            updateDeposit(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDeposit(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const depositIds = row.depositId || this.ids;
      this.$modal.confirm('是否确认删除cmc保证金审批编号为"' + depositIds + '"的数据项？').then(function () {
        return delDeposit(depositIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/deposit/export', {
        ...this.queryParams
      }, `deposit_${new Date().getTime()}.xlsx`)
    },
    remoteMethod(val) {
      listContract({
        pageNum: 1,
        pageSize: 20, 
        contractName: val
      }).then(res => {
        this.contractList = res.rows;
      })
    },
  }
};
</script>
