<template>
  <div class="app-container">
    <customForm ref="formRef" :model="queryParams" :config="FormConfig">
      <template #time>
        <el-date-picker
          v-model="queryParams.time"
          type="daterange"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          style="width: 100%"
        />
      </template>
      <template #action>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </template>
    </customForm>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['dataentry:add']"
          >数据录入</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dataentry:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['dataentry:export']"
          >下载</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <tablePage
      class="tablePage"
      v-loading="loading"
      :cloumn="TableHeader"
      :table="{ total, data: dkdbList }"
      :indexed="false"
      :check="true"
      ref="tableDialogRef"
      @selectChange="handleSelectionChange"
      @currentChange="getList"
      v-model:pageNum="queryParams.pageNum"
      v-model:pageSize="queryParams.pageSize"
    >
      <template #action="{ row }">
        <el-button link type="primary" @click="handleBrowse(row)"
          >查看
        </el-button>
        <el-button
          v-if="row.status == '0'"
          link
          type="primary"
          @click="Issued(row)"
          >下发
        </el-button>
        <!-- <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dataentry:edit']"
            >修改</el-button
          > -->
      </template>
    </tablePage>

    <!-- 添加或修改地块基本信息（DKdb）对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="dkdbRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="数据内容：" prop="dataType">
          <el-radio-group v-model="form.dataType">
            <el-radio label="1" size="large">监测图斑数据</el-radio>
            <el-radio label="2" size="large">业务研判数据</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="选择数据：" prop="shppath">
          <fileUpload
            v-model="form.shppath"
            :limit="1"
            :fileSize="50"
            :fileType="['zip']"
            urltype="/v1"
          ></fileUpload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">上 传</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
  
  <script setup name="Dkdb">
import { listDkdb, adddb, issueDkdb } from "@/api/rsmonitoring/dataEntry";
const { proxy } = getCurrentInstance();
import { FormConfig, TableHeader } from "./config";
const route = useRoute();
const router = useRouter();

const dkdbList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: { dataType: "1", shppath: "" },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    dkbh: null,
    dkmj: null,
    xzqdm: null,
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询地块基本信息（DKdb）列表 */
function getList(pages) {
  loading.value = true;
  listDkdb(queryParams.value).then((response) => {
    dkdbList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    dataType: "1",
    shppath: "",
  };
  proxy.resetForm("dkdbRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "数据录入";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dkdbRef"].validate((valid) => {
    if (valid) {
      // if (form.value.id != null) {
      //   updateDkdb(form.value).then((response) => {
      //     proxy.$modal.msgSuccess("修改成功");
      //     open.value = false;
      //     getList();
      //   });
      // } else {
      adddb(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
      // }
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "rsmonitoring/dkdb/export",
    {
      ...queryParams.value,
    },
    `dkdb_${new Date().getTime()}.xlsx`
  );
}
function handleBrowse(row) {
  router.push({
    path: "/remote/gdjc",
    query: { id: row.id, form: "dataEntry", dataType: row.dataType },
  });
}

function Issued(row) {
  issueDkdb({ id: row.id }).then((res) => {
    proxy.$modal.msgSuccess("下发成功");
    this.getList();
  });
}
getList();
</script>
  