<template>
  <div class="app-container">
    <el-form v-model="queryParams" :inline="true">
      <el-form-item :label="$t('patrolpointpreset.PointCode')">
        <el-input
          v-model="queryParams.channelCode"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('patrolpointpreset.PointCode')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('patrolpointpreset.InspectionEquipmentID')">
        <el-input
          v-model="queryParams.eqpBookId"
          :placeholder="
            $t('share.PleaseEnter') +
            ' ' +
            $t('patrolpointpreset.InspectionEquipmentID')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('patrolpointpreset.ChannelNumber')">
        <el-input
          v-model="queryParams.channelId"
          :placeholder="
            $t('share.PleaseEnter') +
            ' ' +
            $t('patrolpointpreset.ChannelNumber')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('patrolpointpreset.PresetPointNumber')">
        <el-input
          v-model="queryParams.presetCode"
          :placeholder="
            $t('share.PleaseEnter') +
            ' ' +
            $t('patrolpointpreset.PresetPointNumber')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('patrolpointpreset.PresetPointName')">
        <el-input
          v-model="queryParams.presetName"
          :placeholder="
            $t('share.PleaseEnter') +
            ' ' +
            $t('patrolpointpreset.PresetPointName')
          "
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="handleQuery"
          size="mini"
          >{{ $t("share.search") }}</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery" size="mini">{{
          $t("share.reset")
        }}</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          plain
          size="mini"
          @click="add"
          >{{ $t("share.new") }}</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          plain
          size="mini"
          @click="handleEdit"
          >{{ $t("share.edit") }}</el-button
        >
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          plain
          size="mini"
          @click="handleDeleteAll"
          >{{ $t("share.delete") }}</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          plain
          size="mini"
          @click="handleExport"
          >{{ $t("share.export") }}</el-button
        >
      </el-col>
    </el-row>
    <el-table
      :data="tableData"
      style="width: 100%; margin-bottom: 20px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        :label="$t('patrolpointpreset.PointID')"
        prop="patrolDeviceName"
        align="center"
      />
      <el-table-column
        :label="$t('patrolpointpreset.PointCode')"
        prop="channelCode"
        align="center"
      />
      <el-table-column
        :label="$t('patrolpointpreset.InspectionEquipmentID')"
        prop="eqpBookId"
        align="center"
      />
      <el-table-column
        :label="$t('patrolpointpreset.ChannelNumber')"
        prop="channelId"
        align="center"
      />
      <el-table-column
        :label="$t('patrolpointpreset.PresetPointNumber')"
        prop="presetCode"
        align="center"
      />
      <el-table-column
        :label="$t('patrolpointpreset.PresetPointName')"
        prop="presetName"
        align="center"
      />
      <el-table-column :label="$t('share.operate')" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleEdit(scope.row.patrolPointPresetId)"
            >{{ $t("share.edit") }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row.patrolPointPresetId)"
            >{{ $t("share.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="init"
    />
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="30%"
      :before-close="handleClose"
      ref="ruleForm"
    >
      <el-form :model="dialogForm" :rules="rules" ref="ruleForm">
        <el-form-item :label="$t('patrolpointpreset.ParentPointID')">
          <span class="fatherID" @click="fatherIDFn">{{
            dialogForm.patrolPointId || fatherID
          }}</span>
          <i
            class="el-icon-close"
            style="color: red; cursor: pointer; margin-left: 5px"
          ></i>
        </el-form-item>
        <el-form-item :label="$t('patrolpointpreset.ParentPointCode')">
          <span>{{ dialogForm.patrolPointPresetId }}</span>
        </el-form-item>
        <el-form-item :label="$t('patrolpointpreset.ParentPointName')">
          <span>{{ dialogForm.patrolPointName }}</span>
        </el-form-item>
        <el-form-item :label="$t('patrolpointpreset.InspectionEquipmentID')">
          <el-input
            v-model="dialogForm.eqpBookId"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('patrolpointpreset.InspectionEquipmentID')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patrolpointpreset.ChannelNumber')">
          <el-input
            v-model="dialogForm.channelId"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('patrolpointpreset.ChannelNumber')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patrolpointpreset.PresetPointNumber')">
          <el-input
            v-model="dialogForm.presetCode"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('patrolpointpreset.PresetPointNumber')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patrolpointpreset.PresetPointName')">
          <el-input
            v-model="dialogForm.presetName"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('patrolpointpreset.PresetPointName')
            "
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ $t("share.cancel") }}</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">{{
          $t("share.confirm")
        }}</el-button>
      </span>
    </el-dialog>
    <el-dialog
      :title="tableTitle"
      :visible.sync="tableDialogVisible"
      width="60%"
      :before-close="tableDialogHandleClose"
      append-to-body
    >
      <el-form v-model="tableDialogForm" :inline="true">
        <el-form-item :label="$t('patrolpointpreset.PointCode')">
          <el-input
            v-model="tableDialogForm.patrolpointCode"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('patrolpointpreset.PointCode')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patrolpointpreset.PointName')">
          <el-input
            v-model="tableDialogForm.patrolpointName"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('patrolpointpreset.PointName')
            "
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="tableDialogHandleQuery"
            size="mini"
            >{{ $t("share.search") }}</el-button
          >
          <el-button
            icon="el-icon-refresh"
            @click="tableDialogResetQuery"
            size="mini"
            >{{ $t("share.reset") }}</el-button
          >
        </el-form-item>
      </el-form>
      <!-- :row-style="rowStyle" -->
      <el-table
        :data="dialogTableData"
        style="width: 100%; margin-bottom: 20px"
        height="500px"
        @row-click="changeCurrentRow"
      >
        <el-table-column type="index" width="55" align="center" />
        <el-table-column
          :label="$t('patrolpointpreset.PointCode')"
          prop="patrolpointCode"
          align="center"
        />
        <el-table-column
          :label="$t('patrolpointpreset.PointName')"
          prop="patrolpointName"
          align="center"
        />
        <el-table-column
          :label="$t('patrolpointpreset.IntelligentRecognitionType')"
          prop="algSubtypeName"
          align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.algSubtypeName || "无法设置阈值" }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('patrolpointpreset.InspectionContent')"
          prop="patrolContent"
          align="center"
        />
        <el-table-column
          :label="$t('patrolpointpreset.purpose')"
          prop="purpose"
          align="center"
        />
      </el-table>
      <pagination
        v-show="tableDialogFormTotal > 0"
        :total="tableDialogFormTotal"
        :page.sync="tableDialogForm.pageNum"
        :limit.sync="tableDialogForm.pageSize"
        @pagination="patrolpointList"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="tableDialogHandleClose()">{{
          $t("share.cancel")
        }}</el-button>
        <el-button type="primary" @click="tableDialogVisibleBtn()">{{
          $t("share.confirm")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  patrolpoint_list,
  postAdd,
  delPatrolpointpreset,
  getItem,
  updatePatrolpointpreset,
} from "@/api/basedata/patrolpointmnt/patrolpointpreset";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 10,
      dialogVisible: false,
      title: this.$t("patrolpointpreset.AddPresetInspectionPoints"),
      dialogForm: {},
      fatherID: "选择",
      tableTitle: this.$t(
        "patrolpointpreset.EditInspectionPointPresetPositions"
      ),
      tableDialogVisible: false,
      rules: {
        eqpbookId: [
          {
            required: true,
            message:
              this.$t("share.PleaseEnter") +
              " " +
              this.$t("patrolpointpreset.InspectionEquipmentID"),
            trigger: "blur",
          },
        ],
      },
      dialogTableData: [],
      deviceId: null, // 当前点击的行ID
      deviceItem: null,
      type: 0,
      tableDialogForm: {
        pageNum: 1,
        pageSize: 10,
      },
      tableDialogFormTotal: 10,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      getList(this.queryParams).then((res) => {
        console.log(this.queryParams);
        if (res.code == 200) {
          this.tableData = res.rows;
        }
      });
    },
    add() {
      this.dialogForm = {};
      this.dialogVisible = true;
      this.type = 0;
      this.title = this.$t("patrolpointpreset.AddPresetInspectionPoints");
    },
    handleEdit(id) {
      this.type = 1;
      this.dialogVisible = true;
      this.title = this.$t(
        "patrolpointpreset.EditInspectionPointPresetPositions"
      );
      getItem(id).then((res) => {
        console.log(res, 11111);
        if (res.code == 200) {
          this.dialogForm = res.data;
        }
      });
    },
    fatherIDFn() {
      this.tableDialogVisible = true;
      this.patrolpointList();
    },
    patrolpointList() {
      patrolpoint_list(this.tableDialogForm).then((res) => {
        if (res.code == 200) {
          this.dialogTableData = res.rows;
          this.tableDialogFormTotal = res.total;
        }
      });
    },
    tableDialogVisibleBtn() {
      this.dialogForm = {
        patrolPointName: this.deviceItem.patrolpointName,
        patrolPointPresetId: this.deviceItem.patrolpointCode,
        patrolPointId: this.deviceItem.patrolpointId,
      };
      //  patroldeviceCode: device.patroldeviceCode,
      //   eqpbookId: device.eqpbookId,
      //   patrolpointId: this.currentRow.patrolpointId,
      this.tableDialogVisible = false;
    },
    rowStyle({ row }) {
      // return
      if (this.deviceId === row.deviceId) {
        return {
          "background-color": "rgb(94, 180, 251)",
        }; // 返回选中行的样式
      }
      return {};
    },
    changeCurrentRow(row) {
      this.deviceId = row.deviceId; // 更新当前点击的行ID
      this.deviceItem = row;
    },
    // 新增
    submitForm(ruleForm) {
      // console.log(this.dialogForm, 111111111);
      // return;
      this.$refs[ruleForm].validate((v) => {
        if (v) {
          if (this.type == 0) {
            postAdd(this.dialogForm).then((res) => {
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: "添加成功",
                });
                this.dialogVisible = false;
                this.init();
              }
            });
          } else {
            // 修改
            updatePatrolpointpreset(this.dialogForm).then((res) => {
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: "修改成功",
                });
                this.dialogVisible = false;
                this.init();
              }
            });
          }
        }
      });
    },
    handleQuery() {
      this.init();
    },
    resetQuery() {
      let { pageNum, pageSize } = { ...this.queryParams };
      let data = {
        pageNum,
        pageSize,
      };
      this.queryParams = data;
      this.init();
    },
    handleClose() {
      this.dialogVisible = false;
    },
    tableDialogHandleQuery() {
      console.log(this.tableDialogForm);
      this.patrolpointList();
      // patrolpoint_list(this.tableDialogForm).then((res) => {
      //   if (res.code == 200) {
      //     this.dialogTableData = res.rows;
      //     this.tableDialogFormTotal = res.total;
      //   }
      // });
    },
    tableDialogResetQuery() {},
    tableDialogHandleClose() {
      this.tableDialogVisible = false;
    },

    handleExport() {
      this.download(
        "/basedata/patrolpointmnt/patrolpointpreset/export",
        {
          ...this.queryParams,
        },
        `patrolpointpreset_${new Date().getTime()}.xlsx`
      );
    },
    // 删除
    handleDelete(patrolPointPresetId) {
      this.$confirm(`确定要删除Id为${patrolPointPresetId}的数据吗？`, "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delPatrolpointpreset(patrolPointPresetId).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.init();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 表格多选
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.patrolPointPresetId);
    },
    // 多选删除
    handleDeleteAll() {
      let ids = this.ids.toString();
      this.handleDelete(ids);
    },
  },
};
</script>
 
<style lang="scss" scoped>
.fatherID {
  color: blue;
  cursor: pointer;
}
</style>
