<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="行政区">
        <!-- <el-col :span="4"> -->
        <treeselect v-model="queryParams.xzqdm" :options="deptOptions" :normalizer="normalizer" placeholder="选择行政区"
          noOptionsText="暂无数据" noChildrenText="没有子选项" :value="selectedValues" @select="treeselecthange" />
        <!-- </el-col> -->
      </el-form-item>
      <el-form-item label="文件名称">
        <el-input v-model="queryParams.name" placeholder="请输入文件名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="数据日期">
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择开始日期" v-model="queryParams.beginTime" style="width: 100%"
            value-format="yyyy-MM-dd"></el-date-picker>
        </el-col>
        <el-col class="line" :span="2">-</el-col>
        <el-col :span="11">
          <el-date-picker type="date" placeholder="选择截止日期" v-model="queryParams.endTime" style="width: 100%"
            value-format="yyyy-MM-dd"></el-date-picker>
        </el-col>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery"
          v-hasPermi="['supervise:pcsj:list']">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          v-hasPermi="['supervise:pcsj:list']">重置</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-upload" @click="fileUploadHandle" size="mini"
          v-hasPermi="['supervise:pcsj:upload']">上传</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supervise:pcsj: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:pcsj: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:pcsj:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supervise:pcsj:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar> -->
    </el-row>

    <el-table v-loading="loading" :data="pcsjList">
      <!-- @selection-change="handleSelectionChange" -->
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <!-- <el-table-column label="序号" align="center" prop="index" /> -->
      <el-table-column label="序号" type="index" align="center">
        <!-- <template #default="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="文件名称" align="center" prop="name" />
      <el-table-column label="区县名称" align="center" prop="xzqmc" />
      <el-table-column label="图斑个数(个)" align="center" prop="spotsnumber" width="180">
        <!-- <template #default="scope">
          <span>{{ parseTime(scope.row.uploadtime, "{y}-{m}-{d}") }}</span>
        </template> -->
      </el-table-column>
      <el-table-column label="监测面积（亩）" align="center" prop="spotsarea" />
      <el-table-column label="文件大小(MB)" align="center" prop="spotssize">
        <template #default="scope">
          <!-- <span>{{
            scope.row.spotssize
              ? (scope.row.spotssize / 1024 / 1024).toFixed(2)
              : ""
          }}</span> -->
          <!-- <span>{{ scope.row.spotssize }}</span> -->
        </template>
      </el-table-column>
      <el-table-column label="上传时间" align="center" prop="readtime" />
      <el-table-column label="数据日期" align="center" prop="filetime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleBrowse(scope.row)"
            :disabled="scope.row.readstatus != 0" v-hasPermi="['supervise:pcsj:list']">浏览</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleDownload(scope.row)"
            v-hasPermi="['supervise:pcsj:list']">下载</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['supervise:pcsj: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="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="上传用户" prop="uploaduser">
          <el-input v-model="form.uploaduser" placeholder="请输入上传用户" />
        </el-form-item>
        <el-form-item label="上传时间" prop="uploadtime">
          <el-date-picker clearable v-model="form.uploadtime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择上传时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="文件位置" prop="filepath">
          <el-input v-model="form.filepath" placeholder="请输入文件位置" />
        </el-form-item>
        <el-form-item label="shp位置" prop="shppath">
          <el-input v-model="form.shppath" placeholder="请输入shp位置" />
        </el-form-item>
        <el-form-item label="文件服务发布地址" prop="proxypath">
          <el-input v-model="form.proxypath" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="解压文件地址" prop="unzippath">
          <el-input v-model="form.unzippath" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="是否入库" prop="storage">
          <el-input v-model="form.storage" placeholder="请输入是否入库" />
        </el-form-item>
        <el-form-item label="行政区划代码" prop="xzqhdm">
          <el-input v-model="form.xzqhdm" placeholder="请输入行政区划代码" />
        </el-form-item>
        <el-form-item label="图斑数量" prop="spotsnumber">
          <el-input v-model="form.spotsnumber" placeholder="请输入图斑数量" />
        </el-form-item>
        <el-form-item label="图斑面积" prop="spotsarea">
          <el-input v-model="form.spotsarea" placeholder="请输入图斑面积" />
        </el-form-item>
        <el-form-item label="图斑大小" prop="spotssize">
          <el-input v-model="form.spotssize" placeholder="请输入图斑大小" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" v-hasPermi="['supervise:pcsj:upload']" @click="submitForm">确 定</el-button>
        <el-button v-hasPermi="['supervise:pcsj:upload']" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="文件上传" :visible.sync="fileUploadDiv" width="400px" append-to-body>
      <el-upload class="upload-demo" drag :action="uploadFileUrl" :before-upload="handleBeforeUpload"
        :on-error="handleUploadError" :on-success="handleUploadSuccess" :limit="1" accept=".zip" :file-list="fileList"
        :show-file-list="true" multiple>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传zip文件</div>
      </el-upload>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPcsj,
  getPcsj,
  delPcsj,
  addPcsj,
  updatePcsj,
  tiffUpload,
  listDept,
} from "@/api/supervise/pcsj";
// import Treeselect from "@riophae/vue-treeselect";
// import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Pcsj",
  // components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 监管批次数据表格数据
      pcsjList: [
        // {
        //   name: "", //文件名称
        //   xzqdm: "", //区县名称
        //   spotsnumber: "", //图斑个数
        //   spotsarea: "", //监测面积（亩）
        //   spotssize: "", //文件大小
        //   filetime: "", //录入时间
        // },
      ],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: "", //文件名称
        xzqdm: undefined, //区县编码
        beginTime: "", //录入时间
        endTime: "", //处理时间
      },
      // 表单参数
      form: {
        name: "1",
        uploaduser: "1",
        uploadtime: "1",
        filepath: "1",
        shppath: "1",
        proxypath: "1",
        unzippath: "1",
        filetype: "1",
        storage: "1",
        status: "1",
        xzqhdm: "1",
        spotsnumber: "1",
        spotsarea: "1",
        spotssize: "1",
      },
      // 表单校验
      rules: {},
      fileList: [],
      fileUploadDiv: false, //文件上传弹窗
      loading2: null,
      uploadFileUrl: '',//process.env.VUE_APP_BASE_API + "/file/uploadSpotZip",
      // 部门树选项
      deptOptions: [],
      selectedValues: [],
    };
  },
  created() {
    this.getList();
    this.getDept();
  },
  computed: {
    watchDeptId() {
      return this.queryParams.xzqdm;
    },
  },
  watch: {
    watchDeptId(newVal) {
      // console.log(newVal,'newValnewVal');
    },
  },
  methods: {
    getDept() {
      // console.log(this.$store.state, '----')
      // 查询参数
      let queryParams = {
        deptId: '101010',//this.$store.state.user.deptId,
        status: undefined,
      };
      listDept(queryParams).then((res) => {
        this.deptOptions = res.data;
      });
    },
    treeselecthange(value) {
      this.selectedValues = value.district;
      this.queryParams.xzqdm = value.district;
    },
    handleChange() { },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.district,
        label: node.label,
        children: node.children,
      };
    },
    fileUploadHandle() {
      console.log(this.uploadFileUrl);
      this.uploadFileUrl = this.uploadFileUrl;
      this.fileList = [];
      this.fileUploadDiv = true;
    },
    handleBeforeUpload(file) {
      this.$modal.loading("正在上传文件，请稍候...");
      return true;
    },
    // 上传失败
    handleUploadError(err) {
      this.$modal.msgError("上传文件失败，请重试");
      this.$modal.closeLoading();
    },
    // 上传成功回调
    handleUploadSuccess(res, file) {
      console.log("handleUploadSuccess");
      this.fileList = [];
      this.fileUploadDiv = false;
      this.$modal.closeLoading();
      this.$modal.msgSuccess(res.message);
      this.getList();
    },
    handleDownload(row) {
      const zipfile = row.proxypath + ".zip";
      window.open(`${zipfile}`);
    },
    /** 查询监管批次数据列表 */
    getList() {
      this.loading = true;
      listPcsj(this.queryParams).then((response) => {
        this.pcsjList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        uploaduser: null,
        uploadtime: null,
        filepath: null,
        shppath: null,
        proxypath: null,
        unzippath: null,
        filetype: null,
        storage: null,
        status: null,
        xzqhdm: null,
        xzqmc: null,
        spotsnumber: null,
        spotsarea: null,
        spotssize: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      // 查询参数
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: "", //文件名称
        xzqdm: "", //区县名称
        beginTime: "", //录入时间
        endTime: "", //处理时间
      }),
        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 = "添加监管批次数据";
    },
    // 浏览按钮
    handleBrowse(row) {
      this.$router.push({ path: "/remote/gdjc", query: { id: row.id } });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPcsj(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) {
            updatePcsj(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPcsj(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      console.log(row)
      const ids = row.id || this.ids;
      const idname = row.name;
      this.$modal
        .confirm('是否确认删除监管批次数据名称为"' + idname + '"的数据项？')
        .then(function () {
          return delPcsj(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "supervise/pcsj/export",
        {
          ...this.queryParams,
        },
        `pcsj_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
::v-deep .el-form-item--medium .el-form-item__label {
  font-size: 11px;
}

::v-deep .vue-treeselect {
  display: table-caption !important;
  width: 200px;
}
</style>
