<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="18" :xs="24">
        <h2 class="text-center">保证金审批表</h2>
        <el-divider></el-divider>
        <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="投标项目id" prop="tenderId">
            <el-select v-model="form.tenderId" filterable placeholder="请选择" @change="handleSelectTender"
              :disabled="taskName != '保证金申请'" clearable>
              <el-option v-for="item in tenderList" :key="item.value" :label="item.projectName" :value="item.tenderId">
              </el-option>
            </el-select>
          </el-form-item>
          <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="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>
        <div style="text-align: center;">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-col>
      <el-col :span="6" :xs="24">
        <el-card>
          <h2 style="text-align: center;">流程进度</h2>
          <div>
            <flow :flowData="flowData" />
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { parseTime } from "@/utils/ruoyi";
import { listTender, getTender } from "@/api/oa/tender/tender";
import { listDeposit, getDeposit, delDeposit, addDeposit, updateDeposit } from "@/api/oa/deposit/deposit";
import { complete, getNextFlowNode } from "@/api/flowable/todo";
import flow from '@/views/flowable/task/todo/detail/flow'
import { flowXmlAndNode } from "@/api/flowable/definition";

export default {
  components: {
    flow
  },
  props: {
    taskName: {
      type: String,
      required: true
    },
    taskForm: {
      type: Object,
      required: true
    }
  },
  name: "Deposit",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc保证金审批表格数据
      depositList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        depositType: null,
        tenderId: null,
        contractId: null,
        returnTime: null,
        accountName: null,
        accountBank: null,
        cardNumber: null,
        depositWay: null,
        applier: null,
        applyTime: null,
        applyReason: null,
        applyDocument: null,
        managerTime: null,
        managerUserId: null,
        remitTime: null,
        cwTime: null,
        backTime: null,
        cwComment: null,
        cwUserId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      chooseTender: {},
      isSelect: false,
      formTotal: 0,
      flowData: {},
      tenderList: [],
    };
  },
  mounted() {
    this.initForm();
  },
  created() {
    this.getTenderList();
    this.getList();
    flowXmlAndNode({ procInsId: this.taskForm.procInsId, deployId: this.taskForm.deployId }).then(res => {
      this.flowData = res.data;
    })
  },
  methods: {
    // 初始化表单
    initForm() {
      getDeposit(this.taskForm.formId).then(res => {
        if (this.isEmptyObject(res.data)) {
          this.formTotal = 0;
          this.form.applier = this.$store.getters.userId;
          this.form.applyTime = parseTime(new Date(), '{y}-{m}-{d}')
        }
        else {
          this.formTotal = 1;
          this.form = res.data;
          if (res.data.tenderId) {
            getTender(data.tenderId).then(response => {
              this.chooseTender = response.data;
              this.isSelect = true
            })
          }
          this.loading = false
        }
      })
    },
    /** 查询cmc保证金审批列表 */
    getList() {
      this.loading = true;
      listDeposit(this.queryParams).then(response => {
        this.depositList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 查询项目列表
    getTenderList() {
      this.loading = true
      listTender({
        pageNum: 1,
        pageSize: 99999999
      }).then(response => {
        this.tenderList = response.rows;
      })
    },
    // 选择项目
    handleSelectTender(val) {
      if (val != "" && val != undefined && val != null) {
        getTender(val).then(res => {
          this.chooseTender = res.data;
          this.isSelect = true;
        })
      } else {
        this.isSelect = false
      }
    },
    isEmptyObject(obj) {
      for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
          return false;
        }
      }
      return true;
    },
    // 表单重置
    reset() {
      this.form = {
        depositId: null,
        depositType: null,
        tenderId: null,
        contractId: null,
        returnTime: null,
        accountName: null,
        accountBank: null,
        cardNumber: null,
        depositWay: null,
        applier: null,
        applyTime: null,
        applyReason: null,
        applyDocument: null,
        managerTime: null,
        managerUserId: null,
        remitTime: null,
        cwTime: null,
        backTime: null,
        cwComment: null,
        cwUserId: 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保证金审批";
    },
    /** 修改按钮操作 */
    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.formTotal != 0) {
            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`)
    }
  }
};
</script>
