<template>
  <div class="app-container">
    <div class="filter-container">
      <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-button
        class="filter-item"
        type="primary"
        @click="handleSearch"
        icon="el-icon-search"
        >搜索</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        @click="handleCreate"
        icon="el-icon-plus"
        >新建工单</el-button
      >
      <el-button
        class="filter-item"
        type="primary"
        @click="handleExport"
        icon="el-icon-download"
        >导出Excel</el-button
      >
    </div>
    <el-table
      :data="list"
      style="width: 100%; margin-bottom: 20px"
      row-key="id"
      border
    >
      <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">
        <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.number }}</span>
        </template>
      </el-table-column>

      <el-table-column label="安置人数" v-if="roles[0] === 3" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.numberOfPlacements }}</span>
        </template>
      </el-table-column>

      <el-table-column label="换房套数" v-if="roles[0] === 3" 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.currentAccountPhase | currentAccountPhase }}</span>
        </template>
      </el-table-column>

      <el-table-column label="创建时间" align="center" width="100">
        <template slot-scope="scope">
          <span>{{
            scope.row.createDate | parseTime("{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>

      <el-table-column label="处理时间" align="center" width="100">
        <template slot-scope="scope">
          <span>{{
            scope.row.phaseDealDate | parseTime("{y}-{m}-{d} {h}:{i}:{s}")
          }}</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="roles[0] === 3 && scope.row.phase===0"
            type="primary"
            size="mini"
            @click="handleUpdate(scope.row.id)"
            >修改协议</el-button
          >
          <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 Pagination from "@/components/Pagination/index";
import { mapGetters } from "vuex";
import { parseTime } from "@/utils";
export default {
  computed: {
    ...mapGetters(["roles"]),
  },
  components: { Pagination },
  filters: {
    phaseFilter(phase) {
      const phaseMap = {
        0: "房票签约阶段",
        1: "选房阶段",
        2: "贷款审查阶段",
        3: "签订四方协议阶段",
        4: "登记备案阶段",
        5: "结算确认阶段",
        6: "还款阶段",
        7: "完结",
      };
      return phaseMap[phase];
    },
    currentAccountPhase(status) {
      const statusMap = {
        0: "待处理",
        1: "已处理",
        null: "全部",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      total: 0,
      list: [],
      listQuery: {
        current: 1,
        size: 10,
        currentAccountPhase: 1,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      list(this.listQuery).then((res) => {
        this.list = res.d.records;
        this.total = res.d.total;
      });
    },
    handleSearch() {
      this.listQuery.current = 1;
      this.getList();
    },
    handleCreate() {
      this.$router.push({
        path: "/ticket/create",
      });
    },
    handleDetail(id) {
      this.$router.push({ path: "ticketProgress", query: { id } });
    },
    handleUpdate(id) {
      this.$router.push({ path: "detail", query: { id } });
    },
    handleExport() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then((excel) => {
        const tHeader = [
          "房票协议编号",
          "用户姓名",
          "身份证号码",
          "手机号",
          "房票金额",
          "安置人数",
          "换房套数",
          "安置用户",
        ];
        const filterVal = [
          "numbering",
          "nameB",
          "idCardB",
          "phoneNumberB",
          "number",
          "numberOfPlacements",
          "huNumberOfPlacements",
          "placementsData",
        ];

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

        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>
