<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="管道长度" prop="pipeLength">
        <el-input
          v-model="queryParams.pipeLength"
          placeholder="请输入管道长度"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="管径" prop="pipeDiameter">
        <el-input
          v-model="queryParams.pipeDiameter"
          placeholder="请输入管径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="压力" prop="pipePressure">
        <el-input
          v-model="queryParams.pipePressure"
          placeholder="请输入压力"
          clearable
          size="small"
          @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="['supervise:pipe: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="['supervise:pipe: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="['supervise:pipe:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['supervise:pipe:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pipeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="管道长度" align="center" prop="pipeLength" />
      <el-table-column label="走向" align="center" prop="pipeTrend" />
      <el-table-column label="人员类型" align="center" prop="peopleOccupation" />
      <el-table-column label="建设年代" align="center" prop="buildDate" />
      <el-table-column label="建设单位" align="center" prop="buildUnit" />
      <el-table-column label="权属单位" align="center" prop="beyondEnterpriseId" />
      <el-table-column label="坐标" align="center" prop="coordinates" />
      <el-table-column label="备注" align="center" prop="remarks" />
      <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="['supervise:pipe:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supervise:pipe: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="管道长度" prop="pipeLength">
          <el-input v-model="form.pipeLength" placeholder="请输入管道长度" />
        </el-form-item>
        <el-form-item label="管径" prop="pipeDiameter">
          <el-input v-model="form.pipeDiameter" placeholder="请输入管径" />
        </el-form-item>
        <el-form-item label="压力" prop="pipePressure">
          <el-input v-model="form.pipePressure" placeholder="请输入压力" />
        </el-form-item>
        <el-form-item label="材质" prop="pipeMaterial">
          <el-input v-model="form.pipeMaterial" placeholder="请输入材质" />
        </el-form-item>
        <el-form-item label="埋深" prop="buriedDepth">
          <el-input v-model="form.buriedDepth" placeholder="请输入埋深" />
        </el-form-item>
        <el-form-item label="走向" prop="pipeTrend">
          <el-input v-model="form.pipeTrend" placeholder="请输入走向" />
        </el-form-item>

        <el-form-item label="人员类型" prop="peopleOccupation">
          <el-select v-model="form.peopleOccupation" placeholder="请选择人员类型">
            <el-option label="运行维护人员" value="1" />
            <el-option label="抢修人员" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="建设年代" prop="buildDate">
          <el-date-picker
            v-model="form.buildDate"
            type="date"
            placeholder="请选择建设年代"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="建设单位" prop="buildUnit">
          <el-input v-model="form.buildUnit" placeholder="请输入建设单位" />
        </el-form-item>


<!--        <el-form-item label="权属单位" prop="beyondEnterpriseId">-->
<!--          <el-input v-model="form.beyondEnterpriseId" placeholder="请输入权属单位" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="权属单位名称" prop="beyondEnterpriseName">-->
<!--          <el-input v-model="form.beyondEnterpriseName" placeholder="请输入权属单位名称" />-->
<!--        </el-form-item>-->

        <el-form-item label="权属单位" prop="beyondEnterpriseName">
          <el-select v-model="form.beyondEnterpriseId"  placeholder="请在下拉框中选择权属单位" maxlength="255" :disabled="false" clearable>
            <el-option v-for="item in test" :key="item.enterpriseId" :label="item.enterpriseName" :value="item.enterpriseId">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="坐标" prop="coordinates">
          <el-input v-model="form.coordinates" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" 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 { listPipe, getPipe, delPipe, addPipe, updatePipe, exportPipe, selectTEnterprise} from "@/api/regulation/pipe";

export default {
  name: "Pipe",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 管道信息表格数据
      infoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //下拉框数据
      test:{},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pipeLength: null,
        pipeDiameter: null,
        pipePressure: null,
        pipeMaterial: null,
        buriedDepth: null,
        pipeTrend: null,
        peopleOccupation: null,
        buildDate: null,
        buildUnit: null,
        beyondEnterpriseId: null,
        beyondEnterpriseName: null,
        coordinates: null,
        isDel: null,
        remarks: null
      },
      // 表单参数
      form: {},
      //下拉列表数据
      test: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询管道信息列表 */
    getList() {
      this.loading = true;
      listPipe(this.queryParams).then(response => {
        this.pipeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pipeId: null,
        pipeLength: null,
        pipeDiameter: null,
        pipePressure: null,
        pipeMaterial: null,
        buriedDepth: null,
        pipeTrend: null,
        peopleOccupation: null,
        buildDate: null,
        buildUnit: null,
        beyondEnterpriseId: null,
        beyondEnterpriseName: null,
        coordinates: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: 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.pipeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      //查询企业名称下拉框数据
      selectTEnterprise().then(response => {
        this.test = response.data;
      });
      this.reset();
      this.open = true;
      this.title = "添加管道信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      //查询企业名称下拉框数据
      selectTEnterprise().then(response => {
        this.test = response.data;
      });
      this.reset();
      const pipeId = row.pipeId || this.ids
      getPipe(pipeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改管道信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pipeId != null) {
            updatePipe(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPipe(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pipeIds = row.pipeId || this.ids;
      this.$confirm('是否确认删除管道信息编号为"' + pipeIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPipe(pipeIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有管道信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportPipe(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
