<template>
  <div>
    <el-button type="primary" size="mini" class="btn" @click="handleMakeNew"
      >制作合同</el-button
    >
    <div style="margin: 10px 0; display: flex; align-items: center">
      <el-input
        class="input_content"
        placeholder="请输入内容"
        v-model="sourceList.pagination.keyword"
        style="width: 50%"
        clearable
        size="small"
      ></el-input>
      <el-select
        v-model="sourceList.pagination.status"
        clearable
        filterable
        style="width: 40%; margin-left: 10px"
        size="small"
      >
        <el-option
          v-for="(item, index) in statusData"
          :key="index"
          :value="item.value"
          :label="item.label"
        ></el-option>
      </el-select>
      <div style="text-align: center; width: 10%">
        <el-icon style="cursor: pointer" @click="getData"><Search /></el-icon>
      </div>
    </div>
    <el-table
      :data="sourceList.data"
      style="width: 100%; margin-top: 10px"
      v-loading="loading"
      :height="tableHeight"
    >
      <el-table-column prop="code" label="单号" width="130" fixed="left">
        <template #default="{ row }">
          <div
            style="cursor: pointer; color: #409eff"
            @click="handleClickContractCode(row)"
          >
            {{ row.code }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="日期" width="140" />
      <el-table-column prop="buyContactName" label="客户" min-width="150" />
      <el-table-column prop="amount" label="报价" width="100">
        <template #default="{ row }">
          <div>{{ row.currency }} {{ moneyFormat(row.amount, 2) }}</div>
        </template>
      </el-table-column>
      <el-table-column
        prop="status"
        label="状态"
        width="80"
        :formatter="getStatus"
      />
      <!-- <el-table-column label="操作" width="60" fixed="right" align="center">
        <template #default="{ row }">
          <el-button type="primary" text
            ><i class="iconfont icon-iconm_wofqd"></i
          ></el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      style="margin-top: 10px"
      v-model:current-page="sourceList.pagination.pageNum"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="sourceList.pagination.total"
      prev-text="上一页"
      next-text="下一页"
      @current-change="handleCurrentChange"
    />
  </div>
</template>
  
<script setup>
const tableHeight = ref(0);
const getTableHeight = () => {
  tableHeight.value = window.innerHeight - 280;
};
getTableHeight();
window.addEventListener("resize", () => {
  getTableHeight();
});
const loading = ref(false);
const sourceList = ref({
  data: [],
  pagination: {
    total: 300,
    pageNum: 1,
    pageSize: 10,
  },
});
const statusData = ref([
  {
    label: "草稿",
    value: 0,
  },
  {
    label: "审批中",
    value: 10,
  },
  {
    label: "驳回",
    value: 20,
  },
  {
    label: "通过",
    value: 30,
  },
  {
    label: "终止",
    value: 99,
  },
]);
const { proxy } = getCurrentInstance();
const getStatus = (row) => {
  const current = statusData.value.find((x) => x.value == row.status);
  if (current) return current.label;
};

const getData = () => {
  loading.value = true;
  proxy.post("/contract/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};

const handleCurrentChange = (val) => {
  sourceList.value.pagination.pageNum = val;
  getData();
};

getData();
onMounted(() => {});

const handleMakeNew = () => {
  proxy.$router.replace({
    path: "/platform_manage/process/processApproval",
    query: {
      flowKey: "contract_flow",
      flowName: "销售合同审批流程",
      random: proxy.random(),
    },
  });
};

const handleClickContractCode = (row) => {
  proxy.$router.push({
    path: "/ERP/saleContract/contract",
    query: {
      code: row.code,
    },
  });
};
</script>
  
<style lang="scss" scoped>
* {
  font-size: 12px;
}
.el-button {
  padding: 0px;
}
.btn {
  width: 100%;
  border-radius: 10px;
  padding: 6px 10px;
  height: 24px;
}
.el-pagination {
  padding-left: 30px;
}
:deep(.el-pagination button, .el-pager li) {
  font-size: 12px;
}
:deep(.el-table .el-table__cell) {
  padding: 2px 0px;
}
</style>