<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="房产类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择类型" clearable @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in options"
            :key="item.label"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属单位" prop="appOrg">
        <el-cascader :options="treeList" placeholder="请选择所属单位" v-model="queryParams.appOrg" :props="props" style="width: 400px">
          <template slot-scope="{ node, data }">
            <span>{{ data.appName }}</span>
          </template>
        </el-cascader>
      </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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['zcustom:platHousingLand:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['zcustom:platHousingLand:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-upload"
          size="mini"
          @click="handleUpload"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <input v-show="false" type="file" ref="fileInput" @change="handleFileChange" />
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="platHousingLandList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="50">
        <template slot-scope="scope">
          {{(scope.$index + 1) + ((queryParams.pageNum - 1) * queryParams.pageSize)}}
        </template>
      </el-table-column>
      <el-table-column label="房产数量" align="center" prop="houseNum" />
      房产面积
      土地数量
      土地面积
      <el-table-column label="房产面积" align="center" prop="houseAcreage" />
      <el-table-column label="土地数量" align="center" prop="landNum" />
      <el-table-column label="土地面积" align="center" prop="landAcreage" />
      <el-table-column label="房产类型" align="center" prop="type">
        <template slot-scope="scope">
          {{options[scope.row.type].label}}
        </template>
      </el-table-column>
      <el-table-column label="所属单位" align="center" prop="appOrg">
        <template slot-scope="scope">
          {{findLabelByValue(treeList, scope.row.appOrg ? scope.row.appOrg : null)}}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" 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="['zcustom:platHousingLand:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['zcustom:platHousingLand: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" :close-on-click-modal="false" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="房产数量" prop="houseNum">
          <el-input v-model="form.houseNum" placeholder="请输入房产名称" />
        </el-form-item>
        <el-form-item label="房产面积" prop="houseAcreage">
          <el-input v-model="form.houseAcreage" placeholder="请输入房产面积" />
        </el-form-item>
        <el-form-item label="土地数量" prop="houseNum">
          <el-input v-model="form.landNum" placeholder="请输入土地数量" />
        </el-form-item>
        <el-form-item label="土地面积" prop="houseAcreage">
          <el-input v-model="form.landAcreage" placeholder="请输入土地面积" />
        </el-form-item>
        <el-form-item label="房产类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择类型">
            <el-option
              v-for="item in options"
              :key="item.label"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属单位" prop="appOrg">
          <el-cascader :options="treeList" placeholder="请选择所属单位" v-model="form.appOrg" :props="props" style="width: 400px">
            <template slot-scope="{ node, data }">
              <span>{{ data.appName }}</span>
            </template>
          </el-cascader>
        </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 {
  listPlatHousingLand,
  getPlatHousingLand,
  delPlatHousingLand,
  addPlatHousingLand,
  updatePlatHousingLand,
  uploadExcel
} from '@/api/zcustom/land'
import { parseTime } from '../../../utils/ruoyi'
import { getListTree } from '@/api/zcustom/platAppOrg'

export default {
  name: "PlatHousingLand",
  data() {
    return {
      treeList:[],
      props:{
        value: 'appOrg',
        label: 'appName',
        checkStrictly: true,
        emitPath: false
      },
      options:[
        {
          value: 0,
          label: "供电所"
        },
        {
          value: 1,
          label: "物资仓储"
        },
        {
          value: 2,
          label: "办公"
        },
        {
          value: 3,
          label: "周转"
        }
      ],
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房产土地表格数据
      platHousingLandList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        housing: null,
        acreage: null,
        type: null,
        constructionTime: null,
        ownershipFlag: null,
        investmentPlanFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        houseNum: [
          { required: true, message: "房产数量不能为空", trigger: "blur" }
        ],
        houseAcreage: [
          { required: true, message: "房产面积不能为空", trigger: "blur" }
        ],
        landNum: [
          { required: true, message: "土地数量不能为空", trigger: "blur" }
        ],
        landAcreage: [
          { required: true, message: "土地面积不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "房产类型不能为空", trigger: "change" }
        ],
        ownershipFlag: [
          { required: true, message: "所有权状态不能为空", trigger: "change" }
        ],
        appOrg: [
          { required: true, message: "所属单位id不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getListTree();
  },
  methods: {
    parseTime,
    /** 查询房产土地列表 */
    getList() {
      this.loading = true;
      listPlatHousingLand(this.queryParams).then(response => {
        this.platHousingLandList =  response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        delFlag: null,
        housing: null,
        acreage: null,
        type: null,
        constructionTime: null,
        ownershipFlag: null,
        investmentPlanFlag: null
      };
      this.resetForm("form");
    },
    getListTree(){
      getListTree().then(res => {
        this.treeList.push(res.data)
      })
    },
    /** 搜索按钮操作 */
    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
      getPlatHousingLand(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) {
            updatePlatHousingLand(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlatHousingLand(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除？').then(function() {
        return delPlatHousingLand(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导入按钮操作 */
    handleUpload() {
      this.$refs.fileInput.click()
    },
    handleFileChange(event) {
      const selectedFile = event.target.files[0];
      const formData = new FormData();
      formData.append('file', selectedFile);
      uploadExcel(formData).then(res =>{
        if (res.code === 200){
          this.$modal.msgSuccess("导入成功");
        }
      });
    },
    findLabelByValue(list, appOrg) {
      for (const item of list) {
        if (item.appOrg === appOrg) {
          return item.appName;
        }
        if (item.children) {
          const result = this.findLabelByValue(item.children, appOrg);
          if (result !== '无') return result;
        }
      }
      return "无";
    }
  }
};
</script>
