<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.houseuser_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/community/houseuser";
  import {mapGetters} from "vuex";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "住户名字",
              prop: "name",
              rules: [{
                required: true,
                message: "请输入住户名字",
                trigger: "blur"
              }]
            },
            {
              label: "社区id",
              prop: "agencyId",
              rules: [{
                required: true,
                message: "请输入社区id",
                trigger: "blur"
              }]
            },
            {
              label: "社区名称",
              prop: "agencyName",
              rules: [{
                required: true,
                message: "请输入社区名称",
                trigger: "blur"
              }]
            },
            {
              label: "小区id",
              prop: "residentialId",
              rules: [{
                required: true,
                message: "请输入小区id",
                trigger: "blur"
              }]
            },
            {
              label: "小区名称",
              prop: "residentialName",
              rules: [{
                required: true,
                message: "请输入小区名称",
                trigger: "blur"
              }]
            },
            {
              label: "楼栋id",
              prop: "buildingId",
              rules: [{
                required: true,
                message: "请输入楼栋id",
                trigger: "blur"
              }]
            },
            {
              label: "楼栋名称",
              prop: "buildingName",
              rules: [{
                required: true,
                message: "请输入楼栋名称",
                trigger: "blur"
              }]
            },
            {
              label: "单元id",
              prop: "unitId",
              rules: [{
                required: true,
                message: "请输入单元id",
                trigger: "blur"
              }]
            },
            {
              label: "单元名称",
              prop: "unitName",
              rules: [{
                required: true,
                message: "请输入单元名称",
                trigger: "blur"
              }]
            },
            {
              label: "楼层id",
              prop: "floorId",
              rules: [{
                required: true,
                message: "请输入楼层id",
                trigger: "blur"
              }]
            },
            {
              label: "楼层名称",
              prop: "floorName",
              rules: [{
                required: true,
                message: "请输入楼层名称",
                trigger: "blur"
              }]
            },
            {
              label: "房间id",
              prop: "roomId",
              rules: [{
                required: true,
                message: "请输入房间id",
                trigger: "blur"
              }]
            },
            {
              label: "房间名称",
              prop: "roomName",
              rules: [{
                required: true,
                message: "请输入房间名称",
                trigger: "blur"
              }]
            },
            {
              label: "机构路径",
              prop: "orgPosition",
              rules: [{
                required: true,
                message: "请输入机构路径",
                trigger: "blur"
              }]
            },
            {
              label: "审核状态：，0-待审核 1-审核通过 2-审核不通过",
              prop: "checkState",
              rules: [{
                required: true,
                message: "请输入审核状态：，0-待审核 1-审核通过 2-审核不通过",
                trigger: "blur"
              }]
            },
            {
              label: "审核意见",
              prop: "checkOpinion",
              rules: [{
                required: true,
                message: "请输入审核意见",
                trigger: "blur"
              }]
            },
            {
              label: "人员分类：常住；重点。。。。",
              prop: "personType",
              rules: [{
                required: true,
                message: "请输入人员分类：常住；重点。。。。",
                trigger: "blur"
              }]
            },
            {
              label: "手机号",
              prop: "tel",
              rules: [{
                required: true,
                message: "请输入手机号",
                trigger: "blur"
              }]
            },
            {
              label: "国籍，0 中国大陆 1 中国香港/澳门 2 中国台湾 3海外",
              prop: "nationality",
              rules: [{
                required: true,
                message: "请输入国籍，0 中国大陆 1 中国香港/澳门 2 中国台湾 3海外",
                trigger: "blur"
              }]
            },
            {
              label: "证件号码 中国大陆:身份证；中国香港/澳门:港澳居民来往内地通行证；中国台湾:台湾居民来往内地通行证；海外:护照",
              prop: "idCard",
              rules: [{
                required: true,
                message: "请输入证件号码 中国大陆:身份证；中国香港/澳门:港澳居民来往内地通行证；中国台湾:台湾居民来往内地通行证；海外:护照",
                trigger: "blur"
              }]
            },
            {
              label: "籍贯（nationality = 0时,国籍为中国大陆）",
              prop: "birthPlace",
              rules: [{
                required: true,
                message: "请输入籍贯（nationality = 0时,国籍为中国大陆）",
                trigger: "blur"
              }]
            },
            {
              label: "护照照片url",
              prop: "passportImgUri",
              rules: [{
                required: true,
                message: "请输入护照照片url",
                trigger: "blur"
              }]
            },
            {
              label: "入境证明url",
              prop: "entryImgUri",
              rules: [{
                required: true,
                message: "请输入入境证明url",
                trigger: "blur"
              }]
            },
            {
              label: "性别  0-未定义  1-男 2-女",
              prop: "sex",
              rules: [{
                required: true,
                message: "请输入性别  0-未定义  1-男 2-女",
                trigger: "blur"
              }]
            },
            {
              label: "备注",
              prop: "remark",
              rules: [{
                required: true,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
            {
              label: "脸部照片url（没有域名）",
              prop: "imageUri",
              rules: [{
                required: true,
                message: "请输入脸部照片url（没有域名）",
                trigger: "blur"
              }]
            },
            {
              label: "根据图片生成的faceId",
              prop: "faceCode",
              rules: [{
                required: true,
                message: "请输入根据图片生成的faceId",
                trigger: "blur"
              }]
            },
            {
              label: "小区省（id）",
              prop: "regionProvince",
              rules: [{
                required: true,
                message: "请输入小区省（id）",
                trigger: "blur"
              }]
            },
            {
              label: "小区市（id）",
              prop: "regionCity",
              rules: [{
                required: true,
                message: "请输入小区市（id）",
                trigger: "blur"
              }]
            },
            {
              label: "小区区/县（id）",
              prop: "regionArea",
              rules: [{
                required: true,
                message: "请输入小区区/县（id）",
                trigger: "blur"
              }]
            },
            {
              label: "住户来源创建类型，0-小程序添加 1-后台添加  2-后台导入 3-app添加",
              prop: "createType",
              rules: [{
                required: true,
                message: "请输入住户来源创建类型，0-小程序添加 1-后台添加  2-后台导入 3-app添加",
                trigger: "blur"
              }]
            },
            {
              label: "创建时间",
              prop: "createDate",
              rules: [{
                required: true,
                message: "请输入创建时间",
                trigger: "blur"
              }]
            },
            {
              label: "是否激活App",
              prop: "isMember",
              rules: [{
                required: true,
                message: "请输入是否激活App",
                trigger: "blur"
              }]
            },
            {
              label: "住户类型 1-业主 2-成员 3-租户",
              prop: "type",
              rules: [{
                required: true,
                message: "请输入住户类型 1-业主 2-成员 3-租户",
                trigger: "blur"
              }]
            },
            {
              label: "民族",
              prop: "nation",
              rules: [{
                required: true,
                message: "请输入民族",
                trigger: "blur"
              }]
            },
            {
              label: "政治面貌",
              prop: "political",
              rules: [{
                required: true,
                message: "请输入政治面貌",
                trigger: "blur"
              }]
            },
            {
              label: "文化程度",
              prop: "education",
              rules: [{
                required: true,
                message: "请输入文化程度",
                trigger: "blur"
              }]
            },
            {
              label: "旧平台id",
              prop: "oldId",
              rules: [{
                required: true,
                message: "请输入旧平台id",
                trigger: "blur"
              }]
            },
            {
              label: "人员标签",
              prop: "personTag",
              rules: [{
                required: true,
                message: "请输入人员标签",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.houseuser_add, false),
          viewBtn: this.vaildData(this.permission.houseuser_view, false),
          delBtn: this.vaildData(this.permission.houseuser_delete, false),
          editBtn: this.vaildData(this.permission.houseuser_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
