<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="被检查设施名称" label-width="120" prop="devName">
        <el-input
          v-model="queryParams.devName"
          clearable
          placeholder="请输入被检查设施名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!--      <el-form-item label="检查周期" prop="checkCycle">
              <el-select v-model="queryParams.checkCycle" placeholder="请选择检查周期"
                         @change="handleQuery">
                <el-option
                  v-for="dict in dict.type.inspection_cycle"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>-->
      <el-form-item label="年" prop="year">
        <el-date-picker
          v-model="year"
          :clearable="false"
          placeholder="请选择年份"
          type="year"
          @change="handleQuery">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检查责任人" label-width="120" prop="personLiable">
        <el-input
          v-model="queryParams.personLiable"
          clearable
          placeholder="请输入检查责任人"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @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
          v-hasPermi="['ps:patrol:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['ps:patrol:export']"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="mainList" :height="clientHeight" border
              @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="被检查设施名称" prop="devName" width="180"/>
      <el-table-column align="center" label="设施数量" prop="devNum" width="100"/>
      <el-table-column align="center" label="检查周期" prop="checkCycle" width="140">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.inspection_cycle" :value="scope.row.checkCycle"/>
        </template>
      </el-table-column>
      <el-table-column :label="'年度：'+queryParams.year" align="center" prop="year">
        <el-table-column align="center" label="一月" prop="jan" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.jan.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,0)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="二月" prop="feb" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.feb.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,1)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="三月" prop="mar" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.mar.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,2)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="四月" prop="apr" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.apr.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,3)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="五月" prop="may" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.may.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,4)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="六月" prop="jun" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.jun.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,5)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="七月" prop="jul" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.jul.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,6)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="八月" prop="aug" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.aug.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,7)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="九月" prop="sep" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.sep.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,8)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="十月" prop="oct" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.oct.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,9)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="十一月" prop="nov" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.nov.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,10)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" label="十二月" prop="dec" width="100">
          <template slot-scope="scope">
            <div v-for=" item in scope.row.dec.split(',')" class="monthStyle" @click="toLink(scope.row.devLink,11)">
              <dict-tag :options="dict.type.inspection_status" :value="item"/>
            </div>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column align="center" label="检查责任人" prop="personLiable" width="180"/>
      <el-table-column align="center" label="备注" prop="remarks" width="120"/>
      <el-table-column label="操作" align="center" width="140" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ps:patrol:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ps:patrol:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改消气防设施和器材检查计划总对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" append-to-body width="30%">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="检查设施" prop="patrolType">
          <el-select v-model="form.patrolType" clearable placeholder="请选择检查设施" @change="setDevType">
            <el-option
              v-for="dict in dict.type.ps_patrol_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设施数量" prop="devNum">
          <el-input v-model="form.devNum" placeholder="请输入设施数量"/>
        </el-form-item>
        <el-form-item label="检查周期" prop="checkCycle">
          <el-select v-model="form.checkCycle" placeholder="请选择检查周期">
            <el-option
              v-for="dict in dict.type.inspection_cycle"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年" prop="year">
          <el-date-picker
            v-model="form.year"
            type="year"
            value-format="yyyy"
            placeholder="请选择年份">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检查责任人" prop="personLiable">
          <el-input v-model="form.personLiable" 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">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addMain, delMain, getMain, listMain, updateMain} from "@/api/ps/patrol/main";

export default {
  name: "Main",
  dicts: ['inspection_cycle', 'inspection_status', 'ps_patrol_type'],
  data() {
    return {
      year: new Date(),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 页面高度
      clientHeight: 300,
      // 总条数
      total: 0,
      // 消气防设施和器材检查计划总表格数据
      mainList: [],
      cycleOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        devName: null,
        devNum: null,
        checkCycle: null,
        year: null,
        jan: null,
        feb: null,
        mar: null,
        apr: null,
        may: null,
        jun: null,
        jul: null,
        aug: null,
        sep: null,
        oct: null,
        nov: null,
        dec: null,
        personLiable: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        remarks: null,
        devLink: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    //设置表格高度对应屏幕高度
    this.$nextTick(() => {
      this.clientHeight = document.body.clientHeight - 270
    });
  },
  methods: {
    setDevType(value) {
      if (value)
        this.form.devName = this.dict.type.ps_patrol_type[value - 1].label;
      else
        this.form.devName = null;
      console.log(this.form.devName)
    },
    toLink(devLink, month) {
      if (devLink) {
        this.$router.push({path: devLink, query: {month: month, year: this.year.getFullYear()}});
      }
    },
    /** 查询消气防设施和器材检查计划总列表 */
    getList() {
      this.loading = true;
      this.queryParams.year = this.year.getFullYear();
      listMain(this.queryParams).then(response => {
        this.mainList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        devName: null,
        devNum: null,
        checkCycle: null,
        year: null,
        jan: null,
        feb: null,
        mar: null,
        apr: null,
        may: null,
        jun: null,
        jul: null,
        aug: null,
        sep: null,
        oct: null,
        nov: null,
        dec: null,
        personLiable: null,
        delFlag: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        remarks: null,
        devLink: null,
        deptId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.year = new Date();
      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.reset();
      this.open = true;
      this.title = "添加消气防设施和器材检查计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMain(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改消气防设施和器材检查计划";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMain(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMain(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认数据项？').then(function () {
        return delMain(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download2('patrol/main/export', {
        ...this.queryParams
      }, `main_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
<style>
.monthStyle {
  display: inline-block;
}

.monthStyle:hover {
  cursor: pointer;
}

</style>
