<template>
  <div class="app-container">
    <el-card>
      <el-form
        :model="queryParams"
        stripe
        border
        ref="queryForm"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <div class="filter-box-new">
          <div class="filter-lf">
            <el-form-item label="协议编号" prop="contractNo">
              <el-input
                v-model="queryParams.contractNo"
                placeholder="请输入协议编号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <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="agreementAmt">
              <el-input
                v-model="queryParams.agreementAmt"
                placeholder="请输入协议价格"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="甲方编码" prop="orgNo">
              <el-input
                v-model="queryParams.orgNo"
                placeholder="请输入甲方编码"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="甲方名称" prop="orgName">
              <el-input
                v-model="queryParams.orgName"
                placeholder="请输入甲方名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="乙方编码" prop="supplyNo">
              <el-input
                v-model="queryParams.supplyNo"
                placeholder="请输入乙方编码"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
            <el-form-item label="乙方名称" prop="supplyName">
              <el-input
                v-model="queryParams.supplyName"
                placeholder="请输入乙方名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </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>
          </div>
        </div>
      </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="['ct:agreement: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="['ct:agreement: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="['ct:agreement: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="['ct:agreement:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        stripe
        border
        :data="agreementList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="协议编号" align="center" prop="contractNo" />
        <el-table-column label="协议名称" align="center" prop="contractName" />
        <el-table-column label="应付款" align="center" prop="needCharge" />
        <el-table-column label="预付款" align="center" prop="advanceCharge" />
        <el-table-column label="协议价格" align="center" prop="agreementAmt" />
        <el-table-column
          label="定价基准"
          align="center"
          prop="pricingBenchmark"
        />
        <el-table-column label="甲方名称" align="center" prop="orgName" />
        <el-table-column label="甲方法人" align="center" prop="orgFr" />
        <el-table-column label="乙方名称" align="center" prop="supplyName" />
        <el-table-column label="截至时间" align="center" prop="byTime">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.byTime, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="告警" align="center" prop="ifNotice">
          <template slot-scope="scope">
            <span v-show="scope.row.ifNotice > 0" style="color: red"
              >已超期</span
            >
            <span v-show="scope.row.ifNotice == 0" style="color: #f8ac59"
              >将要超期</span
            >
            <span v-show="scope.row.ifNotice < 0" style="color: green"
              >未超期</span
            >
          </template>
        </el-table-column>
        <el-table-column
          label="单据状态"
          align="center"
          prop="status"
          width="180"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.oa_flow_billstatus"
              :value="scope.row.status"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="300"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              v-show="scope.row.status == '0' || scope.row.status == '3'"
              icon="el-icon-edit"
              @click="bindTpl(scope.row)"
              v-hasPermi="['ct:agreementTpl:edit']"
              >绑定模版</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="preview(scope.row)"
              v-hasPermi="['ct:agreementTpl:edit']"
              >预览</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="doBuildFile(scope.row)"
              v-hasPermi="['ct:agreementTpl:edit']"
              >生成word</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="dlAgreement(scope.row)"
              v-hasPermi="['ct:agreementTpl:edit']"
              >下载文件</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-show="scope.row.status == '0' || scope.row.status == '3'"
              icon="el-icon-edit"
              @click="doSubmitFlow(scope.row)"
              v-hasPermi="['ct:agreementTpl:edit']"
              >提交审批</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-show="scope.row.status == '0' || scope.row.status == '3'"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['ct:agreement:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-show="scope.row.status == '0' || scope.row.status == '3'"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['ct:agreement:remove']"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-show="scope.row.status == '1'"
              icon="el-icon-edit"
              @click="doCancleFlow(scope.row)"
              v-hasPermi="['ct:agreementTpl:edit']"
              style="color: #ff6666"
              >撤销申请</el-button
            >
            <el-button
              size="mini"
              type="text"
              v-show="
                scope.row.status == '1' ||
                scope.row.status == '2' ||
                scope.row.status == '3'
              "
              icon="el-icon-edit"
              @click="flowDetail(scope.row)"
              v-hasPermi="['ct:agreementTpl:edit']"
              >流程信息</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改补充协议对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="协议编号" prop="contractNo">
          <el-input v-model="form.contractNo" placeholder="请输入协议编号" />
        </el-form-item>
        <el-form-item label="协议名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入协议名称" />
        </el-form-item>
        <el-form-item label="签约地点" prop="signAddr">
          <el-input v-model="form.signAddr" placeholder="请输入签约地点" />
        </el-form-item>
        <el-form-item label="最低保证金" prop="lowBond">
          <el-input v-model="form.lowBond" placeholder="请输入最低保证金" />
        </el-form-item>
        <el-form-item label="应付款" prop="needCharge">
          <el-input v-model="form.needCharge" placeholder="请输入应付款" />
        </el-form-item>
        <el-form-item label="预付款" prop="advanceCharge">
          <el-input v-model="form.advanceCharge" placeholder="请输入预付款" />
        </el-form-item>
        <el-form-item label="协议价格" prop="agreementAmt">
          <el-input v-model="form.agreementAmt" placeholder="请输入协议价格" />
        </el-form-item>
        <el-form-item label="材质" prop="material">
          <el-input v-model="form.material" placeholder="请输入材质" />
        </el-form-item>
        <el-form-item label="参考价" prop="referencePrice">
          <el-input v-model="form.referencePrice" placeholder="请输入参考价" />
        </el-form-item>
        <el-form-item label="定价基准" prop="pricingBenchmark">
          <el-input
            v-model="form.pricingBenchmark"
            placeholder="请输入定价基准"
          />
        </el-form-item>
        <el-form-item label="甲方编码" prop="orgNo">
          <el-input v-model="form.orgNo" placeholder="请输入甲方编码" />
        </el-form-item>
        <el-form-item label="甲方名称" prop="orgName">
          <el-input v-model="form.orgName" placeholder="请输入甲方名称" />
        </el-form-item>
        <el-form-item label="甲方法人" prop="orgFr">
          <el-input v-model="form.orgFr" placeholder="请输入甲方法人" />
        </el-form-item>
        <el-form-item label="甲方地址" prop="orgAddr">
          <el-input v-model="form.orgAddr" placeholder="请输入甲方地址" />
        </el-form-item>
        <el-form-item label="甲方委托代理人" prop="orgWtr">
          <el-input v-model="form.orgWtr" placeholder="请输入甲方委托代理人" />
        </el-form-item>
        <el-form-item label="甲方电话" prop="orgTel">
          <el-input v-model="form.orgTel" placeholder="请输入甲方电话" />
        </el-form-item>
        <!--                <el-form-item label="甲方签证" prop="okDt">-->
        <!--                    <el-input v-model="form.okDt" placeholder="请输入甲方签证"/>-->
        <!--                </el-form-item>-->
        <el-form-item label="乙方编码" prop="supplyNo">
          <el-input v-model="form.supplyNo" placeholder="请输入乙方编码" />
        </el-form-item>
        <el-form-item label="乙方名称" prop="supplyName">
          <el-input v-model="form.supplyName" placeholder="请输入乙方名称" />
        </el-form-item>
        <el-form-item label="乙方法人" prop="spFr">
          <el-input v-model="form.spFr" placeholder="请输入乙方法人" />
        </el-form-item>
        <el-form-item label="乙方地址" prop="spAddr">
          <el-input v-model="form.spAddr" placeholder="请输入乙方地址" />
        </el-form-item>
        <el-form-item label="乙方委托人" prop="spWtr">
          <el-input v-model="form.spWtr" placeholder="请输入乙方委托人" />
        </el-form-item>
        <el-form-item label="乙方电话" prop="spTel">
          <el-input v-model="form.spTel" placeholder="请输入乙方电话" />
        </el-form-item>
        <!--                <el-form-item label="乙方签证" prop="spOkDt">-->
        <!--                    <el-input v-model="form.spOkDt" placeholder="请输入乙方签证"/>-->
        <!--                </el-form-item>-->
        <el-form-item label="补充" prop="other">
          <el-input
            v-model="form.other"
            type="textarea"
            :rows="5"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="截止时间" prop="other">
          <el-date-picker
            clearable
            v-model="form.byTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择截止时间"
          ></el-date-picker>
        </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="绑定模版"
      :visible.sync="showBindTpl"
      width="1000px"
      append-to-body
    >
      <SelectAgreementTpl
        @selectedAgreementTpl="selectedAgreementTpl"
      ></SelectAgreementTpl>
      <span slot="footer" class="dialog-footer">
        <el-button @click="showBindTpl = false">取 消</el-button>
        <el-button type="primary" @click="doBindTplUpdate">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      title="预览协议"
      :visible.sync="showAgreemwnt"
      width="1200px"
      append-to-body
    >
      <div v-html="previewContent"></div>
    </el-dialog>

    <!-- <el-dialog title="流程信息" :visible.sync="showFlowInfo" width="1200px" append-to-body>
            <FlowInfo :businessId="businessId" billType="agreement" v-if="showFlowInfo"></FlowInfo>
        </el-dialog> -->
  </div>
</template>

<script>
import {
  addAgreement,
  buildFile,
  delAgreement,
  getAgreement,
  listAgreement,
  previewAgreemwnt,
  updateAgreement,
} from "@/api/ct/agreement";
// import {submitFlow,cancleFlow} from "@/api/oa/processInstance";
import SelectAgreementTpl from "@/views/ct/agreementTpl/selectAgreementTpl";
// import FlowInfo from "@/views/oa/flow/flowInfo";

export default {
  name: "Agreement",
  dicts: ["oa_customform_category", "oa_flow_billstatus"],
  components: {
    SelectAgreementTpl,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 补充协议表格数据
      agreementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: "createTime",
        isAsc: "descending",
        contractNo: null,
        contractName: null,
        signAddr: null,
        lowBond: null,
        gzType: null,
        payType: null,
        needCharge: null,
        advanceCharge: null,
        agreementAmt: null,
        material: null,
        referencePrice: null,
        pricingBenchmark: null,
        other: null,
        orgNo: null,
        orgName: null,
        orgFr: null,
        orgAddr: null,
        orgWtr: null,
        orgTel: null,
        okDt: null,
        supplyNo: null,
        supplyName: null,
        spFr: null,
        spAddr: null,
        spWtr: null,
        spTel: null,
        spOkDt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      showBindTpl: false,
      tplTotal: 0,
      showAgreemwnt: false,
      previewContent: "",
      showFlowInfo: false,
      insId: "",
      businessId: "",
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    /** 查询补充协议列表 */
    getList() {
      this.loading = true;
      listAgreement(this.queryParams).then((response) => {
        this.agreementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractNo: null,
        contractName: null,
        signAddr: null,
        lowBond: null,
        gzType: null,
        payType: null,
        needCharge: null,
        advanceCharge: null,
        agreementAmt: null,
        material: null,
        referencePrice: null,
        pricingBenchmark: null,
        other: null,
        orgNo: null,
        orgName: null,
        orgFr: null,
        orgAddr: null,
        orgWtr: null,
        orgTel: null,
        okDt: null,
        supplyNo: null,
        supplyName: null,
        spFr: null,
        spAddr: null,
        spWtr: null,
        spTel: null,
        spOkDt: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        bindAgreementId: null,
        selectedTplId: null,
        selectedTplName: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加补充协议";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAgreement(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改补充协议";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAgreement(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAgreement(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除补充协议编号为"' + ids + '"的数据项？')
        .then(function () {
          return delAgreement(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "ct/agreement/export",
        {
          ...this.queryParams,
        },
        `agreement_${new Date().getTime()}.xlsx`
      );
    },
    /** 绑定模版 ***/
    bindTpl(row) {
      this.bindAgreementId = row.id;
      this.showBindTpl = true;
    },
    /** 选择确定之后 **/
    selectedAgreementTpl(sel) {
      this.selectedTplId = sel.id;
      this.selectedTplName = sel.name;
    },
    doBindTplUpdate() {
      let form = {};
      form.id = this.bindAgreementId;
      form.tplId = this.selectedTplId;
      form.tplName = this.selectedTplName;
      updateAgreement(form).then((response) => {
        this.$modal.msgSuccess("绑定成功");
        this.open = false;
        this.getList();
      });
      this.showBindTpl = false;
    },
    /** 预览协议 ***/
    preview(row) {
      if (!row.tplId) {
        this.$modal.msgError("未绑定协议模板，请先绑定协议模板！！");
        return;
      }
      previewAgreemwnt(row.id).then((res) => {
        if (200 == res.code) {
          this.previewContent = res.data;
          this.showAgreemwnt = true;
        }
      });
    },
    /** 生成文件 **/
    doBuildFile(row) {
      buildFile(row.id).then((res) => {
        if (200 == res.code) {
          this.$modal.msgSuccess("生成成功");
          this.getList();
        } else {
          this.$modal.msgError("生成失败");
        }
      });
    },
    /** 下载协议 ***/
    dlAgreement(row) {
      this.$download.resource(row.url);
    },
    /** 提交审批 ***/
    doSubmitFlow(row) {
      if (!row.tplId) {
        this.$modal.msgError("未绑定模版，无法提交。请先绑定模版");
        return;
      }
      let data = {
        businessId: row.id,
        billType: row.billType,
      };
      this.$modal
        .confirm("确认要提交该协议进行审批吗？")
        .then(function () {
          return submitFlow(data);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("提交成功");
        })
        .catch(() => {});
    },
    /** 取回审批 ***/
    doCancleFlow(row) {
      let data = {
        businessId: row.id,
        billType: row.billType,
      };
      this.$modal.confirm("确认要取回该协议审批吗？").then(() => {
        cancleFlow(data).then((res) => {
          if (200 == res.code) {
            this.getList();
            this.$modal.msgSuccess("取回成功");
          }
        });
      });
    },
    /** 流程信息 ***/
    flowDetail(row) {
      this.businessId = row.id;
      // this.billType = row.billType;
      this.showFlowInfo = true;
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/assets/styles/header/index.scss";
@import "@/assets/styles/common/index.scss";
</style>
