<template>
  <div class="app-container">
    <el-form v-model="queryParams" :inline="true">
      <el-form-item :label="$t('patrolpoint.PointCode')">
        <el-input
          v-model="queryParams.patrolpointCode"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('patrolpoint.PointCode')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('patrolpoint.PointName')">
        <el-input
          v-model="queryParams.patrolpointName"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('patrolpoint.PointName')
          "
        ></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">{{
          $t("share.delete")
        }}</el-button>
      </el-col> -->
    </el-row>
    <el-table :data="tableData" style="width: 100%; margin-bottom: 20px">
      <el-table-column
        type="index"
        :label="$t('patrolpoint.index')"
        width="55"
        align="center"
      />
      <el-table-column
        prop="patrolpointCode"
        :label="$t('patrolpoint.PointCode')"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="patrolpointName"
        :label="$t('patrolpoint.PointName')"
        align="center"
      >
      </el-table-column>
      <el-table-column
        :label="$t('patrolpoint.IntelligentRecognitionType')"
        align="center"
      >
        <template slot-scope="scope">
          <span @click="tableTyle(scope.row)" class="intelligenceType">
            {{
              scope.row.algSubtypeName
                ? scope.row.algSubtypeName
                : "无法设置阈值"
            }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        prop="patrolContent"
        :label="$t('patrolpoint.InspectionContent')"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="purpose"
        :label="$t('patrolpoint.purpose')"
        align="center"
      >
      </el-table-column>

      <el-table-column :label="$t('share.operate')" width="180" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleEdit(scope.row.patrolpointId)"
            >{{ $t("share.edit") }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row.patrolpointId)"
            >{{ $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"
    >
      <el-form :model="dialogForm" :rules="rules" ref="ruleForm">
        <el-form-item :label="$t('patrolpoint.PointCode')">
          <el-input
            v-model="dialogForm.patrolpointCode"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('patrolpoint.PointCode')
            "
          ></el-input>
        </el-form-item>
        <el-form-item
          :label="$t('patrolpoint.PointName')"
          prop="patrolpointName"
        >
          <el-input
            v-model="dialogForm.patrolpointName"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('patrolpoint.PointName')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patrolpoint.MainDeviceTypeID')">
          <el-input
            v-model="dialogForm.devicetypeId"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('patrolpoint.MainDeviceTypeID')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patrolpoint.MainDeviceID')">
          <el-input
            v-model="dialogForm.mainDeviceId"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('patrolpoint.MainDeviceID')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patrolpoint.deviceID')">
          <el-input
            v-model="dialogForm.deviceId"
            :placeholder="$t('patrolpoint.deviceIDMsg')"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patrolpoint.InspectionContent')">
          <Editor
            v-model="dialogForm.patrolContent"
            :height="200"
            style="margin-top: 30px"
          />
        </el-form-item>

        <el-form-item :label="$t('patrolpoint.purpose')">
          <el-input
            v-model="dialogForm.purpose"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('patrolpoint.purpose')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patrolpoint.DeleteFlag')">
          <el-input
            v-model="dialogForm.deleteFlag"
            :placeholder="$t('patrolpoint.DeleteFlagMsg')"
          ></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="typeTitle"
      :visible.sync="typedialogVisible"
      width="50%"
      :before-close="typehandleClose"
    >
      <el-table
        :data="dialogTable"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        row-key="id"
        height="500px"
        @selection-change="handleSelectionChangeDialog"
        ref="multipleTable"
        default-expand-all
        border
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column
          type="index"
          :label="$t('patrolpoint.index')"
          width="55"
          align="center"
        />
        <el-table-column :label="$t('patrolpoint.AlgorithmName')">
          <template slot-scope="scope">
            {{
              scope.row.algSubtypeName
                ? scope.row.algSubtypeName
                : scope.row.algName
            }}
          </template>
        </el-table-column>

        <el-table-column
          :label="$t('patrolpoint.ThresholdUpperLimit')"
          align="center"
          prop="upperValue"
          :render-header="addRedStar"
        >
          <template slot-scope="scope" v-if="scope.row.upperFlag == 1">
            <el-input v-model="scope.row.upperValue" />
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('patrolpoint.LowerThresholdLimit')"
          align="center"
          :render-header="addRedStar"
          prop="lowerValue"
        >
          <template slot-scope="scope" v-if="scope.row.lowerFlag == 1">
            <el-input v-model="scope.row.lowerValue" />
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="typehandleClose">{{ $t("share.cancel") }}</el-button>
        <el-button type="primary" @click="dialogTableOk">{{
          $t("share.confirm")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
// import Editor from "@/components/Editor";
import {
  getList,
  getItem,
  postAdd,
  postRemove,
  edit,
  algList,
  subtypeList,
} from "@/api/basedata/patrolpointmnt/patrolpoint";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 0,
      dialogVisible: false,
      title: this.$t("patrolpoint.AddInspectionPoints"),
      typeTitle: this.$t("patrolpoint.SetIntelligentRecognitionType"),
      typedialogVisible: false,
      dialogForm: {},
      rules: {
        patrolpointName: [
          {
            required: true,
            message: "请输入点位名称",
            trigger: "blur",
          },
        ],
      },
      dialogTable: [],
      dialogTableSelect: [],
      tableItem: {},
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化
    init() {
      getList(this.queryParams).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
      });
    },
    // 查询
    handleQuery() {
      this.init();
    },
    // 重置
    resetQuery() {
      this.queryParams = {};
      this.init();
    },
    // 给表头加必填符号*
    addRedStar(h, { column }) {
      return [
        h("span", { style: "color: red" }, "*"),
        h("span", " " + column.label),
      ];
    },
    add() {
      this.dialogForm = {};
      this.type = 0;
      this.dialogVisible = true;
      this.title = this.$t("patrolpoint.AddInspectionPoints");
    },
    handleEdit(id) {
      this.type = 1;
      this.dialogVisible = true;
      this.title = this.$t("patrolpoint.EditInspectionPoints");
      getItem(id).then((res) => {
        if (res.code == 200) {
          this.dialogForm = res.data;
        }
      });
    },

    // 新增修改弹框确认
    submitForm(ruleForm) {
      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 {
            edit(this.dialogForm).then((res) => {
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: "编辑成功!",
                });
                this.dialogVisible = false;
                this.init();
              }
            });
          }
        }
      });
    },
    handleClose() {
      this.dialogVisible = false;
    },
    // 删除
    handleDelete(id) {
      this.$confirm(`确定要删除Id为${id}的数据吗？`, "删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          postRemove(id).then((res) => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.init();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 智能识别类型弹框
    tableTyle(row) {
      this.tableItem=[]
      this.type = 1;
      this.tableItem = row;
      this.tableItem.alarmThreshold = JSON.parse(row.alarmThreshold);
      subtypeList().then((res0) => {
        if (res0.code == 200) {
          algList().then((res1) => {
            if (res1.code == 200) {
              let list = res1.rows;
              list.forEach((item) => {
                item.children = [];
                item.id = `${item.algId}_${item.algId}`;
                res0.rows.forEach((i) => {
                  i.id = i.algSubtypeId;
                  if (item.algId == i.algId && item.algName == i.algName) {
                    item.children.push(i);
                  }
                });
              });
              this.dialogTable = list;
              this.typedialogVisible = true;
              this.$nextTick(() => {
                if (row.alarmThreshold) {
                  let data = row.alarmThreshold;
                  let idArr = [];
                  data.forEach((item) => {
                    idArr.push(item.id.split("_")[1]);
                  });
                  console.log(idArr,111)
                  idArr.forEach((id) => {
                    let show = res0.rows.find(
                      (item) => item.algSubtypeId == id
                    );
                    if (show) {
                      console.log(show,2222)
                      this.$refs.multipleTable.toggleRowSelection(show, true);
                    }
                  });
                }
              });
            }
          });
        }
      });
    },
    // 智能识别类型树结构处理
    rowKeyFunc(row) {
      if (row.algSubtypeId) {
        return row.algSubtypeId;
      } else {
        return row.algId;
      }
    },
    // 智能识别多选
    handleSelectionChangeDialog(val) {
      this.dialogTableSelect = [];
      console.log(val);
      val.forEach((item) => {
        let data = {
          id: `sub_${item.algSubtypeId}`,
          code: item.algSubtypeCode || "",
          upperValue: item.upperValue || "",
          lowerValue: item.lowerValue || "",
        };
        this.dialogTableSelect.push(data);
      });
    },
    dialogTableOk() {
      this.tableItem.alarmThreshold = JSON.stringify(this.dialogTableSelect);
      edit(this.tableItem).then((res) => {
        if (res.code == 200) {
          this.$message({
            type: "success",
            message: "编辑成功",
          });
          this.typedialogVisible = false;
          this.init();
        }
      });
    },
    // 智能识别关闭
    typehandleClose() {
      this.typedialogVisible = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.intelligenceType {
  cursor: pointer;
  color: #409eff;
}
</style>
