<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.applyuser_delete"-->
<!--                   @click="handleDelete">删 除-->
<!--        </el-button>-->
      </template>

      <template slot="menu" slot-scope="scope">
        <el-button icon="el-icon-s-order" style="color: red" v-if="scope.row.checkState===0" size="small" @click.native="showType('审核',scope.row)" type="text">
          审核
        </el-button>
        <el-button icon="el-icon-view" size="small" @click.native="showType('详情',scope.row)" v-else type="text">
          详情
        </el-button>
      </template>
      <template slot="checkState" slot-scope="scope">
        <el-tag v-if="scope.row.checkState===-1" type="info">未上传</el-tag>
        <el-tag v-if="scope.row.checkState===0">待审核</el-tag>
        <el-tag v-if="scope.row.checkState===1" type="success">审核通过</el-tag>
        <el-tag v-if="scope.row.checkState===2" type="danger">审核不通过</el-tag>
      </template>

      <template slot="residentialId" slot-scope="scope">
        {{scope.row.residentialName}}
      </template>
    </avue-crud>
    <el-dialog title = "住户信息审核" :visible.sync="reviewVisible" :modal-append-to-body="false" >
      <HouseuserCheck v-if="reviewVisible" :reviewDetail="reviewDetail" :reviewType="reviewType" @updateEmit="houseuserCheck"></HouseuserCheck>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/estate/applyuser";
  import {mapGetters} from "vuex";
  import HouseuserCheck from '@/components/houseuser/houseuser-check';
  import {validatePhone} from "../../util/validator";

  export default {
    components: {
      HouseuserCheck
    },
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        reviewVisible: false,
        reviewDetail: {},
        reviewType: false,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          menuWidth: 120,
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          editBtn: false,
          addBtn: false,
          delBtn: false,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "住户id--修改时有值",
              prop: "userId",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入住户id--修改时有值",
                trigger: "blur"
              }]
            },
            {
              label: "申请的会员id",
              prop: "memberId",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入申请的会员id",
                trigger: "blur"
              }]
            },
            {
              label: "住户姓名",
              prop: "name",
              search: true,
              width: 100,
              rules: [{
                required: true,
                message: "请输入住户姓名",
                trigger: "blur"
              }]
            },
            {
              label: "性别",
              prop: "sex",
              width: 60,
              display: false,
              type: 'select',
              dataType: 'string',
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=sex",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入性别 未定义 男 女",
                trigger: "blur"
              }]
            },
            {
              label: "住户\n类型",
              prop: "type",
              type: 'select',
              display: false,
              width: 60,
              dataType: 'string',
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=household-type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入住户类型业主 成员 租户",
                trigger: "blur"
              }]
            },
            {
              label: "身份证",
              prop: "idCard",
              width: 150,
              search: true,
              rules: [{
                required: true,
                message: "请输入居民身份证",
                trigger: "blur"
              }]
            },
            {
              label: "所属小区",
              prop: "residentialName",
              addDisplay: false,
              editDisplay: false,
              rules: [{
                required: true,
                message: "请输入小区名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属小区",
              prop: "residentialId",
              type: "select",
              search: true,
              slot:true,
              searchFilterable: true,
              cascaderItem: ['buildingId'],
              dicUrl: "/api/cyzh-community/residential/select?size=500",
              // dicFormatter:(res)=>{
              //   return res.data.records;//返回字典的层级结构
              // },
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属小区id",
                trigger: "blur"
              }],
              hide: true,
              viewDisplay: false
            },
            {
              label: "所属楼栋",
              prop: "buildingId",
              type: "select",
              search: true,
              cascaderItem: ['unitId'],
              dicUrl: "/api/cyzh-community/building/list?residentialId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入楼栋id",
                trigger: "blur"
              }],
              hide: true,
              viewDisplay: false
            },
            {
              label: "所属\n楼栋",
              prop: "buildingName",
              width: 60,
              // display: false,
              // hide: true,
              rules: [{
                required: true,
                message: "请输入楼栋名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属单元",
              prop: "unitId",
              type: "select",
              search: true,
              cascaderItem: ['roomId'],
              dicUrl: "/api/cyzh-community/unit/list?buildingId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              dicFlag: false,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属单元id",
                trigger: "blur"
              }],
              hide: true,
              viewDisplay: false
            },
            {
              label: "所属\n单元",
              prop: "unitName",
              width: 60,
              // display: false,
              // hide: true,
              rules: [{
                required: true,
                message: "请输入单元名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属房间",
              prop: "roomId",
              type: "select",
              width: 60,
              search: true,
              dicUrl: "/api/cyzh-community/room/list?unitId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属房间id",
                trigger: "blur"
              }],
              hide: true,
              viewDisplay: false
            },
            {
              label: "所属\n房间",
              prop: "roomName",
              width: 60,
              // display: false,
              // hide: true,
              rules: [{
                required: true,
                message: "请输入所属房间名称",
                trigger: "blur"
              }]
            },
            {
              label: "联系电话",
              prop: "tel",
              search: true,
              width: 100,
              rules: [{
                required: true,
                message: "请输入联系电话",
              },{
                validator: validatePhone,
                trigger: "blur"
              }]
            },
            {
              label: "住户申请来源",
              prop: "createType",
              type: 'select',
              dataType: 'number',
              width: 100,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=create-type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入住户来源创建类型，0-小程序添加 1-后台添加  2-后台导入 3-app添加",
                trigger: "blur"
              }]
            },
            {
              label: "审核状态",
              prop: "checkState",
              search: true,
              editDisplay: false,
              slot: true,
              width: 100,
              type: 'select',
              dicData:[
                {
                  label: '未上传',
                  value: -1
                },
                {
                  label: '待审核',
                  value: 0
                },{
                  label: '审核通过',
                  value: 1
                },{
                  label: '审核不通过',
                  value: 2
                }
              ],
              rules: [{
                required: true,
                message: "请输入申请记录审核状态：-1：未上传，0-待审核 1-审核通过 2-审核不通过",
                trigger: "blur"
              }]
            },
            {
              label: "i_check_state=2(审核不通过)时必填",
              prop: "checkOpinion",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入i_check_state=2(审核不通过)时必填",
                trigger: "blur"
              }]
            },
            {
              label: "审核时间",
              prop: "checkDate",
              type: 'datetime',
              valueFormat:'yyyy-MM-dd HH:mm:ss',
              rules: [{
                required: false,
                message: "请输入审核时间",
                trigger: "blur"
              }]
            },
            {
              label: "国籍",
              prop: "nationality",
              hide: true,
              row: true,
              type: 'select',
              dataType: 'number',
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=nationality",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: false,
                message: "请输入国籍，0 中国大陆 1 中国香港/澳门 2 中国台湾 3海外",
                trigger: "blur"
              }]
            },
            {
              label: "护照照片",
              prop: "passportImgUri",
              hide: true,
              span: 8,
              type: 'upload',
              listType: 'picture-img',
              propsHttp: {
                res: "data",
                url: "link"
              },
              action: "/api/blade-resource/oss/endpoint/put-file",
              tip: '只能生产jpg/png图片，且不超过500kb',
              rules: [{
                required: false,
                message: "请输入护照照片url（没有域名，nationality = 3时,国籍为海外）",
                trigger: "blur"
              }]
            },
            {
              label: "入境证明",
              prop: "entryImgUri",
              hide: true,
              span: 8,
              type: 'upload',
              listType: 'picture-img',
              propsHttp: {
                res: "data",
                url: "link"
              },
              action: "/api/blade-resource/oss/endpoint/put-file",
              tip: '只能生产jpg/png图片，且不超过500kb',
              rules: [{
                required: false,
                message: "请输入入境证明url（没有域名，nationality = 3时,国籍为海外）",
                trigger: "blur"
              }]
            },
            {
              label: "存储位置（通过该字段快速查询，所有的子节点）",
              prop: "orgPosition",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入存储位置（通过该字段快速查询，所有的子节点）",
                trigger: "blur"
              }]
            },
            {
              label: "人脸图片uri",
              prop: "imageUri",
              hide: true,
              span: 8,
              type: 'upload',
              listType: 'picture-img',
              propsHttp: {
                res: "data",
                url: "link"
              },
              action: "/api/blade-resource/oss/endpoint/put-file",
              tip: '只能生产jpg/png图片，且不超过500kb',
              rules: [{
                required: false,
                message: "请输入人脸图片uri",
                trigger: "blur"
              }]
            },
            {
              label: "图片的md5",
              prop: "faceCode",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入图片的md5",
                trigger: "blur"
              }]
            },
            {
              label: "申请的记录类型 0--认证类型 1--新增类型 2--编辑类型(通过user编辑生成的) 3--编辑类型(通过applyUser编辑生成)",
              prop: "recordType",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入申请的记录类型 0--认证类型 1--新增类型 2--编辑类型(通过user编辑生成的) 3--编辑类型(通过applyUser编辑生成)",
                trigger: "blur"
              }]
            },
            {
              label: "旧照片",
              prop: "oldImageUri",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入旧照片",
                trigger: "blur"
              }]
            },
            {
              label: "审核人\n类型",
              prop: "operatorType",
              type: 'select',
              width: 70,
              dicData:[
                {
                  label: '无',
                  value: -1
                },
                {
                  label: '业主',
                  value: 1
                },{
                  label: '后台',
                  value: 2
                }
              ],
              rules: [{
                required: false,
                message: "请输入审核人类型：1-业主，2-后台",
                trigger: "blur"
              }]
            },
            {
              label: "审核人姓名",
              prop: "operatorName",
              rules: [{
                required: false,
                message: "请输入审核人姓名",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.applyuser_add, false),
          viewBtn: this.vaildData(this.permission.applyuser_view, false),
          delBtn: this.vaildData(this.permission.applyuser_delete, false),
          editBtn: this.vaildData(this.permission.applyuser_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      //组件方法
      houseuserCheck(data) {
        this.reviewVisible = data;
        this.onLoad(this.page);
      },
      showType(type,row) {
        console.log(type);
        this.reviewDetail = row;
        if (type == '审核') {
          this.reviewType = true;
        } else {
          this.reviewType = false;
        }
        this.reviewVisible = true;
      },
      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>
