<template>
  <div class="table-box">
    <el-table
      ref="multipleTable"
      v-loading="loding"
      :data="tableData"
      element-loading-text="加载中..."
      border
      fit
      highlight-current-row
      max-height="400"
      @row-dblclick="rowClick"
    >
      <template slot="empty">
        <img src="" alt />
        <p>暂无数据</p>
      </template>
      <el-table-column fixed="left" align="center" type="index" label="序号" />
      <el-table-column align="center" label="医疗机构" prop="hospName">
      </el-table-column>
      <el-table-column align="center" label="配送企业" prop="splerName">
      </el-table-column>
      <el-table-column align="center" label="操作" prop="stas" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="orgType == 'HOS' && scope.row.status == '0'"
            size="mini"
            @click.stop="Push(scope.row)"
            type="primary"
            >生成协议</el-button
          >
          <!-- 医疗机构签订协议 -->
          <el-button
            v-if="orgType == 'HOS' && scope.row.status == '1'"
            size="mini"
            @click.stop="hosPush(scope.row)"
            type="success"
            >签订协议</el-button
          >
          <!-- 配送企业签订协议 -->
          <el-button
            v-if="orgType == 'SUP' && scope.row.status == '2'"
            size="mini"
            @click.stop="hosPush(scope.row)"
            type="success"
            >签订协议</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click.stop="DownLoad(scope.row)"
            v-if="orgType == 'SUP' || orgType == 'HOS'"
            >下载</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click.stop="DownLoad(scope.row)"
            v-if="orgType == 'YBJ' && scope.row.status == '3'"
            >下载</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="crutPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import { clumnData, tableData } from "./clumnData";
import { updateStatusPushJSXY, downLoadZjjsFile } from "@/api/signList/index";
import { exportSaveData, statusData } from "./const";
import { supData, hosData } from "../const";
import { hos } from "../../summary/const";
import moment from "moment";
export default {
  name: "tableView",
  components: {},
  data() {
    return {
      loding: false, // 表格数据加载loding
      tableData: [], // 表格数据
      clumnData: clumnData, // 表格头数据
      total: 0,
      pageSize: 10,
      crutPage: 1,
      pamData: {},
      statusData: statusData,
      orgType: "",
    };
  },
  props: {
    getTableData: {
      type: Object,
      default: {},
    },
  },
  created() {
    let { orgType } = this.$store.state.user;
    this.orgType = orgType;
  },
  mounted() {
    let { orgType, userID } = this.$store.state.user;
    let parmObj = { orgType: orgType, userId: userID };
    if (supData[orgType]) parmObj["supplierId"] = userID;
    if (hosData[orgType]) parmObj["hospId"] = userID;
    this.pamData = parmObj;
  },
  methods: {
    succesText() {
      let { orgType } = this.$store.state.user;
      return orgType == "YBJ" ? "数据上传" : "通过";
    },
    //医疗机构生成协议
    Push(row) {
      updateStatusPushJSXY({
        hospId: row.hospId,
        splerId: row.splerId,
      }).then((res) => {
        if (res.success == true) {
          this.$message.success("协议生成成功");
          let tablePam = { size: this.pageSize, current: this.crutPage };
          this.$emit("pageTruning", tablePam);
        }
      });
    },
    //通过
    hosPush(row) {
      let { orgType, userID } = this.$store.state.user;
      let appid = null;
      let CAhost = null;
      let CAport = null;
      if (orgType == "SUP" || orgType == "HOS") {
        appid = "37be22d07f5e44f68b9d2ef5ac281242";
        CAhost = "10.10.2.120";
        CAport = 9527;
      }
      var data = {
        appId: appid,
        srmCode: userID,
        docuNum: row.fileId,
        timestamp: new Date().valueOf(),
      };
      data = JSON.stringify(data);
      var restful = data;
      var url =
        "http://" + CAhost + ":" + CAport + "/ec-web/ecLogin/srmsign?v=";
      url = url + window.btoa(restful);
      let head = document.querySelector("head");
      let methd = document.createElement("meta");
      methd.setAttribute("name", "referrer");
      methd.setAttribute("content", "never");
      head.appendChild(methd);
      window.open(url);
    },
    // 下载
    DownLoad(row) {
      let { userID, orgType } = this.$store.state.user;
      downLoadZjjsFile({
        fileId: row.fileId,
      }).then((res) => {
        if (res.success == true) {
          exportSaveData(res.data, "合同.pdf");
        }
      });
    },
    // 搜索区域输入的数据s
    getSearchData(data) {
      console.log(data, "搜索查询到的数据");
    },
    //每页几条数据
    handleSizeChange(val) {
      this.pageSize = val;
      this.crutPage = 1;
      let tablePam = { size: val, current: 1 };
      this.$emit("pageTruning", tablePam);
    },
    // 当前在第几页
    handleCurrentChange(val) {
      this.crutPage = val;
      let tablePam = { size: this.pageSize, current: val };
      this.$emit("pageTruning", tablePam);
    },
  },
  watch: {
    //表格数据发生改变时重新渲染
    getTableData(newData, oldData) {
      let data = newData.records ? newData.records : [];
      this.tableData = data;
      this.total = newData.total ? newData.total : 0;
      this.pageSize = newData.size ? newData.size : 10;
    },
  },
};
</script>
<style scoped lang="scss">
.table-box {
  padding: 1px;
  position: relative;
  padding-bottom: 50px;
  .block {
    position: absolute;
    right: 10px;
    bottom: 10px;
  }
}
</style>
