<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
      >
    </div>
    <el-table
      v-loading="listLoading"
      :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.auditStatus === null
              ? ""
              : scope.row.auditStatus
              ? "通过"
              : "不通过"
          }}</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">
        <template slot-scope="scope">
          <span>{{ scope.row.phase | phaseFilter }}</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
            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"
    />

    <el-dialog
      :title="textMap[dialogStatus] + '项目'"
      :visible.sync="dialogProject"
      @opened="openMethods"
      width="30%"
    >
      <!-- :close-on-click-modal="false"
      :close-on-press-escape="false" -->
      <el-form :model="form" :rules="rules" ref="form" label-width="120px">
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.name"></el-input
        ></el-form-item>
        <el-form-item label="项目信息"
          ><el-input v-model="form.detail" type="textarea"></el-input
        ></el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogProject = false">取 消</el-button>
        <el-button type="primary" @click="createAndUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list } from "@/api/ticket";
import { dealPhase2 } from "@/api/housingTicket";
import Pagination from "@/components/Pagination/index";
// import { housingStatusOptions } from "@/utils";
// import ReadExcel from "@/components/ReadExcel";
export default {
  components: { Pagination },
  //   computed: {
  //     statusCmp() {
  //       return (status) => {
  //         return housingStatusOptions.find((item) => item.value === status).label;
  //       };
  //     },
  //   },
  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,
      },
      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" },
      },
    };
  },
  created() {
    // this.getBusinessList()
    this.getList();
  },
  methods: {
    openMethods() {
      this.$refs.form.clearValidate();
    },
    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.$nextTick(() => {}, 100);
      //   this.form = {};
      //   this.dialogStatus = "create";
      //   this.dialogProject = true;
      this.$router.push({
        path: "/ticket/create",
      });
    },
    createAndUpdate() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          createAndUpdate(this.form).then((res) => {
            if (res.c === 200) {
              this.$message.success(`${this.textMap[this.dialogStatus]}成功`);
              this.getList();
              this.dialogProject = false;
            }
          });
        }
      });
    },
    handerDetail(row) {
      const { id, type } = row;
      this.$router.push({
        path: "/housing/detail",
        query: { id, type },
      });
    },
    handleOffShelf(row) {
      let payload = {
        id: row.id,
        status: row.status === "SALE" ? "OFF_SHELF" : "SALE",
      };
      offShelf(payload).then((res) => {
        if (res.c === 200) {
          this.$message.success(
            (payload.status === "SALE" ? "上架" : "下架") + "成功"
          );
        }
        this.getList();
      });
    },
    // 修改(提交)
    confirm({ data, close, hideLoading }) {
      update(data)
        .then((res) => {
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs["table"].getList();
          close();
        })
        .catch(() => {
          hideLoading();
        });
    },
    // 修改(表单显示)
    handleUpdate(id) {
      // this.form = Object.assign({}, row);
      // this.dialogStatus = "update";
      // this.dialogProject = true;
      this.$router.push({ path: "detail", query: { id } });
    },
    // 重置密码
    handleResetPsw(row) {
      const { id } = row;
      this.$prompt("请输入新密码", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        closeOnClickModal: false,
        inputValidator: (value) => {
          if (!value) return "请输入新密码";
          if (value.length < 6) return "请输入6位以上密码";
          return true;
        },
        inputErrorMessage: "密码不正确",
      }).then(({ value }) => {
        reset({
          id,
          password: value,
        }).then((res) => {
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
      });
    },
    // 删除
    handerDel(row) {
      const { name, id } = row;
      this.$confirm(`是否删除${name}?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deleteInfo({ id }).then((res) => {
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs["table"].getList();
        });
      });
    },
    // handleAudit(id, type) {
    //   dealPhase2({
    //     id,
    //     type,
    //   }).then((res) => {
    //     console.log(res);
    //     this.$message.success(`${type === 0 && "预"}审核成功`);
    //   });
    // },
    getExcelData(data) {
      console.log(data);
    },
    handleExport() {
      this.downloadLoading = true;
      import("@/vendor/Export2Excel").then((excel) => {
        const tHeader = [
          "楼盘名称",
          "楼栋",
          "面积（㎡）",
          "户型",
          "楼层",
          "房号",
          "价格",
          "房屋情况介绍",
          "状态",
        ];
        const filterVal = [
          "propertiesForSaleDeveloperName",
          "building",
          "area",
          "houseType",
          "floor",
          "roomNumber",
          "price",
          "detail",
          "status",
        ];

        const payload = {
          size: 10000,
          current: 1,
          extra: {},
        };

        list(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 === "status") {
          //     return this.housingStatusOptions.find((item) => item.value === v[j])
          //       .label;
          //   } else {
          return v[j];
          //   }
        })
      );
    },

    handleFourPartyAgreement(id) {
      this.$router.push({ path: "/fourPartyAgreement/detail", query: { id } });
    },
    handleDetail(id) {
      this.$router.push({ path: "/ticket/ticketProgress", query: { id } });
    },
    // handerParent(row) {
    // businessInfoId
    // console.log(this.tableProps.filterFields);
    // this.tableProps.filterFields.push({
    //   prop: "businessInfoId",
    // });

    // this.tableProps.fields.secondaryType.props.disabled = true
    // },
  },
};
</script>

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