<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="xqType">
        <el-select
          v-model="queryParams.xqType"
          placeholder="小区类型"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_xq_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小区名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入小区名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="楼栋数" prop="ldNum">
        <el-input
          v-model="queryParams.ldNum"
          placeholder="请输入楼栋数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建筑面积" prop="jzArea">
        <el-input
          v-model="queryParams.jzArea"
          placeholder="请输入建筑面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="占地面积" prop="zdArea">
        <el-input
          v-model="queryParams.zdArea"
          placeholder="请输入占地面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物业管理方式" prop="wyType">
        <el-select
          v-model="queryParams.wyType"
          placeholder="物业管理方式"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_wy_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="物业负责人" prop="wyResponse">
        <el-input
          v-model="queryParams.wyResponse"
          placeholder="请输入物业负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="wyPhone">
        <el-input
          v-model="queryParams.wyPhone"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="小区地址" prop="xqAddress">
        <el-input
          v-model="queryParams.xqAddress"
          placeholder="请输入小区地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="建成日期" prop="buildDate">
        <el-date-picker clearable
          v-model="queryParams.buildDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择建成日期">
        </el-date-picker>
      </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:xqinfo: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:xqinfo: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:xqinfo: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:xqinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="xqinfoList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="ID" align="center" prop="id" min-width="60"/>
      <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" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.buildDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="小区描述" align="center" prop="xqDescribe" />
      <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:xqinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['szxc:xqinfo: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="800px" append-to-body top="15px">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="所属网格" prop="deptId">
              <treeselect v-model="form.deptId" :options="deptOptions" :disable-branch-nodes="true"
                          placeholder="请选择所属网格" style="width:100%;" @select="selectedTree"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="小区名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入小区名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="小区类型" prop="xqType">
              <el-select
                v-model="form.xqType"
                placeholder="小区类型"
                clearable
                style="width:100%"
              >
                <el-option
                  v-for="dict in dict.type.szxc_xq_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="楼栋数" prop="ldNum">
              <el-input v-model="form.ldNum" placeholder="请输入楼栋数" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="建筑面积" prop="jzArea">
              <el-input v-model="form.jzArea" placeholder="请输入建筑面积" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="占地面积" prop="zdArea">
              <el-input v-model="form.zdArea" placeholder="请输入占地面积" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="物业管理方式" prop="wyType">
              <el-select
                v-model="form.wyType"
                placeholder="物业管理方式"
                clearable
                style="width:100%"
              >
                <el-option
                  v-for="dict in dict.type.szxc_wy_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物业负责人" prop="wyResponse">
              <el-input v-model="form.wyResponse" placeholder="请输入物业负责人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="联系电话" prop="wyPhone">
              <el-input v-model="form.wyPhone" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="建成日期" prop="buildDate">
              <el-date-picker clearable style="width: 100%"
                              v-model="form.buildDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择建成日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="小区地址" prop="xqAddress">
          <el-input v-model="form.xqAddress" placeholder="请输入小区地址" />
        </el-form-item>
        <el-form-item label="小区描述" prop="xqDescribe">
          <el-input v-model="form.xqDescribe" 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>
  </div>
</template>

<script>
import { listXqinfo, getXqinfo, delXqinfo, addXqinfo, updateXqinfo } 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: "Xqinfo",
  components: { Treeselect },
  dicts: ['szxc_xq_type', 'szxc_wy_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 小区信息表格数据
      xqinfoList: [],
      // 组织树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        xqType: null,
        ldNum: null,
        jzArea: null,
        zdArea: null,
        wyType: null,
        wyResponse: null,
        wyPhone: null,
        xqAddress: null,
        buildDate: null,
        xqDescribe: null,
        deptId: null,
        deptName: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deptId: [
          { required: true, message: "所属网格不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "小区名称不能为空", trigger: "blur" }
        ],
        xqType: [
          { required: true, message: "小区类型不能为空", trigger: "blur" }
        ],
        xqAddress: [
          { required: true, message: "小区地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  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;
      listXqinfo(this.queryParams).then(response => {
        this.xqinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        xqType: null,
        ldNum: null,
        jzArea: null,
        zdArea: null,
        wyType: null,
        wyResponse: null,
        wyPhone: null,
        xqAddress: null,
        buildDate: null,
        xqDescribe: 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
      getXqinfo(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) {
            updateXqinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addXqinfo(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 delXqinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('szxc/xqinfo/export', {
        ...this.queryParams
      }, `xqinfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
