<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
        v-for="item in clumnData"
        align="center"
        :label="item.label"
        :key="item.prop"
        :prop="item.prop"
        :min-width="item.width"
      />
      <el-table-column
        align="center"
        label="类型"
        prop="type"
        #default="{ row }"
      >
        <span>
          {{ row.type == "mcs" ? "耗材" : "药品" }}
        </span>
      </el-table-column>
      <el-table-column
        align="center"
        prop="hospName"
        label="医院名称"
        v-if="orgType == 'YBJ'"
      />

      <el-table-column align="center" label="操作" prop="stas" fixed="right">
        <template slot-scope="scope">
          <el-button
            :style="{
              display: pushBtnNone(scope.row),
            }"
            size="mini"
            @click.stop="Push(scope.row)"
            type="primary"
            >推送</el-button
          >
          <el-button
            :style="{
              display: sucessBtnNone(scope.row),
            }"
            size="mini"
            type="success"
            @click.stop="PassThough(scope.row)"
            >{{ succesText() }}</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click.stop="DownLoad(scope.row)"
            :style="{
              display: downdBtnNone(scope.row),
            }"
            >下载</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 {
  updateStatusPushdata,
  updateStatusDownLoad,
  updateStatusPass,
  updateStatusYbjPass,
} from "@/api/suppleDudit/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" ? "数据上传" : "通过";
    },
    pushBtnNone(row) {
      let style = null;
      let { orgType } = this.$store.state.user;
      let stopTime = moment(new Date())
        .subtract(2, "months")
        .startOf("month")
        .format("YYYYMM");
      let days = Number(moment().format("DD"));
      if (Number(row.initCode) <= Number(stopTime)) {
        style = "none";
      } else {
        if (orgType == "YBJ") {
          style = "none";
        }
        if (supData[orgType]) {
          style =
            statusData[row.status] ||
            days > Number(row.firstTime) ||
            days < Number(row.startTime)
              ? "none"
              : "inline-block";
        }
        if (hosData[orgType]) {
          style =
            row.status == "2" ||
            row.status == "Y" ||
            row.status == "Z" ||
            days < Number(row.secondTime) ||
            days > Number(row.thirdTime)
              ? "none"
              : "inline-block";
        }
      }
      return style;
    },
    downdBtnNone() {
      let style = null;
      let { orgType } = this.$store.state.user;
      let days = moment().format("DD");
      if (orgType == "YBJ") {
        style = "none";
      }
      return style;
    },
    sucessBtnNone(row) {
      let style = null;
      let { orgType } = this.$store.state.user;
      let days = moment().format("DD");
      let stopTime = moment(new Date())
        .subtract(2, "months")
        .startOf("month")
        .format("YYYYMM");
      if (Number(row.initCode) <= Number(stopTime)) {
        style = "none";
      } else {
        if (orgType == "YBJ" && days < Number(row.thirdTime)) {
          style = "none";
        }
        if (supData[orgType]) {
          style =
            row.status == "Y" ||
            row.status == "A" ||
            row.status == "Z" ||
            days > Number(row.firstTime) ||
            days < Number(row.startTime)
              ? "none"
              : "inline-block";
        }
        if (hosData[orgType]) {
          style =
            row.status == "Y" ||
            row.status == "Z" ||
            days < Number(row.secondTime) ||
            days > Number(row.thirdTime)
              ? "none"
              : "inline-block";
        }
      }
      return style;
    },
    //推送
    Push(row) {
      updateStatusPushdata({
        ...this.pamData,
        billAmt: row.billAmt,
        id: row.id,
        initCode: row.initCode,
      }).then((res) => {
        if (res.success == true) {
          this.$message.success("推送成功");
          let tablePam = { size: this.pageSize, current: this.crutPage };
          this.$emit("pageTruning", tablePam);
        }
      });
    },
    //通过
    PassThough(row) {
      let { orgType, userID } = this.$store.state.user;
      if (orgType == "YBJ") {
        this.ybjUploadData(row);
        return;
      }
      if (!row.status || row.status == "A") {
        this.$message.error("请先进行推送");
        return;
      }
      let appid = null;
      let CAhost = null;
      let CAport = null;
      if (orgType == "SUP" || orgType == "HOS") {
        appid = "37be22d07f5e44f68b9d2ef5ac281242";
        CAhost = "10.10.2.120";
        CAport = 9527;
      }

      if (orgType == "MSUP" || orgType == "MHOS") {
        appid = "5448fd499f0b47498615c11cb5027e51";
        CAhost = "10.10.2.120";
        CAport = 9528;
      }

      var data = {
        appId: appid,
        srmCode: userID,
        docuNum: userID + row.initCode + row.id,
        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);
    },
    ybjUploadData(row) {
      updateStatusYbjPass({
        hospId: row.hospId,
        hospName: row.hospName,
        type: row.type,
        billAmt: row.billAmt,
        initCode: row.initCode,
      }).then((res) => {
        if (res.success == true) {
          this.$message.success("上传成功");
          let tablePam = { size: this.pageSize, current: this.crutPage };
          this.$emit("pageTruning", tablePam);
        }
      });
    },
    // 下载
    DownLoad(row) {
      let { userID, orgType } = this.$store.state.user;
      updateStatusDownLoad({
        id: row.id,
        userId: userID,
        initCode: row.initCode,
        orgType,
      }).then((res) => {
        if (res.success == true) {
          exportSaveData(res.data, "合同.pdf");
        }
      });
    },
    //表格点击触发
    rowClick(row, column, event) {
      this.$router.push({
        name: "summary",
        query: {
          initCode: row.initCode || "",
          hospId: row.hospId || "",
          type: row.type == "mcs" ? "耗材" : "药品",
        },
      });
    },
    // 搜索区域输入的数据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>
