<template>
  <el-dialog title="法规项" append-to-body :visible.sync="visible" width="1600px">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="装置类别" prop="plantType">
        <el-select v-model="queryParams.plantType" @change="handleQuery" placeholder="请选择装置类别" clearable
                   size="small">
          <el-option
            v-for="dict in plantTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="密封点类型" prop="pointType" label-width="80">
        <el-select v-model="queryParams.pointType" @change="handleQuery" placeholder="请选择密封点类型" clearable
                   size="small">
          <el-option
            v-for="dict in pointOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="介质状态" prop="mediumType">
        <el-select v-model="queryParams.mediumType" @change="handleQuery" placeholder="请选择介质状态" clearable
                   size="small"
                   style="width: 100%">
          <el-option
            v-for="dict in mediumTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</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-plus"
          size="mini"
          @click="handleAdd"
          v-if="row.status!=1"
          v-hasPermi="['check:lawitems: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-if="row.status!=1"
          v-hasPermi="['check:lawitems: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-if="row.status!=1"
          v-hasPermi="['check:lawitems: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="['check:lawitems:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="lawitemsList" @selection-change="handleSelectionChange" :height="clientHeight"
              border>
      <el-table-column type="selection" width="55" align="center" fixed="left"/>
      <el-table-column label="装置类型" align="center" prop="plantType" :show-overflow-tooltip="true" width="130"
                       :formatter="plantTypeFormat"/>
      <el-table-column label="密封点类型" align="center" prop="pointType" :show-overflow-tooltip="true" width="130"
                       :formatter="pointFormat"/>
      <el-table-column label="介质状态" align="center" prop="mediumType" :show-overflow-tooltip="true" width="130"
                       :formatter="mediumTypeFormat"/>
      <el-table-column label="一般泄露标准(ppm)" align="center" prop="general" :show-overflow-tooltip="true"
                       width="140"/>
      <el-table-column label="严重泄漏标准(ppm)" align="center" prop="serious" :show-overflow-tooltip="true"
                       width="140"/>
      <el-table-column label="监测频次" align="center" prop="detectionFrequency" :show-overflow-tooltip="true"
                       width="130" :formatter="detectionFormat"/>
      <el-table-column label="首次维修时限(天)" align="center" prop="stratFix" :show-overflow-tooltip="true"
                       width="130"/>
      <el-table-column label="最终维修时限(天)" align="center" prop="endFix" :show-overflow-tooltip="true" width="130"/>
      <el-table-column label="备注" align="center" prop="remarks" :show-overflow-tooltip="true" width="130"/>
      <el-table-column label="维护人" align="center" prop="updater" :show-overflow-tooltip="true" width="130"/>
      <el-table-column label="维护时间" align="center" prop="updatedate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="130" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['check:lawitems:edit']"
            :disabled="row.status==1"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            :disabled="row.status==1"
            v-hasPermi="['check:lawitems: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="装置类别" prop="plantType">
          <el-select v-model="form.plantType" :disabled="edit" placeholder="请选择装置类别" clearable size="small" style="width: 100%">
            <el-option
              v-for="dict in plantTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="密封点类型" prop="pointType">
          <el-select v-model="form.pointType" :disabled="edit" placeholder="请选择密封点类型" clearable size="small" style="width: 100%">
            <el-option
              v-for="dict in pointOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="介质状态" prop="mediumType">
          <el-select v-model="form.mediumType" :disabled="edit" placeholder="请选择介质状态" clearable size="small" style="width: 100%">
            <el-option
              v-for="dict in mediumTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="一般泄露标准(ppm)" prop="general">
          <el-input v-model="form.general" placeholder="请输入一般泄露标准"/>
        </el-form-item>
        <el-form-item label="严重泄漏标准(ppm)" prop="serious">
          <el-input v-model="form.serious" placeholder="请输入严重泄漏标准"/>
        </el-form-item>
        <el-form-item label="检测频次" prop="detectionFrequency">
          <el-select v-model="form.detectionFrequency" placeholder="请选择检测频次" clearable size="small"
                     style="width: 100%">
            <el-option
              v-for="dict in detectionFrequencyOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="首次维修时限(天)" prop="stratFix">
          <el-input v-model="form.stratFix" placeholder="请输入首次维修时限"/>
        </el-form-item>
        <el-form-item label="最终维修时限(天)" prop="endFix">
          <el-input v-model="form.endFix" placeholder="请输入最终维修时限"/>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="disabledBotton">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import {listLawitems, getLawitems, delLawitems, addLawitems, updateLawitems} from "@/api/check/lawitems";

export default {
  name: "Lawitems",
  data() {
    return {
      disabledBotton: false,
      plantTypeOptions: [],
      pointOptions: [],
      mediumTypeOptions: [],
      detectionFrequencyOptions: [],
      visible: false,
      // 页面高度
      clientHeight: 600,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      row: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 法规项表格数据
      lawitemsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      edit: false,
      // 查询参数
      queryParams: {
        lawId: null,
        pageNum: 1,
        pageSize: 10,
        plantType: null,
        pointType: null,
        mediumType: null,
        general: null,
        serious: null,
        detectionFrequency: null,
        stratFix: null,
        endFix: null,
        remarks: null,
        deptId: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        plantType: [
          {required: true, message: '请选择装置类型', trigger: "blur"}
        ],
        pointType: [
          {required: true, message: '请选择密封点类型', trigger: "blur"}
        ],
        mediumType: [
          {required: true, message: '请选择介质状态', trigger: "blur"}
        ],
      }
    };
  },
  created() {
  },
  methods: {
    init(row) {
      this.lawitemsList = [];
      this.row = row
      this.visible = true
      this.queryParams.lawId = row.id
      this.getList();
      this.getDicts("plant_type").then(response => {
        this.plantTypeOptions = response.data;
      });
      this.getDicts("point_type").then(response => {
        this.pointOptions = response.data;
      });
      this.getDicts("medium_type").then(response => {
        this.mediumTypeOptions = response.data;
      });
      this.getDicts("detection_frequency").then(response => {
        this.detectionFrequencyOptions = response.data;
      });
    },
    plantTypeFormat(row, column) {
      return this.selectDictLabel(this.plantTypeOptions, row.plantType);
    },
    detectionFormat(row, column) {
      return this.selectDictLabel(this.detectionFrequencyOptions, row.detectionFrequency);
    },
    pointFormat(row, column) {
      return this.selectDictLabel(this.pointOptions, row.pointType);
    },
    mediumTypeFormat(row, column) {
      return this.selectDictLabel(this.mediumTypeOptions, row.mediumType);
    },
    /** 查询法规项列表 */
    getList() {
      this.loading = true;
      listLawitems(this.queryParams).then(response => {
        this.lawitemsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        plantType: null,
        pointType: null,
        mediumType: null,
        general: null,
        serious: null,
        detectionFrequency: null,
        stratFix: null,
        endFix: null,
        remarks: null,
        deptId: null,
        delFlag: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        lawId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.disabledBotton = false
      this.reset();
      this.open = true;
      this.edit = false;
      this.title = "添加法规项";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.disabledBotton = false
      this.reset();
      this.edit = true;
      const id = row.id || this.ids
      getLawitems(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改法规项";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.disabledBotton = true
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.lawId = this.row.id
          if (this.form.id != null) {
            updateLawitems(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLawitems(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除法规项编号为"' + ids + '"的数据项？').then(function () {
        return delLawitems(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('check/lawitems/export', {
        ...this.queryParams
      }, `lawitems_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
