<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        class="filter-item"
        clearable
        filterable
        v-model="listQuery.projectId"
        placeholder="请选择项目"
      >
        <el-option
          v-for="item in projectOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        ></el-option>
      </el-select>

      <el-input
        v-model="listQuery.name"
        placeholder="请输入用户名称"
        class="filter-item"
        clearable
      ></el-input>

      <el-date-picker
        class="filter-item"
        value-format="timestamp"
        v-model="month"
        @change="handleRepaymentDateChange"
        type="month"
        placeholder="选择月"
      >
      </el-date-picker>

      <el-button
        class="filter-item"
        type="primary"
        @click="handleSearch"
        icon="el-icon-search"
        >搜索</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        @click="handleExport"
        icon="el-icon-download"
        >导出Excel</el-button
      >

      <!-- <el-button class="filter-item" type="primary" @click="handleMutiplePaid"
        >批量还款</el-button
      > -->
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <!-- <el-table-column type="selection" width="55" :selectable="selectable">
      </el-table-column> -->

      <el-table-column label="项目名称" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.projectName }}</span>
        </template>
      </el-table-column>

      <el-table-column label="房票编号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.numbering }}</span>
        </template>
      </el-table-column>

      <el-table-column label="用户姓名" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.name }}</span>
        </template>
      </el-table-column>

      <el-table-column label="购房人手机号" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.phone }}</span>
        </template>
      </el-table-column>

      <el-table-column label="房票金额" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.number }}</span>
        </template>
      </el-table-column>

      <el-table-column label="分期数" align="center">
        <template slot-scope="scope">
          <span
            >{{ scope.row.numberOfInstallmentsNow }}/{{
              scope.row.numberOfInstallments
            }}</span
          >
        </template>
      </el-table-column>

      <el-table-column label="每期还款" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.repaymentAmount }}</span>
        </template>
      </el-table-column>

      <el-table-column label="本期应还款日期" align="center">
        <template slot-scope="scope">
          <span>{{
            scope.row.repaymentDate | parseTime("{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="还款状态" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.repaymentStatus ? "已还" : "未还" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        width="300"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.repaymentStatus"
            type="primary"
            size="mini"
            @click="handlePaid(scope.row.id)"
            >处理</el-button
          >
          <el-button
            type="primary"
            size="mini"
            @click="handleDetail(scope.row.housingTicketId)"
            >工单详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getPhase6Data, dealPhase6 } from "@/api/housingTicket";
import { list as projectList } from "@/api/project";
import Pagination from "@/components/Pagination/index";
import { parseTime } from "@/utils";
export default {
  components: { Pagination },

  data() {
    return {
      total: 0,
      list: [],
      listQuery: {
        current: 1,
        size: 10,
        repaymentStatus: true,
      },
      listLoading: false,
      multipleSelection: [],
      month: undefined,
      projectOptions: [],
    };
  },
  created() {
    // const currentDate = Math.floor(Date.now());
    // this.month = currentDate;
    // this.listQuery.repaymentDate = Math.floor(currentDate / 1000);
    this.getList();
    this.getProjectList();
  },
  methods: {
    selectable(row, index) {
      if (row.repaymentStatus) {
        return false;
      } else {
        return true;
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    openMethods() {
      this.$refs.form.clearValidate();
    },
    getList() {
      getPhase6Data(this.listQuery).then((res) => {
        this.list = res.d.records;
        this.total = res.d.total;
      });
    },
    getProjectList() {
      projectList({ current: 1, size: 99999 }).then((res) => {
        this.projectOptions = res.d.records;
      });
    },
    handleSearch() {
      this.listQuery.current = 1;
      this.getList();
    },
    handleMutiplePaid() {
      console.log("handleMutiplePaid");
      let payload = {
        ids: this.multipleSelection.map((item) => item.id),
      };
      console.log(payload);
      dealPhase6(payload).then((res) => {
        if (res.c === 200) {
          this.$message.success("批量还款成功");
          this.getList();
        }
      });
    },
    handlePaid(id) {
      let payload = {
        ids: [],
      };
      payload.ids.push(id);
      dealPhase6(payload).then((res) => {
        if (res.c === 200) {
          this.$message.success("还款成功");
          this.getList();
        }
      });
    },
    handleRepaymentDateChange(val) {
      if (!val) {
        this.listQuery.repaymentDate = undefined;
      } else {
        this.listQuery.repaymentDate = val / 1000;
      }
    },
    handleDetail(id) {
      this.$router.push({ path: "/ticket/ticketProgress", query: { id } });
    },
    handleExport() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then((excel) => {
        const tHeader = [
          "项目名称",
          "房票编号",
          "用户姓名",
          "购房人手机号",
          "房票金额",
          // "已还期数",
          "分期数",
          "每期还款",
          "本期应还款日期",
          "还款状态",
        ];
        const filterVal = [
          "projectName",
          "numbering",
          "name",
          "phone",
          "number",
          // "numberOfInstallmentsNow",
          "numberOfInstallments",
          "repaymentAmount",
          "repaymentDate",
          "repaymentStatus",
        ];

        const payload = {
          ...this.listQuery,
          size: 10000,
          current: 1,
        };

        getPhase6Data(payload).then((response) => {
          excel.export_json_to_excel({
            header: tHeader,
            data: this.formatJson(filterVal, response.d.records),
            filename: "还款列表",
          });
        });

        this.downloadLoading = false;
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) =>
        filterVal.map((j) => {
          if (j === "repaymentDate") {
            return parseTime(v[j], "{y}-{m}-{d}");
          } else if (j === "repaymentStatus") {
            return v[j] ? "已还" : "未还";
          } else if (j === "numberOfInstallments") {
            return v["numberOfInstallmentsNow"] + "/" + v[j];
          } else {
            return v[j];
          }
        })
      );
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 200px;
  margin-right: 10px;
}
.el-select {
  margin-right: 10px;
}
</style>
