<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-model="listQuery.projectId"
        class="filter-item"
        clearable
        placeholder="请选择项目"
        filterable
        v-if="isAll"
      >
        <el-option
          v-for="item in projectOptions"
          :label="item.name"
          :value="item.id"
          :key="item.id"
        ></el-option>
      </el-select>
      <el-select
        v-model="listQuery.streetOfficeNameA"
        class="filter-item"
        clearable
        placeholder="请选择街道"
        filterable
      >
        <el-option label="高桥" value="高桥"></el-option>
        <el-option label="胜利" value="胜利"></el-option>
      </el-select>
      <el-input
        v-model="listQuery.numbering"
        placeholder="请输入房票编号"
        class="filter-item"
        clearable
      ></el-input>
      <el-input
        v-model="listQuery.nameB"
        placeholder="请输入用户姓名"
        class="filter-item"
        clearable
      ></el-input>
      <el-input
        v-model="listQuery.idCardB"
        placeholder="请输入身份证号"
        class="filter-item"
        clearable
      ></el-input>
      <el-input
        v-model="listQuery.phoneNumberB"
        placeholder="请输入手机号"
        class="filter-item"
        clearable
      ></el-input>

      <el-select
        v-model="listQuery.phase"
        class="filter-item"
        clearable
        placeholder="请选择阶段"
        filterable
      >
        <el-option label="房票签约阶段" :value="0"></el-option>
        <el-option label="选房阶段" :value="1"></el-option>
        <el-option label="贷款审查阶段" :value="2"></el-option>
        <el-option label="开发商审核阶段" :value="2.5"></el-option>
        <el-option label="签订四方协议阶段" :value="3"></el-option>
        
        <el-option label="登记备案阶段" :value="4"></el-option>
        <el-option label="结算确认阶段" :value="5"></el-option>
        <!-- <el-option label="管理员审核阶段" :value="5.5"></el-option> -->
        <el-option label="还款阶段" :value="6"></el-option>
        <el-option label="完结" :value="7"></el-option>
      </el-select>

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

      <!-- <ReadExcel @listenToChildEvent="getExcelData" ref="readExcel"></ReadExcel>

       -->
      <el-button
        class="filter-item"
        type="primary"
        @click="handleExport"
        icon="el-icon-download"
        >导出Excel</el-button
      >
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <el-table-column label="项目名称" align="center" v-if="isAll">
        <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.nameB }}</span>
        </template>
      </el-table-column>

      <el-table-column label="身份证号码" align="center" width="95">
        <template slot-scope="scope">
          <span>{{ scope.row.idCardB }}</span>
        </template>
      </el-table-column>

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

      <el-table-column label="房票发放街道" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.streetOfficeNameA }}</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.numberOfPlacements }}</span>
        </template>
      </el-table-column>

      <el-table-column label="换房套数" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.huNumberOfPlacements }}</span>
        </template>
      </el-table-column>

      <el-table-column label="已购房源" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.housingInfo }}</span>
        </template>
      </el-table-column>

      <el-table-column label="工单阶段" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.phase | phaseFilter }}</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.phase !== 0"
            type="primary"
            size="mini"
            @click="handleDetail(scope.row.id)"
            >工单详情</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 { list, listExport } from "@/api/ticket";
import { list as projectList } from "@/api/project";
import Pagination from "@/components/Pagination/index";
import { mapGetters } from "vuex";
export default {
  computed: {
    ...mapGetters(["roles"]),
  },
  components: { Pagination },
  filters: {
    phaseFilter(phase) {
      const phaseMap = {
        0: "房票签约阶段",
        1: "选房阶段",
        2: "贷款审查阶段",
        2.5:"开发商审核阶段",
        3: "签订四方协议阶段",
       
        4: "登记备案阶段",
        5: "结算确认阶段",
        //  5.5:"管理员审核阶段",
        6: "还款阶段",
        7: "完结",
      };
      return phaseMap[phase];
    },
    currentAccountPhase(status) {
      const statusMap = {
        0: "待处理",
        1: "已处理",
        // 2: "非本账号阶段",
        3: "银行已预审",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      total: 0,
      list: [],
      listQuery: {
        current: 1,
        size: 10,
        projectId: undefined,
      },
      listLoading: false,
      //   housingStatusOptions,
      downloadLoading: false,
      dialogProject: false,
      form: {},
      textMap: {
        update: "修改",
        create: "创建",
      },
      dialogStatus: "",
      rules: {
        name: { required: true, message: "不能为空", trigger: "blur" },
        // detail: { required: true, message: "不能为空", trigger: "blur" },
      },
      isAll: false,
      projectOptions: [],
    };
  },
  created() {
    this.listQuery.projectId = this.$route.query.id;
    this.isAll = this.$route.name === "AllUserList";
    this.getList();
    this.getProjects();
  },
  methods: {
    openMethods() {
      this.$refs.form.clearValidate();
    },
    getList() {
      list(this.listQuery).then((res) => {
        this.list = res.d.records;
        this.total = res.d.total;
      });
    },
    getProjects() {
      projectList({ current: 1, size: 99999 }).then((res) => {
        this.projectOptions = res.d.records;
      });
    },
    handleSearch() {
      this.listQuery.current = 1;
      this.getList();
    },
    handleDetail(id) {
      this.$router.push({ path: "/ticket/ticketProgress", query: { id } });
    },
    handleExport() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then((excel) => {
        const tHeader = [
          "房票协议编号",
          "用户姓名",
          "身份证号码",
          "手机号",
          "房票发放街道",
          "房票金额",
          "安置人数",
          "换房套数",
          "已购房源",
          "购房总价",
          "首付金额",
          "贷款金额",
          "贷款期数",
          "月供金额",
          "第一次还款金额",
          "第二次还款金额",
          "结算补差",
          "工单阶段",
          "安置用户",
        ];
        const filterVal = [
          "numbering",
          "nameB",
          "idCardB",
          "phoneNumberB",
          "streetOfficeNameA",
          "number",
          "numberOfPlacements",
          "huNumberOfPlacements",
          "housingInfo",
          "totalPrice",
          "totalFirstPay",
          "loanAmount",
          "numberOfInstallments",
          "amountOfInstallments",
          "threeAspectAmount1",
          "threeAspectAmount2",
          "shouldBeRefunded",
          "phase",
          "placementsData",
        ];

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

        listExport(payload).then((response) => {
          excel.export_json_to_excel({
            header: tHeader,
            data: this.formatJson(filterVal, response.d),
            filename: "用户列表",
          });
        });

        this.downloadLoading = false;
      });
    },
    formatJson(filterVal, jsonData) {
      return jsonData.map((v) =>
        filterVal.map((j) => {
          if (j === "phase") {
            const map = {
              0: "房票签约阶段",
              1: "选房阶段",
              2: "贷款审查阶段",
              3: "签订四方协议阶段",
              4: "登记备案阶段",
              5: "结算确认阶段",
              6: "还款阶段",
              7: "完结",
            };
            return map[v[j]];
            // return this.housingStatusOptions.find((item) => item.value === v[j])
            //   .label;
          } else {
            return v[j];
          }
        })
      );
    },
  },
};
</script>

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