<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="128px">
      <el-form-item label="合同名称" prop="subContractName">
        <el-input v-model="queryParams.subContractName" placeholder="请输入合同名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="承接单位" prop="partnerId">
        <el-select v-model="queryParams.partnerId" clearable filterable remote reserve-keyword placeholder="请输入承接单位"
          :remote-method="remoteMethod" :loading="loading" style="width: 400px;">
          <el-option v-for="partner in partnerList" :key="partner.partnerId" :label="partner.partnerName"
            :value="partner.partnerId">
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="拟稿人" prop="drafter">
        <el-select v-model="queryParams.drafter" 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:subContract:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border :data="subContractList" @selection-change="handleSelectionChange">
      <el-table-column type="index" label="序号" width="55" align="center" />
      <el-table-column label="合同名称" align="center" prop="subContractName" width="200px" />
      <el-table-column label="分包合同金额" align="center" prop="subAmount" />
      <el-table-column label="承接单位" align="center" prop="partner.partnerName" />
      <!-- <el-table-column label="承接单位联系人" align="center" prop="contactPerson" /> -->
      <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="remark" /> -->
      <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">
        <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'">
            {{ setCommentType(scope.row.commentType) }}
          </el-tag>
        </template>
      </el-table-column>
      <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:subContract: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="subContractName">
          <el-input v-model="form.subContractName" placeholder="请输入合同名称" />
        </el-form-item>
        <el-form-item label="分包合同金额" prop="subAmount">
          <el-input v-model="form.subAmount" placeholder="请输入分包合同金额" />
        </el-form-item>
        <el-form-item label="承接单位id" prop="partnerId">
          <el-input v-model="form.partnerId" placeholder="请输入承接单位id" />
        </el-form-item>
        <el-form-item label="承接单位联系人" prop="contactPerson">
          <el-input v-model="form.contactPerson" 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" append-to-body width="70%">
      <sub-contract :taskForm="taskForm" :taskName="''" :flowDisabled="false"></sub-contract>
    </el-dialog>
  </div>
</template>

<script>
import { listSubContract, getSubContract, delSubContract, addSubContract, updateSubContract } from "@/api/oa/contract/subContract";
import { listPartner } from "@/api/oa/partner/partner";
import subContract from '../../flowable/form/business/subContract.vue';

export default {
  components: { subContract },
  name: "SubContract",
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cmc分包合同评审表格数据
      subContractList: [],
      partnerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractId: null,
        subContractName: null,
        subAmount: null,
        partnerId: null,
        contactPerson: null,
        drafter: null,
        draftTime: null,
        signDate: null,
        signRemark: null,
        signScan: null,
        commentType: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      taskForm: {
        formId: ''
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询cmc分包合同评审列表 */
    getList() {
      this.loading = true;
      listSubContract(this.queryParams).then(response => {
        this.subContractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    remoteMethod(val) {
      listPartner({
        pageNum: 1,
        pageSize: 20, 
        partnerName: val
      }).then(res => {
        this.partnerList = res.rows;
      })
    },
    handleView(row) {
      this.openInfo = true;
      this.title = "合同详情";
      this.form = row;
      this.$set(this.taskForm, 'formId', row.subContractId)
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        subContractId: null,
        contractId: null,
        subContractName: null,
        subAmount: null,
        partnerId: null,
        contactPerson: 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.subContractId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加cmc分包合同评审";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const subContractId = row.subContractId || this.ids
      getSubContract(subContractId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改cmc分包合同评审";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.subContractId != null) {
            updateSubContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSubContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const subContractIds = row.subContractId || this.ids;
      this.$modal.confirm('是否确认删除cmc分包合同评审编号为"' + subContractIds + '"的数据项？').then(function () {
        return delSubContract(subContractIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('oa/subContract/export', {
        ...this.queryParams
      }, `subContract_${new Date().getTime()}.xlsx`)
    },
    setCommentType(commentType) {
      if (commentType == '2') {
        return '线上评审'
      } else if (commentType == '0') {
        return '无须评审'
      } else {
        return '会议评审'
      }
    }
  }
};
</script>
