<template>
  <div class="tenant">
    <byTable
      :source="sourceList.data"
      :pagination="sourceList.pagination"
      :config="config"
      :loading="loading"
      :selectConfig="selectConfig"
      highlight-current-row
      @get-list="getList"
    >
      <template #code="{ item }">
        <div
          style="cursor: pointer; color: #409eff"
          @click="handleClickCode(item)"
        >
          {{ item.code }}
        </div>
      </template>
      <template #amount="{ item }">
        <div>
          <span style="padding-right: 4px">{{ item.currency }}</span>
          <span>{{ moneyFormat(item.amount, 2) }}</span>
        </div>
      </template>
    </byTable>
    <el-dialog title="打印" v-if="openPdf" v-model="openPdf" width="900px">
      <PurchasePDFOne :rowData="rowData"></PurchasePDFOne>
      <template #footer ref="printBtn">
        <el-button @click="openPdf = false" size="large">关闭</el-button>
        <el-button type="primary" v-print="printObj" size="large"
          >打印</el-button
        >
        <el-button type="primary" @click="clickDownload()" size="large"
          >下载PDF</el-button
        >
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import byTable from "@/components/byTable/index";
import { ElMessage, ElMessageBox } from "element-plus";
import PurchasePDFOne from "@/components/PDF/purchasePDFOne.vue";

const { proxy } = getCurrentInstance();
const supplierList = ref([]);
const status = ref([
  {
    label: "草稿",
    value: 0,
  },
  {
    label: "审批中",
    value: 10,
  },
  {
    label: "驳回",
    value: 20,
  },
  {
    label: "已采购",
    value: 30,
  },
  {
    label: "作废",
    value: 88,
  },
  {
    label: "终止",
    value: 99,
  },
]);
const payStatus = ref([
  {
    label: "未付款",
    value: 0,
  },
  {
    label: "部分付款",
    value: 10,
  },
  {
    label: "已付款",
    value: 20,
  },
]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    status: "",
    payStatus: "",
  },
});
const loading = ref(false);
const selectConfig = computed(() => {
  return [
    {
      label: "采购状态",
      prop: "status",
      data: status.value,
    },
    {
      label: "付款状态",
      prop: "payStatus",
      data: payStatus.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "采购单号",
        slot: "code",
        width: 180,
      },
    },
    {
      attrs: {
        label: "供应商",
        prop: "sellCorporationId",
        "min-width": 220,
      },
      render(type) {
        return proxy.dictValueLabel(type, supplierList.value);
      },
    },
    {
      attrs: {
        label: "采购金额",
        slot: "amount",
        width: 140,
      },
    },
    {
      attrs: {
        label: "版本号",
        prop: "version",
        width: 100,
      },
    },
    {
      attrs: {
        label: "采购人",
        prop: "userName",
        width: 140,
      },
    },
    {
      attrs: {
        label: "采购时间",
        prop: "createTime",
        width: 160,
      },
    },
    {
      attrs: {
        label: "采购状态",
        prop: "status",
        width: 140,
      },
      render(type) {
        return proxy.dictValueLabel(type, status.value);
      },
    },
    {
      attrs: {
        label: "付款状态",
        prop: "payStatus",
        width: 140,
      },
      render(type) {
        return proxy.dictValueLabel(type, payStatus.value);
      },
    },
    {
      attrs: {
        label: "操作",
        width: 160,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "打印",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              handlePrintPdf(row);
            },
          },
          row.status == 30
            ? {
                attrs: {
                  label: "作废",
                  type: "primary",
                  text: true,
                },
                el: "button",
                click() {
                  ElMessageBox.confirm(
                    "此操作将作废该数据, 是否继续?",
                    "提示",
                    {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                    }
                  ).then(() => {
                    proxy
                      .post("/ehsdPurchase/edit", {
                        id: row.id,
                        status: 88,
                      })
                      .then(() => {
                        ElMessage({
                          message: "作废成功",
                          type: "success",
                        });
                        getList();
                      });
                  });
                },
              }
            : {},
          row.status == 30
            ? {
                attrs: {
                  label: "终止",
                  type: "primary",
                  text: true,
                },
                el: "button",
                click() {
                  ElMessageBox.confirm(
                    "此操作将终止该数据, 是否继续?",
                    "提示",
                    {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                    }
                  ).then(() => {
                    proxy
                      .post("/ehsdPurchase/edit", {
                        id: row.id,
                        status: 99,
                      })
                      .then(() => {
                        ElMessage({
                          message: "终止",
                          type: "success",
                        });
                        getList();
                      });
                  });
                },
              }
            : {},
        ];
      },
    },
  ];
});
const getDict = () => {
  proxy
    .post("/supplierInfo/page", { pageNum: 1, pageSize: 999 })
    .then((res) => {
      supplierList.value = res.rows.map((item) => {
        return {
          ...item,
          label: item.name,
          value: item.id,
        };
      });
    });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/ehsdPurchase/page", sourceList.value.pagination).then((res) => {
    console.log(res);
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getDict();
getList();

const openPdf = ref(false);
const pdfData = ref({});
const rowData = ref({});
const handlePrintPdf = (row) => {
  rowData.value = {
    id: row.id,
    code: row.code,
  };
  rowData.value = row;
  openPdf.value = true;
};
const clickDownload = () => {
  proxy.getPdf("购销合同" + rowData.value.code);
};
const pdfDom = ref(null);
const printObj = ref({
  id: "pdfDom",
  popTitle: "",
  extraCss:
    "https://cdn.bootcdn.net/ajax/libs/animate.css/4.1.1/animate.compat.css, https://cdn.bootcdn.net/ajax/libs/hover.css/2.3.1/css/hover-min.css",
  extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>',
});

const handleClickCode = (row) => {
  proxy.$router.push({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: row.processInstanceId,
      id: row.flowId,
      processType: 20,
    },
  });
};
</script>

<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
.baseRow {
  min-height: 24px;
  border-top: 1px solid black;
  border-left: 1px solid black;
}
.contentRow {
  border-right: 1px solid black;
  line-height: 24px;
  padding-left: 4px;
}
</style>
