<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="所属网格" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :disable-branch-nodes="true" style="width:215px;"
                    placeholder="请选择所属网格"/>
      </el-form-item>
      <el-form-item label="所属小区" prop="xqName">
        <el-input
          v-model="queryParams.xqName"
          placeholder="请输入所属小区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼栋名称" prop="ldName">
        <el-input
          v-model="queryParams.ldName"
          placeholder="请输入楼栋名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼栋类型" prop="ldType">
        <el-select
          v-model="queryParams.ldType"
          placeholder="楼栋类型"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_ld_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋类别" prop="ldSort">
        <el-select
          v-model="queryParams.ldSort"
          placeholder="楼栋类别"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_ldlb"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="楼栋结构" prop="ldjg">
        <el-select
          v-model="queryParams.ldjg"
          placeholder="楼栋结构"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_ldjg"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="取暖方式" prop="qnfs">
        <el-select
          v-model="queryParams.qnfs"
          placeholder="取暖方式"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_qnfs"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="楼层数" prop="lcNum">
        <el-input
          v-model="queryParams.lcNum"
          placeholder="请输入楼层数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单元数" prop="unitNum">
        <el-input
          v-model="queryParams.unitNum"
          placeholder="请输入单元数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="户数" prop="huNum">
        <el-input
          v-model="queryParams.huNum"
          placeholder="请输入户数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['szxc:ldinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['szxc:ldinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['szxc:ldinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['szxc:ldinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ldinfoList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="所属小区" align="center" prop="xqName" />
      <el-table-column label="楼栋名称" align="center" prop="ldName" />
      <el-table-column label="楼栋类型" align="center" prop="ldType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_ld_type" :value="scope.row.ldType"/>
        </template>
      </el-table-column>
      <el-table-column label="楼栋类别" align="center" prop="ldSort" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_ldlb" :value="scope.row.ldSort"/>
        </template>
      </el-table-column>
      <el-table-column label="楼栋结构" align="center" prop="ldjg" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_ldjg" :value="scope.row.ldjg"/>
        </template>
      </el-table-column>
      <el-table-column label="取暖方式" align="center" prop="qnfs" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_qnfs" :value="scope.row.qnfs"/>
        </template>
      </el-table-column>
      <el-table-column label="楼层数" align="center" prop="lcNum" />
      <el-table-column label="单元数" align="center" prop="unitNum" />
      <el-table-column label="户数" align="center" prop="huNum" />
      <el-table-column label="楼栋描述" align="center" prop="ldDescribe" />
      <el-table-column label="所属网格" align="center" prop="deptName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['szxc:ldinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['szxc:ldinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改楼栋信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="所属小区" prop="xqName">
          <el-input readonly
            placeholder="请选择所属小区"
            v-model="form.xqName">
            <el-button slot="append" icon="el-icon-search" @click="xqQuery()"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="所属网格" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入所属网格" disabled/>
        </el-form-item>
        <el-form-item label="楼栋名称" prop="ldName">
          <el-input v-model="form.ldName" placeholder="请输入楼栋名称" />
        </el-form-item>
        <el-form-item label="楼栋类型" prop="ldType">
          <el-select
            v-model="form.ldType"
            placeholder="楼栋类型"
            clearable
            style="width:100%"
          >
            <el-option
              v-for="dict in dict.type.szxc_ld_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋类别" prop="ldSort">
          <el-select
            v-model="form.ldSort"
            placeholder="楼栋类别"
            clearable
            style="width:100%"
          >
            <el-option
              v-for="dict in dict.type.szxc_ldlb"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="楼栋结构" prop="ldjg">
          <el-select
            v-model="form.ldjg"
            placeholder="楼栋结构"
            clearable
            style="width:100%"
          >
            <el-option
              v-for="dict in dict.type.szxc_ldjg"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="取暖方式" prop="qnfs">
          <el-select
            v-model="form.qnfs"
            placeholder="取暖方式"
            clearable
            style="width:100%"
          >
            <el-option
              v-for="dict in dict.type.szxc_qnfs"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="楼层数" prop="lcNum">
          <el-input v-model="form.lcNum" placeholder="请输入楼层数" />
        </el-form-item>
        <el-form-item label="单元数" prop="unitNum">
          <el-input v-model="form.unitNum" placeholder="请输入单元数" />
        </el-form-item>
        <el-form-item label="户数" prop="huNum">
          <el-input v-model="form.huNum" placeholder="请输入户数" />
        </el-form-item>
        <el-form-item label="楼栋描述" prop="ldDescribe">
          <el-input v-model="form.ldDescribe" type="textarea" placeholder="请输入楼栋描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="选择小区" :visible.sync="xqInfo.visible" width="1000px" append-to-body>
      <el-form :model="xqInfo.queryParams" ref="queryForm" size="small" :inline="true" label-width="70px">
        <el-form-item label="小区名称" prop="name">
          <el-input
            v-model="xqInfo.queryParams.name"
            placeholder="请输入小区名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="xqInfoQuery">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-alert title="双击选择户主" type="warning" :closable="false"></el-alert>
      <el-table v-loading="xqInfo.loading" :data="xqInfo.list" border @cell-dblclick="xqInfoDblclick">
        <el-table-column label="所属网格" align="center" prop="deptName" />
        <el-table-column label="小区类型" align="center" prop="xqType" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.szxc_xq_type" :value="scope.row.xqType"/>
          </template>
        </el-table-column>
        <el-table-column label="小区名称" align="center" prop="name" />
        <el-table-column label="楼栋数" align="center" prop="ldNum" />
        <el-table-column label="建筑面积" align="center" prop="jzArea" />
        <el-table-column label="占地面积" align="center" prop="zdArea" />
        <el-table-column label="物业管理方式" align="center" prop="wyType" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.szxc_wy_type" :value="scope.row.wyType"/>
          </template>
        </el-table-column>
        <el-table-column label="物业负责人" align="center" prop="wyResponse" />
        <el-table-column label="联系电话" align="center" prop="wyPhone" />
        <el-table-column label="小区地址" align="center" prop="xqAddress" />
        <el-table-column label="建成日期" align="center" prop="buildDate" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.buildDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="xqInfo.total>0"
        :total="xqInfo.total"
        :page.sync="xqInfo.queryParams.pageNum"
        :limit.sync="xqInfo.queryParams.pageSize"
        @pagination="getlistXqinfo"
      />
    </el-dialog>

  </div>
</template>

<script>
import { listLdinfo, getLdinfo, delLdinfo, addLdinfo, updateLdinfo } from "@/api/szxc/ldinfo";
import { listXqinfo } from "@/api/szxc/xqinfo";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Ldinfo",
  components: { Treeselect },
  dicts: ['szxc_xq_type', 'szxc_ld_type', 'szxc_ldlb', 'szxc_ldjg', 'szxc_qnfs' ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 楼栋信息表格数据
      ldinfoList: [],
      // 组织树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xqId: null,
        ldName: null,
        ldType: null,
        ldSort: null,
        ldjg: null,
        qnfs: null,
        lcNum: null,
        unitNum: null,
        huNum: null,
        ldDescribe: null,
        deptId: null,
        deptName: null,
        userId: null
      },
      // 表单参数
      form: {
        id: null,
        xqId: null,
        xqName:  null,
        ldName: null,
        ldType: null,
        ldSort: null,
        ldjg: null,
        qnfs: null,
        lcNum: null,
        unitNum: null,
        huNum: null,
        ldDescribe: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptId: null,
        deptName: null,
        userId: null,
      },
      // 表单校验
      rules: {
        xqId: [
          { required: true, message: "所属小区不能为空", trigger: "blur" }
        ],
        deptId: [
          { required: true, message: "部门id不能为空", trigger: "blur" }
        ],
      },
      // 小区信息
      xqInfo: {
        visible: false,
        // 遮罩层
        loading: false,
        // 总条数
        total: 0,
        // 查询参数
        queryParams:{
          pageNum: 1,
          pageSize: 10,
          name: null
        },
        // 查询列表
        list: [],
        // 选中数据
        selectRow: null
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询组织下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    selectedTree(node){
      this.form.deptId = node.id;
      this.form.deptName = node.label;
    },
    /** 查询楼栋信息列表 */
    getList() {
      this.loading = true;
      listLdinfo(this.queryParams).then(response => {
        this.ldinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        xqId: null,
        xqName:  null,
        ldName: null,
        ldType: null,
        ldSort: null,
        ldjg: null,
        qnfs: null,
        lcNum: null,
        unitNum: null,
        huNum: null,
        ldDescribe: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptId: null,
        deptName: null,
        userId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加楼栋信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLdinfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改楼栋信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLdinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLdinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除楼栋信息编号为"' + ids + '"的数据项？').then(function() {
        return delLdinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('szxc/ldinfo/export', {
        ...this.queryParams
      }, `ldinfo_${new Date().getTime()}.xlsx`)
    },
    // 小区查询
    xqQuery(){
      this.xqInfo.visible = true;
      this.getlistXqinfo()
    },
    /** 查询户主信息列表 */
    getlistXqinfo() {
      this.xqInfo.loading = true;
      listXqinfo(this.xqInfo.queryParams).then(response => {
        this.xqInfo.list = response.rows;
        this.xqInfo.total = response.total;
        this.xqInfo.loading = false;
      });
    },
    // 户主信息查询
    xqInfoQuery(){
      this.xqInfo.queryParams.pageNum = 1;
      this.getlistXqinfo();
    },
    // 户主选择 选中数据
    xqInfoDblclick(row) {
      this.xqInfo.selectRow = row;
      this.form.xqId = row.id;
      this.form.xqName = row.name;
      this.form.deptId = row.deptId;
      this.form.deptName = row.deptName;
      this.xqInfo.visible = false
    },
  }
};
</script>
