<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.guest_delete"-->
<!--                   @click="handleDelete">删 除-->
<!--        </el-button>-->
<!--      </template>-->
      <template slot="checkState" slot-scope="scope">
        <el-tag v-if="scope.row.checkState===0" type="info">待审核</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>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/estate/guest";
  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,
          addBtn:false,
          delBtn:false,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "访客名称",
              prop: "guestName",
              search: true,
              rules: [{
                required: true,
                message: "请输入访客名称",
                trigger: "blur"
              }]
            },
            {
              label: "手机号",
              prop: "guestTel",
              rules: [{
                required: false,
                message: "请输入手机号",
                trigger: "blur"
              }]
            },
            {
              label: "性别",
              prop: "guestSex",
              type: 'select',
              dataType:"number",
              dicData:[
                {
                  label:"男",
                  value:1
                },{
                  label:'女',
                  value:0
                },{
                  label: "暂无信息",
                  value:-1
                }
              ],
              // dicUrl: "/api/blade-system/dict-biz/dictionary?code=sex",
              // props: {
              //   label: "dictValue",
              //   value: "dictKey"
              // },
              rules: [{
                required: true,
                message: "请输入性别",
                trigger: "blur"
              }]
            },
            {
              label: "访问缘由",
              prop: "guestReason",
              type: 'textarea',
              rules: [{
                required: false,
                message: "请输入访问缘由",
                trigger: "blur"
              }]
            },
            {
              label: "所属社区",
              prop: "agencyId",
              search: true,
              searchFilterable: true,
              filterable: true,
              cascaderItem: ['residentialId'],
              type: "select",
              // remote: true,
              dicUrl: "/api/cyzh-community/agency/list",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属社区id",
                trigger: "blur"
              }]
            },
            {
              label: "所属社区名称",
              prop: "agencyName",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入所属社区名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属小区",
              prop: "residentialId",
              type: "select",
              cascaderItem: ['buildingId'],
              dicUrl: "/api/cyzh-community/residential/list?agencyId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              // dicFlag: false,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: false,
                message: "请输入所属小区id",
                trigger: "blur"
              }]
            },
            {
              label: "小区名称",
              prop: "residentialName",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入小区名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属楼栋",
              prop: "buildingId",
              hide: true,
              type: "select",
              cascaderItem: ['unitId'],
              dicUrl: `/api/cyzh-community/building/list?residentialId={{key}}`,
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              // dicFlag: false,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: false,
                message: "请输入楼栋id",
                trigger: "blur"
              }]
            },
            {
              label: "楼栋名称",
              prop: "buildingName",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入楼栋名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属单元",
              prop: "unitId",
              hide: true,
              type: "select",
              cascaderItem: ['floorId'],
              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: false,
                message: "请输入所属单元id",
                trigger: "blur"
              }]
            },
            {
              label: "单元名称",
              prop: "unitName",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入单元名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属楼层",
              prop: "floorId",
              type: "select",
              hide: true,
              cascaderItem: ['roomId'],
              dicUrl: "/api/cyzh-community/floor/list?unitId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              // dicFlag: false,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: false,
                message: "请输入楼层id",
                trigger: "blur"
              }]
            },
            {
              label: "楼层名称",
              prop: "floorName",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入楼层名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属房间",
              prop: "roomId",
              type: "select",
              hide: true,
              dicUrl: "/api/cyzh-community/room/list?floorId={{key}}",
              dicFormatter:(res)=>{
                return res.data.records;//返回字典的层级结构
              },
              // dicFlag: false,
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: false,
                message: "请输入所属房间id",
                trigger: "blur"
              }]
            },
            {
              label: "所属房间名称",
              prop: "roomName",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入所属房间名称",
                trigger: "blur"
              }]
            },
            {
              label: "被访者姓名",
              prop: "userName",
              rules: [{
                required: true,
                message: "请输入要访问的住户姓名",
                trigger: "blur"
              }]
            },
            {
              label: "访问的住户的id",
              prop: "userId",
              hide: true,
              display: false,
              rules: [{
                required: false,
                message: "请输入访问的住户的id",
                trigger: "blur"
              }]
            },
            {
              label: "企业id",
              prop: "enterpriseId",
              hide: true,
              display: false,
              rules: [{
                required: false,
                message: "请输入企业id",
                trigger: "blur"
              }]
            },
            {
              label: "企业名称",
              prop: "enterpriseName",
              hide: true,
              display: false,
              rules: [{
                required: false,
                message: "请输入企业名称",
                trigger: "blur"
              }]
            },
            {
              label: "存储位置（通过该字段快速查询，所有的子节点）",
              prop: "orgPosition",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入存储位置（通过该字段快速查询，所有的子节点）",
                trigger: "blur"
              }]
            },
            {
              label: "审核状态",
              prop: "checkState",
              slot: true,
              display: false,
              type:'select',
              dicData:[
                {
                  label:'待审核',
                  value:0,
                  color:'red'
                },{
                  label:'审核通过',
                  value:1
                },{
                  label:'审核不通过',
                  value:2
                }
              ],
              rules: [{
                required: false,
                message: "请输入审核状态",
                trigger: "blur"
              }]
            },
            {
              label: "脸部照片",
              prop: "imageUri",
              type: "upload",
              width: 150,
              listType: 'picture-img',
              propsHttp: {
                res: "data",
                url: "link"
              },
              action: "/api/blade-resource/oss/endpoint/put-file",
              rules: [{
                required: false,
                message: "请输入脸部照片",
                trigger: "blur"
              }]
            },
            {
              label: "openId",
              prop: "openId",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入openId",
                trigger: "blur"
              }]
            },
            {
              label: "访问时间",
              prop: "interviewTime",
              type: 'datetime',
              valueFormat: 'yyyy-MM-dd HH:mm:ss',
              hide: true,
              rules: [{
                required: false,
                message: "请输入访问时间",
                trigger: "blur"
              }]
            },
            {
              label: "身份证",
              prop: "guestIdcard",
              hide: true,
              rules: [{
                required: false,
                message: "请输入身份证",
                trigger: "blur"
              }]
            },
            {
              label: "审核意见",
              prop: "opinion",
              hide: true,
              rules: [{
                required: false,
                message: "请输入审核意见",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.guest_add, false),
          viewBtn: this.vaildData(this.permission.guest_view, false),
          delBtn: this.vaildData(this.permission.guest_delete, false),
          editBtn: this.vaildData(this.permission.guest_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    created() {
      const tenantType = localStorage.getItem("tenantType");
      if (tenantType == 0){
        this.option.column[4].label = "所属社区"
        this.option.column[6].label = "所属小区"
      }else if (tenantType == 1){
        this.option.column[4].label = "所属园区"
        this.option.column[6].label = "所属区域"
      }
    },
    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>
