<!--
 * @Author: ysh
 * @Date: 2024-03-18 09:05:41
 * @LastEditors: wrh
 * @LastEditTime: 2024-07-03 11:33:28
-->
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="合同名称" prop="contractName">
        <el-input v-model="queryParams.contractName" placeholder="请输入合同名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="合同编码" prop="contractCode">
        <el-input v-model="queryParams.contractCode" placeholder="请输入合同编码" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="合同编号" prop="contractNumber">
        <el-input v-model="queryParams.contractNumber" placeholder="请输入合同编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="拟稿人" prop="drafter">
        <el-select v-model="queryParams.drafter" filterable clearable>
          <el-option :key="item.userId" v-for="item of $store.state.user.userList" :label="item.nickName"
            :value="item.userId"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="拟稿日期" prop="draftTime">
        <el-date-picker clearable v-model="queryParams.draftTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择拟稿日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="签订日期" prop="signDate">
        <el-date-picker clearable v-model="queryParams.signDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择签订日期">
        </el-date-picker>
      </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="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['oa:contract:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['oa:contract:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['oa:contract:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['oa:contract:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="contractList" @selection-change="handleSelectionChange">
      <!-- <el-table-column label="合同id" align="center" prop="contractId" /> -->
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="合同名称" align="center" prop="contractName" width="220px" />
      <el-table-column label="合同金额" align="center" prop="amount" />
      <el-table-column label="甲方单位" align="center" prop="partyA.partyAName" width="220px" />
      <el-table-column label="合同编码" align="center" prop="contractCode" />
      <el-table-column label="合同编号" align="center" prop="contractNumber" />
      <!-- <el-table-column label="履约保证金" align="center" prop="deposit" /> -->
      <el-table-column label="合同文件" align="center" prop="contractDocument" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link type="primary" @click="reviewWord(`${baseUrl}${'/profile/upload' + scope.row.contractDocument}`)">
            {{ getFileName(scope.row.contractDocument) }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="拟稿日期" align="center" prop="draftTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.draftTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="签订日期" align="center" prop="signDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="签订备注" align="center" prop="signRemark" /> -->
      <el-table-column label="签订扫描件" align="center" prop="signScan" show-overflow-tooltip>
        <template slot-scope="scope">
          <el-link type="primary" @click="reviewWord(`${baseUrl}${'/profile/upload' + scope.row.signScan}`)">
            {{ getFileName(scope.row.signScan) }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="评审方式" align="center" prop="commentType">
        <template slot-scope="scope">
          <el-tag :type="scope.row.commentType == '2' ? 'success' : 'warning'">
            {{ getCommentType(scope.row.commentType) }}
          </el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="合同备注" align="center" prop="remark" /> -->
      <el-table-column label="拟稿人" align="center" prop="drafter">
        <template slot-scope="scope">
          {{ getUserName(scope.row.drafter) }}
        </template>
      </el-table-column>
      <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:contract:query']">查看</el-button>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['oa:contract:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['oa:contract:remove']">删除</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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="合同编号" prop="contractNumber">
          <el-input v-model="form.contractNumber" placeholder="请输入合同编号" />
        </el-form-item>
        <el-form-item label="合同金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入合同金额" />
        </el-form-item>
        <el-form-item label="履约保证金" prop="deposit">
          <el-input v-model="form.deposit" placeholder="请输入履约保证金" />
        </el-form-item>
        <el-form-item label="合同文件" prop="contractDocument">
          <el-input v-model="form.contractDocument" placeholder="请输入合同文件" />
        </el-form-item>
        <el-form-item label="拟稿人" prop="drafter">
          <el-input v-model="form.drafter" placeholder="请输入拟稿人" />
        </el-form-item>
        <el-form-item label="拟稿日期" prop="draftTime">
          <el-date-picker clearable v-model="form.draftTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择拟稿日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入合同备注" />
        </el-form-item>
        <el-form-item label="签订日期" prop="signDate">
          <el-date-picker clearable v-model="form.signDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择签订日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="签订备注" prop="signRemark">
          <el-input v-model="form.signRemark" placeholder="请输入签订备注" />
        </el-form-item>
        <el-form-item label="签订扫描件" prop="signScan">
          <el-input v-model="form.signScan" 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="openInfo" width="80%" append-to-body>
      <!-- <contract-info :form="form"></contract-info> -->
      <contract-form :taskForm="taskForm" :taskName="''" :flowDisabled="false"></contract-form>

    </el-dialog>
  </div>
</template>

<script>
import { listContract, getContract, delContract, addContract, updateContract } from "@/api/oa/contract/contract";
import contractInfo from './contractInfo.vue';
import ContractForm from '../../flowable/form/business/contractForm.vue';
export default {
  components: { contractInfo, ContractForm },
  name: "Contract",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc合同评审表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractNumber: null,
        amount: null,
        deposit: null,
        contractDocument: null,
        drafter: null,
        draftTime: null,
        signDate: null,
        signRemark: null,
        signScan: null,
        commentType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      project: {},
      taskForm:{}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc合同评审列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;

        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getCommentType(row) {
      let name = '';
      if (row == '0') {
        name = '无须评审'
      } else if (row == '1') {
        name = '会议评审'
      } else {
        name = '线上评审'
      }
      return name
    },
    // 表单重置
    reset() {
      this.form = {
        contractId: null,
        contractNumber: null,
        amount: null,
        deposit: null,
        contractDocument: null,
        drafter: null,
        draftTime: null,
        remark: null,
        signDate: null,
        signRemark: null,
        signScan: null,
        commentType: 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.contractId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc合同评审";
    },
    handleView(row) {
      this.openInfo = true;
      this.title = "合同详情";
      this.form = row;
      this.$set(this.taskForm,'formId',row.contractId)
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contractId = row.contractId || this.ids
      getContract(contractId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc合同评审";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.contractId != null) {
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contractIds = row.contractId || this.ids;
      this.$modal.confirm('是否确认删除cmc合同评审编号为"' + contractIds + '"的数据项？').then(function () {
        return delContract(contractIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/contract/export', {
        ...this.queryParams
      }, `contract_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
