<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('pileHole.pileHoleNo')" prop="holeNum">
        <el-input
            v-model="queryParams.holeNum"
            :placeholder="$t('common.typeInfo',{name: $t('pileHole.pileHoleNo')})"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('pileHole.pileHoleStatus')" prop="consStatus">
        <el-select v-model="queryParams.consStatus"
                   :placeholder="$t('common.select',{name:$t('pileHole.pileHoleStatus')})" clearable>
          <el-option
              v-for="dict in dict.type.pile_hole_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
            $t('common.search')
          }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="info"
            plain
            icon="el-icon-upload2"
            size="mini"
            @click="handleImport"
            v-hasPermi="['cons:pileHoleInfo:add']"
        >{{ $t("common.import") }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['cons:pileHoleInfo:add']"
        >{{ $t("common.add") }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['cons:pileHoleInfo:export']"
        >{{ $t("common.export") }}
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="pileHoleInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column :label="$t('common.index')" align="center" prop="id" width="120"/>
      <el-table-column :label="$t('pileHole.pileHoleNo')" align="center" prop="holeNum" width="120"/>
      <el-table-column label="dx" align="center" prop="deltaX" width="120"/>
      <el-table-column label="dy" align="center" prop="deltaY" width="120"/>
      <el-table-column :label="$t('pileHole.lng')" align="center" prop="lng" width="120"/>
      <el-table-column :label="$t('pileHole.lat')" align="center" prop="lat" width="120"/>
      <el-table-column :label="$t('pileHole.pileHoleStatus')" align="center" width="120" prop="consStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pile_hole_status" :value="scope.row.consStatus"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('pileHole.startTime')" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('pileHole.endTime')" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('pileHole.desDepth')" align="center" prop="desDept"/>
      <el-table-column :label="$t('pileHole.pileRadius')" align="center" prop="diameter"/>
      <el-table-column :label="$t('common.updateTime')" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.createTime')" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.operation')" align="center" class-name="small-padding fixed-width"
                       width="150">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['cons:pileHoleInfo:edit']"
          >
            {{ $t('common.edit') }}
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['cons:pileHoleInfo:remove']"
          >{{ $t('common.delete') }}
          </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="$t('pileHole.pileHoleNo')" prop="holeNum">
          <el-input v-model="form.holeNum"
                    :placeholder="$t('common.typeInfo',{name:$t('pileHole.pileHoleNo')})"/>
        </el-form-item>
        <el-form-item label="dx" prop="deltaX">
          <el-input v-model="form.deltaX" :placeholder="$t('common.typeInfo',{name:'dx'})"/>
        </el-form-item>
        <el-form-item label="dy" prop="deltaY">
          <el-input v-model="form.deltaY" :placeholder="$t('common.typeInfo',{name:'dy'})"/>
        </el-form-item>
        <el-form-item :label="$t('pileHole.lng')" prop="lng">
          <el-input v-model="form.lng"
                    :placeholder="$t('common.typeInfo',{name:$t('pileHole.lng')})"/>
        </el-form-item>
        <el-form-item :label="$t('pileHole.lat')" prop="lat">
          <el-input v-model="form.lat"
                    :placeholder="$t('common.typeInfo',{name:$t('pileHole.lat')})"/>
        </el-form-item>
        <el-form-item :label="$t('pileHole.pileHoleStatus')" prop="consStatus">
          <el-select v-model="form.consStatus"
                     :placeholder="$t('common.select',{name:$t('pileHole.pileHoleStatus')})" clearable>
            <el-option
                v-for="dict in dict.type.pile_hole_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item :label="$t('pileHole.desDepth')" prop="desDept">
          <el-input v-model="form.desDept" :placeholder="$t('common.typeInfo',{name:$t('pileHole.desDepth')})"/>
        </el-form-item>
        <el-form-item :label="$t('pileHole.pileRadius')" prop="diameter">
          <el-input v-model="form.diameter" :placeholder="$t('common.typeInfo',{name:$t('pileHole.pileRadius')})"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{ $t('common.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
          ref="upload"
          :limit="1"
          accept=".dwg"
          :headers="upload.headers"
          :action="upload.url + '?updateSupport=' + upload.updateSupport"
          :disabled="upload.isUploading"
          :on-progress="handleFileUploadProgress"
          :on-success="handleFileSuccess"
          :auto-upload="false"
          drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">{{ $t("common.importModeMsg") }}</div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            {{ $t("common.importMessage") }}
          </div>
          <span>{{ $t("common.importFileType", {fileType: 'DWG'}) }}</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">{{ $t('common.confirm') }}</el-button>
        <el-button @click="upload.open = false">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addPileHoleInfo,
  delPileHoleInfo,
  getPileHoleInfo,
  listPileHoleInfo,
  updatePileHoleInfo
} from "@/api/cons/pileHoleInfo";
import {getToken} from "@/utils/auth";

export default {
  name: "PileHoleInfo",
  dicts: ['pile_hole_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 施工桩点信息表格数据
      pileHoleInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        holeNum: null,
        consStatus: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        holeNum: [
          {required: true, message: this.$t("common.required", {name: this.$t("pileHole.pileHoleNo")}), trigger: "blur"}
        ],
        deltaX: [
          {required: true, message: this.$t("common.required", {name: "dx"}), trigger: "blur"}
        ],
        deltaY: [
          {required: true, message: this.$t("common.required", {name: "dy"}), trigger: "blur"}
        ],
        lng: [
          {
            required: true,
            message: this.$t("common.required", {name: this.$t("pileHole.lng")}),
            trigger: "blur"
          }
        ],
        lat: [
          {
            required: true,
            message: this.$t("common.required", {name: this.$t("pileHole.lat")}),
            trigger: "blur"
          }
        ],
        consStatus: [
          {
            required: true,
            message: this.$t("common.required", {name: this.$t("pileHole.pileHoleStatus")}),
            trigger: "blur"
          }
        ],
      },
      upload: {
        title: this.$t("cons.pileHoleImportTitle"),
        open: false,
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/system/user/importData"
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleImport() {
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    /** 查询施工桩点信息列表 */
    getList() {
      this.loading = true;
      listPileHoleInfo(this.queryParams).then(response => {
        this.pileHoleInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        holeNum: null,
        deltaX: null,
        deltaY: null,
        lng: null,
        lat: null,
        consStatus: null,
        startTime: null,
        endTime: null,
        desDept: null,
        diameter: null,
        updateTime: null,
        createTime: null,
        createBy: null,
        updateBy: 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 = this.$t("common.addDialog", {name: this.$t("pileHole.title")});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPileHoleInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t("common.modifyDialog", {name: this.$t("pileHole.title")});
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePileHoleInfo(this.form).then(response => {
              this.$modal.msgSuccess(this.$t("tips.modifySuccess"));
              this.open = false;
              this.getList();
            });
          } else {
            addPileHoleInfo(this.form).then(response => {
              this.$modal.msgSuccess(this.$t("tips.addSuccess"));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t("tips.confirmDelete", {name: ids})).then(function () {
        return delPileHoleInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t("tips.modifySuccess"));
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cons/pileHoleInfo/export', {
        ...this.queryParams
      }, `pileHoleInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
