<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="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.floor_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
      <template slot="menu" slot-scope="scope">
        <el-button type="text" size="small" icon="el-icon-setting">
          <el-dropdown @command="tip">
            <span class="el-dropdown-link">
              操作<i class="el-icon-arrow-down el-icon--right"> </i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item divided @click.native="floorManage(scope.row.id,3)" icon="el-icon-s-home">房间管理</el-dropdown-item>
              <el-dropdown-item divided @click.native="floorManage(scope.row.id,4)" icon="el-icon-user-solid">住户管理</el-dropdown-item>
              <!-- <el-dropdown-item divided @click.native="tip(2,scope.row)">审核通过</el-dropdown-item> -->
            </el-dropdown-menu>
          </el-dropdown>
        </el-button>
        <!-- <el-button type="text" icon="el-icon-setting" size="mini" @click="manageResidential(scope.row.oldId)">管理</el-button> -->
      </template>
      <template slot="residentialId" slot-scope="scope">{{scope.row.residentialName}}</template>
      <template slot="buildingId" slot-scope="scope">{{scope.row.buildingName}}</template>
      <template slot="unitId" slot-scope="scope">{{scope.row.unitName}}</template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "../../api/community/floor.js";
  import {mapGetters} from "vuex";
  import {communityManage} from "@/views/community/util/communityManage";

  export default {
    data() {
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 60,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "楼层名称",
              prop: "name",
              rules: [{
                required: true,
                message: "请输入楼层名称",
                trigger: "blur"
              }]
            },
            // {
            //   label: "所属社区",
            //   prop: "agencyId",
            //   hide: true,
            //   display: false,
            //   slot: true,

            //   rules: [{
            //     required: true,
            //     message: "请选择所属社区",
            //     trigger: "blur"
            //   }]
            // },
            {
              label: "所属小区",
              prop: "residentialId",
              search: true,
              slot: true,
              cascaderItem: ['buildingId','unitId'],
              type: "select",
              remote: true,
              //回显的时候有问题，如果所选不在前十条内，下拉框只会显示id
              dicUrl: "/api/cyzh-community/residential/list?name={{key}}",
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请选择所属小区",
                trigger: "blur"
              }]
            },
            {
              label: "所属楼栋",
              prop: "buildingId",
              slot: true,
              search: true,
              type: "select",
              cascaderIndex:0,
              dicUrl: "/api/cyzh-community/building/list?residentialId={{key}}",
              filterable: true,
              dicFlag: false,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请选择所属楼栋",
                trigger: "blur"
              }]
            },
            {
              label: "所属单元",
              prop: "unitId",
              search: true,
              slot: true,
              type: "select",
              dicFlag: false,
              cascaderIndex: 1,
              dicUrl: "/api/cyzh-community/unit/list?buildingId={{key}}",
              filterable: true,
              props: {
                label: "name",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入所属单元id",
                trigger: "blur"
              }]
            },
            {
              label: "负责人姓名",
              prop: "personName",
              hide: true,
              labelWidth: 100,
              rules: [{
                required: false,
                message: "请输入负责人姓名",
                trigger: "blur"
              }]
            },
            {
              label: "负责人电话",
              prop: "personTel",
              labelWidth: 100,
              hide: true,
              rules: [{
                required: false,
                message: "请输入负责人电话",
                trigger: "blur"
              }]
            },
            {
              label: "建筑年代",
              prop: "builtYear",
              hide: true,
              rules: [{
                required: false,
                message: "请输入建筑年代",
                trigger: "blur"
              }]
            },
            {
              label: "产权性质",
              prop: "propertyNature",
              hide: true,
              rules: [{
                required: false,
                message: "请输入产权性质",
                trigger: "blur"
              }]
            },
            {
              label: "有无电梯",
              prop: "includingElevator",
              type: "select",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=including_elevator",
              props:{
                label: "dictValue",
                value: "dictKey",
              },
              dataType: "number",
              hide: true,
              rules: [{
                required: false,
                message: "请输入有无电梯",
                trigger: "blur"
              }]
            },
            {
              label: "房间数量",
              prop: "roomNumber",
              display: false,
              rules: [{
                required: false,
                message: "请输入房间数量",
                trigger: "blur"
              }]
            },
            {
              label: "住户数量",
              prop: "userNumber",
              hide: true,
              display: false,
              rules: [{
                required: false,
                message: "请输入住户数量",
                trigger: "blur"
              }]
            },
            {
              label: "序号",
              prop: "serial",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入序号",
                trigger: "blur"
              }]
            },
            {
              label: "备注",
              prop: "remark",
              hide: true,
              rules: [{
                required: false,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
            {
              label: "所属机构id（属于社区机构，则存社区机构ID;无社区机构，则存小区ID）",
              prop: "orgPid",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入所属机构id（属于社区机构，则存社区机构ID;无社区机构，则存小区ID）",
                trigger: "blur"
              }]
            },
            {
              label: "所属机构名称（属于社区机构，则存社区机构名称;无社区机构，则存小区名称）",
              prop: "orgPidName",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入所属机构名称（属于社区机构，则存社区机构名称;无社区机构，则存小区名称）",
                trigger: "blur"
              }]
            },
            {
              label: "根机构Id",
              prop: "rootOrgId",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入根机构Id",
                trigger: "blur"
              }]
            },
            {
              label: "原数据的Id",
              prop: "oldId",
              hide: true,
              display: false,
              rules: [{
                required: true,
                message: "请输入原数据的Id",
                trigger: "blur"
              }]
            },

          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.floor_add, false),
          viewBtn: this.vaildData(this.permission.floor_view, false),
          delBtn: this.vaildData(this.permission.floor_delete, false),
          editBtn: this.vaildData(this.permission.floor_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    mounted() {
      let tenantType = localStorage.getItem("tenantType");
      const residentialColumn = this.findObject(this.option.column, "residentialId");
      if (tenantType == 1) { //园区
        residentialColumn.label = "所属区域";
      } else { //社区
        residentialColumn.label = "所属小区";
      }
    },
    methods: {
      floorManage(id,type){
        let query = {floorId: id};
        communityManage(this,type,query)
      },
      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;
        if (this.$route.query){
          Object.assign(this.query,this.$route.query)
        }
        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>
