<template>
    <div class="app-container">
    <el-dialog v-dialog-drag :title="title" :visible.sync="openView" width="500px" append-to-body>
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="客户姓名" prop="xm">
            <el-input
            v-model="queryParams.xm"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="handleQuery"
            />
        </el-form-item>
        <el-form-item label="证件号码" prop="zjhm">
            <el-input
            v-model="queryParams.zjhm"
            placeholder="请输入证件号码"
            clearable
            @keyup.enter.native="handleQuery"
            />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="khjbxxList" ref="multipleTable"
        @select-all="onSelectAll" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"  />
        <el-table-column label="序号" type="index" align="center"/>
        <el-table-column label="姓名" align="center" prop="xm" />
        <el-table-column label="证件号码" align="center" prop="zjhm" />
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="selectView">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
        </el-dialog>
    </div>
</template>

<script>
import { listKhjbxxCommon } from "@/api/organization/khjbxx";

export default {
  name: "Khjbxx",
  props: {
      openView: {
          type: Boolean,
          default: false
      }
  },
  dicts: ['C0009', 'CZ036', 'CZ035', 'C0015', 'sys_user_sex', 'C0003', 'C0025', 'C0024', 'C0010', 'C0032', 'C0031', 'XZ002', 'CH045', 'CH063', 'CH065'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      title: "选择客户",
      // 是否显示dialog
      openView: this.$props.openView,
      selectlist: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xm: null,
        xb: null,
        zjhm: null,
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询养老机构_客户_基本信息列表 */
    getList() {
      this.loading = true;
      listKhjbxxCommon(this.queryParams).then(response => {
        this.khjbxxList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.selectlist = [];
      this.$emit("cancel", "");
    },
    selectView() {
        if (this.selectlist) {
            this.$emit("selectRow", this.selectlist[0]);
            this.cancel();
        } else {
            this.$modal.msgError("请选择客户");
        }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
    //   this.ids = selection.map(item => item.id)
    //   this.single = selection.length!==1
    //   this.multiple = !selection.length
    if (selection.length > 1) {
        var newRows = selection.filter((it, index) => {
          if (index == selection.length - 1) {
            this.$refs.multipleTable.toggleRowSelection(it, true);
            return true;
          } else {
            this.$refs.multipleTable.toggleRowSelection(it, false);
            return false;
          }
        });
        this.selectlist = newRows;
      } else {
        this.selectlist = selection;
      }
    },
    onSelectAll() {
        this.selectlist = [];
      this.$refs.multipleTable.clearSelection();
    },
  }
};
</script>
