<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.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="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"
    />

    <el-dialog
      title="分期信息填写"
      :visible.sync="dialogAudit"
      @opened="openMethods"
      width="30%"
    >
      <el-form :model="form" :rules="rules" ref="form" label-width="120px">
        <el-form-item
          label="每期还款金额"
          prop="repaymentPlan.amountOfInstallments"
        >
          <el-input v-model="form.repaymentPlan.amountOfInstallments" />
        </el-form-item>
        <el-form-item label="分期数" prop="repaymentPlan.numberOfInstallments">
          <el-input v-model="form.repaymentPlan.numberOfInstallments" />
        </el-form-item>
        <el-form-item label="首次还款期限" prop="repaymentPlan.startDate">
          <el-date-picker
            v-model="form.repaymentPlan.startDate"
            type="datetime"
            value-format="timestamp"
            placeholder="选择日期时间"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogAudit = false">取 消</el-button>
        <el-button type="primary" @click="audit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { list } from "@/api/ticket";
import { dealPhase2 } from "@/api/housingTicket";
import Pagination from "@/components/Pagination/index";

export default {
  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: 0,
      },
      listLoading: false,
      downloadLoading: false,
      dialogAudit: false,
      form: {
        id: undefined,
        repaymentPlan: {
          amountOfInstallments: undefined,
          numberOfInstallments: undefined,
          startDate: undefined,
        },
        type: 0,
      },
      textMap: {
        update: "修改",
        create: "创建",
      },
      dialogStatus: "",
      rules: {
        "repaymentPlan.amountOfInstallments": [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        "repaymentPlan.numberOfInstallments": [
          {
            required: true,
            message: "不能为空",
            trigger: "blur",
          },
        ],
        "repaymentPlan.startDate": [
          { required: true, message: "不能为空", trigger: "change" },
        ],
      },
    };
  },
  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();
    },
    resetForm() {
      this.form = {
        id: undefined,
        repaymentPlan: {
          amountOfInstallments: undefined,
          numberOfInstallments: undefined,
          startDate: undefined,
        },
        type: 0,
      };
    },
    handleAudit(id, type) {
      if (type === 1) {
        this.dialogAudit = true;
      } else {
        this.resetForm();
        this.form.id = id;
        this.form.type = type;
        this.$confirm(`是否确认?`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          dealPhase2({ id, type }).then((res) => {
            if (res.c === 200) {
              this.$message.success("操作成功");
              this.getList();
            }
          });
        });
      }

      // if (type === 0) {
      //   dealPhase2({ id, type }).then((res) => {
      //     if (res.c === 200) {
      //       this.$message.success("预审核成功");
      //       this.getList();
      //     }
      //   });
      // } else {
      //   this.dialogAudit = true;
      // }
    },
    audit() {
      let payload = JSON.parse(JSON.stringify(this.form));
      payload.repaymentPlan.startDate = payload.repaymentPlan.startDate / 1000;
      dealPhase2(payload).then((res) => {
        if (res.c === 200) {
          this.$message.success(`审核成功`);
          this.dialogAudit = false;
          this.getList();
        }
      });
    },
    handleDetail(id) {
      this.$router.push({ path: "ticketProgress", query: { id } });
    },
  },
};
</script>

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