<template>
  <div class="app-container">
    <TextSearch/>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="日期" prop="checkDateM">
        <el-date-picker
          v-model="queryParams.checkDateM"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择日期"
          @change="handleQuery">
        </el-date-picker>
      </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>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :search="false"></right-toolbar>
    </el-form>

    <table class="patrolTable" style="width: 100%; margin-bottom: 30px">
      <tr>
        <td rowspan="3" colspan="2">漏点巡检记录表</td>
        <td>日期</td>
        <td colspan="2">{{ parseTime(pointPatrol.checkDateM, '{y}年{m}月{d}日') }}</td>
        <td colspan="2">{{ parseTime(pointPatrol.checkDateN, '{y}年{m}月{d}日') }}</td>
      </tr>
      <tr>
        <td>班组</td>
        <td colspan="2">
          <span v-if="!isEdit">{{ pointPatrol.teamM }}班</span>
          <span v-else><el-input v-model="pointPatrol.teamM" placeholder="请输入当前班组"
                                 style="width: 150px"/> 班</span>
        </td>
        <td colspan="2">
          <span v-if="!isEdit">{{ pointPatrol.teamN }}班</span>
          <span v-else><el-input v-model="pointPatrol.teamN" placeholder="请输入当前班组"
                                 style="width: 150px"/> 班</span>
        </td>
      </tr>
      <tr>
        <td>时间</td>
        <td colspan="2">早班一次</td>
        <td colspan="2">夜班一次</td>
      </tr>
      <tr>
        <td>漏点编号</td>
        <td>漏点位置</td>
        <td>介质</td>
        <td>挂牌（是/否）</td>
        <td>泄露状态<br/>（未检出/轻微/严重）</td>
        <td>挂牌（是/否）</td>
        <td>泄露状态<br/>（未检出/轻微/严重）</td>
      </tr>
    </table>
    <div slot="footer" class="footer">
      <el-button type="primary" @click="submitForm">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import {
  addPointPatrol,
  delPointPatrol,
  getPointPatrol,
  listPointPatrol,
  updatePointPatrol
} from "@/api/asset/pointPatrol";
import PointRecord from "@/views/asset/pointRecord/index.vue";

export default {
  name: "PointPatrol",
  components: {PointRecord},
  data() {
    return {
      isEdit: false,
      // 页面高度
      clientHeight: 300,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 漏点巡检表格数据
      pointPatrolList: [],
      pointPatrol: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        teamM: null,
        checkDateM: new Date(),
        signM: null,
        teamN: null,
        checkDateN: null,
        signN: null,
        remarks: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    //设置表格高度对应屏幕高度
    this.$nextTick(() => {
      this.clientHeight = (document.body.clientHeight - 80) * 0.8
    });
  },
  methods: {
    /** 查询漏点巡检列表 */
    getList() {
      this.loading = true;
      if (this.queryParams.checkDateM==null) {
        this.queryParams.checkDateM = new Date();
      }
      listPointPatrol(this.queryParams).then(response => {
        this.pointPatrolList = response.rows;
        if (this.pointPatrolList.length > 0) {
          this.pointPatrol = this.pointPatrolList[0]
          this.isEdit = false
        } else {
          this.isEdit = true
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        teamM: null,
        checkDateM: null,
        signM: null,
        teamN: null,
        checkDateN: null,
        signN: null,
        remarks: null,
        delFlag: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        deptId: 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.reset();
      this.open = true;
      this.title = "添加漏点巡检";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPointPatrol(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改漏点巡检";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.isEdit = false
      this.pointPatrol.checkDateM=this.queryParams.checkDateM;
      if (this.form.id != null) {
        updatePointPatrol(this.pointPatrol).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.getList();
        });
      } else {
        addPointPatrol(this.pointPatrol).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除漏点巡检编号为"' + ids + '"的数据项？').then(function () {
        return delPointPatrol(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('asset/pointPatrol/export', {
        ...this.queryParams
      }, `pointPatrol_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>

<style scoped lang="scss">
/* 表格边框 */
table {
  border-collapse: collapse;
}

table td {
  border: 1px #b4b4b4 solid;
  padding: 10px;
  text-align: center;
}

</style>
