<template>
  <basic-container>
    <el-dialog  :append-to-body="true" :close-on-click-modal="false" :modal-append-to-body="false" :visible.sync="editVisible" title="楼栋编辑"
                width="80%">
      <avue-form v-model="editForm" :option="option" @submit="submitBuilding"></avue-form>
    </el-dialog>
    <el-dialog :modal-append-to-body="false" :visible.sync="deviceDialog" title="设备管理" width="70%">
      <devicebuilding :buildingId="buildingId"></devicebuilding>
    </el-dialog>
    <el-dialog :modal-append-to-body="false" :visible.sync="videoDialog" title="设备管理" width="70%">
      <buildingvideo :buildingId="buildingId"></buildingvideo>
    </el-dialog>
    <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.building_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="edit(scope.row)" icon="el-icon-school">编辑</el-dropdown-item>
              <el-dropdown-item divided @click.native="buildingManage(scope.row.id,1)" icon="el-icon-school">单元管理</el-dropdown-item>
              <el-dropdown-item divided @click.native="buildingManage(scope.row.id,2)" icon="el-icon-s-order">楼层管理</el-dropdown-item>
              <el-dropdown-item divided @click.native="buildingManage(scope.row.id,3)" icon="el-icon-s-home">房间管理</el-dropdown-item>
              <el-dropdown-item divided @click.native="buildingManage(scope.row.id,4)" icon="el-icon-user-solid" v-if="permission.permanent_people_view">住户管理</el-dropdown-item>
              <el-dropdown-item divided @click.native="manageDevice(scope.row)" icon="el-icon-s-management">设备管理</el-dropdown-item>
              <el-dropdown-item divided @click.native="manageVideo(scope.row)" icon="el-icon-s-management">视频监控</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">
        <div>{{scope.row.residentialName}}</div>
      </template>
    </avue-crud>
  </basic-container>
</template>

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

  export default {
    components: {
      devicebuilding,
      buildingvideo
    },
    data() {
      return {
        buildingId : '',
        deviceDialog: false,
        videoDialog: false,
        editForm: {},
        editVisible: false,
        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,
          // addBtn: false,
          editBtn: false,
          delBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "楼栋名称",
              prop: "name",
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "所属小区",
              prop: "residentialId",
              type:"select",
              remote: true,
              //回显的时候有问题，如果所选不在前十条内，下拉框只会显示id
              dicUrl: "/api/cyzh-community/residential/list?name={{key}}",
              props:{
                label: "name",
                value: "id"
              },
              searchFilterable: true,
              search: true,
              slot: true,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "图片",
              prop: "pic",
              type: 'upload',
              dataType: 'string',
              hide: true,
              propsHttp: {
                url: 'data'
              },
              listType: 'picture-card',
              span: 24,
              row: true,
              value: "/img/default/building.png",
              action: "/api/upload/putfile",
              rules: [{
                required: false,
                message: "请输入封面图片",
                trigger: "blur"
              }]
            },
            {
              label: "单元数",
              prop: "unitNumber",
              display: false,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "楼层数",
              prop: "floorNumber",
              display: false,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "房间数",
              prop: "roomNumber",
              display: false,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
            {
              label: "总人口数",
              prop: "personNumber",
              display: false,
              hide: true,
              rules: [{
                required: true,
                message: "请输入",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.building_add, false),
          viewBtn: this.vaildData(this.permission.building_view, false),
          delBtn: this.vaildData(this.permission.building_delete, false),
          editBtn: this.vaildData(this.permission.building_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: {
      submitBuilding(row,done,loading){
        update(this.editForm).then(() => {
          this.onLoad(this.page);
          this.editVisible = false;
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      edit(row){
        this.editForm = row;
        this.editVisible = true;
      },
      buildingManage(id,type){
        let query = {buildingId: id};
        communityManage(this,type,query)
      },
      manageDevice(row){
        this.buildingId = row.id;
        this.deviceDialog = true
      },
      manageVideo(row){
        this.buildingId = row.id;
        this.videoDialog = 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;


        console.log("dfdf:"+ this.$route.query.residentialOldId);
        if(this.$route.query.residentialOldId != undefined){

          params.residentialId = this.$route.query.oldId;

        }
        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>
