<template>
  <div class="app-container">
    <GetPos
      :dialogVisible.sync="dialogVisible"
      device="pipe"
      @close="dialogVisible=false"
      @getPath="getPath"
      :pipePath="this.coordinateslist"
    />

    <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" >
        <span slot-scope="scope" v-if="scope.row.pipeLength">{{scope.row.pipeLength}}</span>
        <span v-else>-</span>
      </el-table-column>
      <el-table-column label="走向" align="center" prop="pipeTrend" >
        <span slot-scope="scope" v-if="scope.row.pipeTrend">{{scope.row.pipeTrend}}</span>
        <span v-else>-</span>
      </el-table-column>
      <el-table-column label="管径(厘米)" align="center" prop="pipeDiameter" >
        <span slot-scope="scope" v-if="scope.row.pipeDiameter">{{scope.row.pipeDiameter}}</span>
        <span v-else>-</span>
      </el-table-column>
      <el-table-column label="压力" align="center" prop="pipePressure" >
        <span slot-scope="scope" v-if="scope.row.pipePressure">{{scope.row.pipePressure}}</span>
        <span v-else>-</span>
      </el-table-column>
<!--      <el-table-column label="人员类型" align="center" prop="peopleOccupation" />-->
      <el-table-column label="建设年代" align="center" prop="buildDate" >
        <span slot-scope="scope" v-if="scope.row.buildDate">{{scope.row.buildDate}}</span>
        <span v-else>-</span>
      </el-table-column>
      <el-table-column label="建设单位" align="center" prop="buildUnit" >
        <span slot-scope="scope" v-if="scope.row.buildUnit">{{scope.row.buildUnit}}</span>
        <span v-else>-</span>
      </el-table-column>
      <el-table-column label="权属单位" align="center" prop="beyondEnterpriseName" >
        <span slot-scope="scope" v-if="scope.row.beyondEnterpriseName">{{scope.row.beyondEnterpriseName}}</span>
        <span v-else>-</span>
      </el-table-column>
<!--      <el-table-column label="坐标" align="center" prop="coordinates" />-->
      <el-table-column label="备注" align="center" prop="remarks" >
        <span slot-scope="scope" v-if="scope.row.remarks">{{scope.row.remarks}}</span>
        <span v-else>-</span>
      </el-table-column>
      <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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-row>
          <el-col :span="11">
            <el-form-item label="管道长度" prop="pipeLength">
              <el-input v-model="form.pipeLength" placeholder="请输入管道长度" >
                <template slot="append">米</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="管径" prop="pipeDiameter">
              <el-input v-model="form.pipeDiameter" placeholder="请输入管径" >
                <template slot="append">厘米</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="压力" prop="pipePressure">
              <el-input v-model="form.pipePressure" placeholder="请输入压力" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="走向" prop="pipeTrend">
              <el-input v-model="form.pipeTrend" placeholder="请输入走向" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="埋深" prop="buriedDepth">
              <el-input v-model="form.buriedDepth" placeholder="请输入埋深" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="材质" prop="pipeMaterial">
              <el-input v-model="form.pipeMaterial" placeholder="请输入材质" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="11">
            <el-form-item label="建设单位" prop="buildUnit">
              <el-input v-model="form.buildUnit" placeholder="请输入建设单位" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <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"
                style="width: 100%">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
<!--          <el-col :span="11">-->
<!--            <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-col>-->

          <el-col :span="11">
            <el-form-item label="权属单位" prop="beyondEnterpriseName">
              <el-select style="width: 100%" 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-col>
        </el-row>

        <el-row>
          <el-col :span="19">
            <el-form-item label="坐标" prop="coordinates">
              <el-input v-model="form.coordinates"   type="textarea" placeholder="请输入坐标" />
            </el-form-item>
          </el-col>
          <el-col :span="3" style="margin-left: 30px">
            <el-button type="primary" plain @click="MapdialogFun">选择坐标</el-button>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="23">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" type="textarea" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>

      </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";
import GetPos from '@/components/GetPos';
export default {
  name: "Pipe",
  components: {
    GetPos
  },
  data() {
    return {
      dialogVisible:false,
      //地图管径回显数据
      coordinateslist:null,
      // 遮罩层
      loading: true,

      pipeList:[],
      // 导出遮罩层
      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: {
        pipeLength: [
          { required: true, message: "请输入管道长度", trigger: "blur" },
          // { min: 0, max: 10, message: "长度10位", trigger: "blur" },
        ],
        pipeDiameter: [
          { required: true, message: "请输入管径", trigger: "blur" },
          // { min: 0, max: 15, message: "长度15位", trigger: "blur" },
        ],
        pipePressure: [
          { required: true, message: "请输入压力", trigger: "blur" },
          // { min: 0, max: 10, message: "长度10位", trigger: "blur" },
        ],
        buildDate: [
          { required: true, message: "请选择建设年代", trigger: "blur" },
        ],
        pipeMaterial: [
          { required: true, message: "请输入材质", trigger: "blur" },
          // { min: 0, max: 10, message: "长度10位", trigger: "blur" },
        ],
        buriedDepth:[
          { required: true, message: "请输入埋深", trigger: "blur" },
          // { min: 0, max: 10, message: "长度10位", trigger: "blur" },
        ],
      }
    };
  },
  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.coordinateslist+=JSON.parse(null);
      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;
        const text=this.form.coordinates
        console.log(text)
        this.coordinateslist=JSON.parse(text);

        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(() => {});
    },
    /**管道信息 选择管道提交数据方法*/
    getPath(e){
      // for (let i=0;i<e.length;i++){
      //   console.log(e[i])
      //   coordinates.push(e[i])
      // }
      var a = '['
      for (var i in e) {
        if (i == e.length - 1) {
          a += '[' + e[i].toString()+']'
        } else {
          a += '[' + e[i].toString()+'],'
        }
      }
      a += ']'
     this.form.coordinates=a;
    },
    /** 管道打开方法*/
    MapdialogFun(){
      this.dialogVisible=true;
    }
  }
};
</script>
