<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="水库名称" prop="resName">
        <el-input
          v-model="queryParams.data.resName"
          placeholder="请输入水库名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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="['res:base:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['res:base:edit']"
        >修改</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="['res:base:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['res:base:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="baseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="序号" type="index" width="50" align="center" fixed/>


      <el-table-column label="水库名称" align="center" prop="resName" min-width="120"/>
      <el-table-column label="水库中心经度" align="center" prop="resCenLong" min-width="120"/>
      <el-table-column label="水库中心纬度" align="center" prop="resCenLat" min-width="120"/>
      <el-table-column label="水库所在位置" align="center" prop="resLoc" min-width="120"/>
      <el-table-column label="水库类型" align="center" prop="resType" :formatter="resTypeFormat" min-width="120"/>
      <el-table-column label="管理单位" align="center" prop="mnun" min-width="120"/>
      <el-table-column label="主管单位" align="center" prop="cmun" min-width="120"/>
      <el-table-column label="工程等别" align="center" prop="engGrad" :formatter="engGradFormat" min-width="120"/>
      <el-table-column label="工程规模" align="center" prop="engScal" :formatter="engScalFormat" min-width="120"/>
      <el-table-column label="坝址控制流域面积" align="center" prop="watShedArea" min-width="120"/>
      <el-table-column label="工程建设状况" align="center" prop="engStat" :formatter="engStatFormat" min-width="120"/>
      <el-table-column label="开工时间" align="center" prop="startDate" min-width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="建成时间" align="center" prop="compDate" min-width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.compDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="归口管理部门" align="center" prop="admDep" :formatter="admDepFormat" min-width="120"/>
      <el-table-column label="记录生效时间" align="center" prop="effDate" min-width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['res:base:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['res:base: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="1200px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item label="水库名称" prop="resName">
          <el-input v-model="form.resName" placeholder="请输入水库名称" />
        </el-form-item>
        <el-form-item label="水库中心经度" prop="resCenLong">
          <el-input v-model="form.resCenLong" placeholder="请输入水库中心经度" />
        </el-form-item>
        <el-form-item label="水库中心纬度" prop="resCenLat">
          <el-input v-model="form.resCenLat" placeholder="请输入水库中心纬度" />
        </el-form-item>
        <el-form-item label="水库所在位置" prop="resLoc">
          <el-input v-model="form.resLoc" placeholder="请输入水库所在位置" />
        </el-form-item>
        <el-form-item label="水库类型" prop="resType">
          <el-select v-model="form.resType" placeholder="请选择水库类型" style="width:100%">
            <el-option
              v-for="dict in resTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="管理单位" prop="mnun">
          <el-input v-model="form.mnun" placeholder="请输入管理单位" />
        </el-form-item>
        <el-form-item label="主管单位" prop="cmun">
          <el-input v-model="form.cmun" placeholder="请输入主管单位" />
        </el-form-item>
        <el-form-item label="工程等别" prop="engGrad">
          <el-select v-model="form.engGrad" placeholder="请选择工程等别" style="width:100%">
            <el-option
              v-for="dict in engGradOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工程规模" prop="engScal">
          <el-select v-model="form.engScal" placeholder="请选择工程规模" style="width:100%">
            <el-option
              v-for="dict in engScalOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="坝址控制流域面积" prop="watShedArea">
          <el-input v-model="form.watShedArea" placeholder="请输入坝址控制流域面积" />
        </el-form-item>
        <el-form-item label="工程建设状况" prop="engStat">
          <el-select v-model="form.engStat" placeholder="请选择工程建设状况" style="width:100%">
            <el-option
              v-for="dict in engStatOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开工时间" prop="startDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.startDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开工时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="建成时间" prop="compDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.compDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择建成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="归口管理部门" prop="admDep">
          <el-select v-model="form.admDep" placeholder="请选择归口管理部门" style="width:100%">
            <el-option
              v-for="dict in admDepOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>        </el-form-item>
        <el-form-item label="水库概况" prop="resOv">
          <el-input v-model="form.resOv" placeholder="请输入水库概况"
                    type="textarea"
          />
        </el-form-item>
        <el-form-item label="记录生效时间" prop="effDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.effDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择记录生效时间">
          </el-date-picker>
        </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 { listBase, getBase, delBase, addBase, updateBase, exportBase } from "@/api/res/base";

export default {
  name: "Base",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 水库基本信息表格数据
      baseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 水库类型字典
      resTypeOptions: [],
      // 工程等别字典
      engGradOptions: [],
      // 工程规模字典
      engScalOptions: [],
      // 工程建设状况字典
      engStatOptions: [],
      // 归口管理部门字典
      admDepOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ids:null,
        data:{
                                                                                                                                                                                                                                                        resName: null,
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    },
        // 排序方式
        params: {
          // 按哪个字段排序
          orderBy: "create_time",
          // desc降序，升序asc
          sort: "desc",
        },

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("reservoir_type").then(response => {
      this.resTypeOptions = response.data;
    });
    this.getDicts("res_engineering_grade").then(response => {
      this.engGradOptions = response.data;
    });
    this.getDicts("res_project_scale").then(response => {
      this.engScalOptions = response.data;
    });
    this.getDicts("res_engineering_condition").then(response => {
      this.engStatOptions = response.data;
    });
    this.getDicts("res_department").then(response => {
      this.admDepOptions = response.data;
    });
  },
  methods: {
    /** 查询水库基本信息列表 */
    getList() {
      this.loading = true;
      listBase(this.queryParams).then(response => {
        this.baseList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 水库类型字典翻译
    resTypeFormat(row, column) {
      return this.selectDictLabel(this.resTypeOptions, row.resType);
    },
    // 工程等别字典翻译
    engGradFormat(row, column) {
      return this.selectDictLabel(this.engGradOptions, row.engGrad);
    },
    // 工程规模字典翻译
    engScalFormat(row, column) {
      return this.selectDictLabel(this.engScalOptions, row.engScal);
    },
    // 工程建设状况字典翻译
    engStatFormat(row, column) {
      return this.selectDictLabel(this.engStatOptions, row.engStat);
    },
    // 归口管理部门字典翻译
    admDepFormat(row, column) {
      return this.selectDictLabel(this.admDepOptions, row.admDep);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createUid: null,
        updateUid: null,
        createTime: null,
        updateTime: null,
        remark: null,
        resCode: null,
        resName: null,
        resCenLong: null,
        resCenLat: null,
        resLoc: null,
        resType: null,
        mnun: null,
        cmun: null,
        engGrad: null,
        engScal: null,
        watShedArea: null,
        engStat: null,
        startDate: null,
        compDate: null,
        admDep: null,
        resOv: null,
        effDate: null,
        note: null
      };
      this.resetForm("form");
    },
    // 查询表单重置
    resetQueryForm() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
          data:{
                                                          id: null,
                                                                            createUid: null,
                                                                            updateUid: null,
                                                                            createTime: null,
                                                                            updateTime: null,
                                                                            remark: null,
                                                                            resCode: null,
                                                                            resName: null,
                                                                            resCenLong: null,
                                                                            resCenLat: null,
                                                                            resLoc: null,
                                                                            resType: null,
                                                                            mnun: null,
                                                                            cmun: null,
                                                                            engGrad: null,
                                                                            engScal: null,
                                                                            watShedArea: null,
                                                                            engStat: null,
                                                                            startDate: null,
                                                                            compDate: null,
                                                                            admDep: null,
                                                                            resOv: null,
                                                                            effDate: null,
                                                                            note: null
                                          }
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetQueryForm();
      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
      getBase(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) {
            updateBase(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addBase(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if(ids){
            this.$confirm("是否删除选中的数据？", "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function() {
              return delBase(ids);
            }).then(() => {
              this.getList();
              this.msgSuccess("删除成功");
            }).catch(function() {});
      }else{
          this.$message.warning("请选择要删除的数据！！");
      }

    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      let message = "是否确认导出所有的数据项?";
      if(this.ids){
        message = "是否确认导出选中的数据项?";
        queryParams.ids = this.ids;
      }
      this.$confirm(message, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportBase(queryParams);
        }).then(response => {
            this.downloadFile(response, true, response.msg);
          // this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style lang="scss" scoped>
    @import "@/assets/css/dialog.scss";
    // ::v-deep {
    //   .el-dialog {
    //     margin-top: 2vh !important;
    //   }
    // }
</style>
