<template>
  <div class="app-container">
    <el-form v-model="queryParams" :inline="true">
      <el-form-item :label="$t('patroltype.ParentDeviceTypeID')">
        <el-input
          v-model="queryParams.parentId"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('patroltype.ParentDeviceTypeID')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('patroltype.InspectionTypeName')">
        <el-input
          v-model="queryParams.patrolTypeName"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('patroltype.InspectionTypeName')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('patroltype.InspectionTypeCode')">
        <el-input
          v-model="queryParams.patrolTypeCode"
          :placeholder="
            $t('share.PleaseEnter') + ' ' + $t('patroltype.InspectionTypeCode')
          "
        ></el-input>
      </el-form-item>
      <el-form-item :label="$t('patroltype.level')">
        <el-input
          v-model="queryParams.patrolTypeLevel"
          :placeholder="$t('share.PleaseEnter') + ' ' + $t('patroltype.level')"
        ></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('patroltype.InspectionTypeID')"
        prop="patrolTypeId"
        align="center"
      />
      <el-table-column
        :label="$t('patroltype.ParentDeviceTypeID')"
        prop="parentId"
        align="center"
      />
      <el-table-column
        :label="$t('patroltype.InspectionTypeName')"
        prop="patrolTypeName"
        align="center"
      />
      <el-table-column
        :label="$t('patroltype.InspectionTypeCode')"
        prop="patrolTypeCode"
        align="center"
      />
      <el-table-column
        :label="$t('patroltype.level')"
        prop="patrolTypeLevel"
        align="center"
      />
      <el-table-column :label="$t('patroltype.notes')" align="center" prop="patrolTypeRemark"/>
      <el-table-column :label="$t('share.operate')" align="center">
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="handleEdit(scope.row.patrolTypeId)"
            >{{ $t("share.edit") }}</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="handleDelete(scope.row.patrolTypeId)"
            >{{ $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 v-model="dialogForm">
        <el-form-item :label="$t('patroltype.ParentDeviceTypeID')">
          <el-input
            v-model="dialogForm.parentId"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('patroltype.ParentDeviceTypeID')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patroltype.InspectionTypeName')">
          <el-input
            v-model="dialogForm.patrolTypeName"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('patroltype.InspectionTypeName')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patroltype.InspectionTypeCode')">
          <el-input
            v-model="dialogForm.patrolTypeCode"
            :placeholder="
              $t('share.PleaseEnter') +
              ' ' +
              $t('patroltype.InspectionTypeCode')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patroltype.DeleteFlag')">
          <el-input
            v-model="dialogForm.delFlag"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('patroltype.DeleteFlag')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patroltype.level')">
          <el-input
            v-model="dialogForm.patrolTypeLevel"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('patroltype.level')
            "
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('patroltype.notes')">
          <el-input
            v-model="dialogForm.patrolTypeRemark"
            :placeholder="
              $t('share.PleaseEnter') + ' ' + $t('patroltype.notes')
            "
            type="textarea"
          ></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">{{
          $t("share.confirm")
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getList,
  getItem,
  postAdd,
  postRemove,
  edit,
  // algList,
  // subtypeList,
} from "@/api/basedata/patrolpointmnt/patroltype";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      tableData: [],
      total: 0,
      dialogVisible: false,
      title: this.$t("patroltype.AddPresetInspectionPoints"),
      dialogForm: {},
      ids: [],
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    //初始化
    init() {
      getList(this.queryParams).then((res) => {
        if (res.code == 200) {
          this.tableData = res.rows;
          this.total = res.total;
        }
      });
    },
    // 查询
    handleQuery() {
      this.init();
    },
    // 重置
    resetQuery() {
      this.queryParams = {};
      this.init();
    },
    // 新增修改弹框确认
    submitForm() {
      console.log(111);
      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();
          }
        });
      }
    },
    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;
        }
      });
    },
    add() {
      this.dialogForm = {};
      this.type = 0;
      this.dialogVisible = true;
      this.title = this.$t("patroltype.AddPresetInspectionPoints");
    },
   
    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: "已取消删除",
          });
        });
    },
    // 表格多选
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.patrolTypeId);
    },
    // 多选删除
    handleDeleteAll() {
      let ids = this.ids.toString();
      this.handleDelete(ids);
    },
     /** 导出按钮操作 */
    handleExport() {
      this.download('/basedata/patrolpointmnt/patroltype/export', {
        ...this.queryParams
      }, `patroltype_${new Date().getTime()}.xlsx`)
    }
  },
};
</script>

<style scoped></style>
