<template>
  <div v-loading.fullscreen.lock="fullscreenLoading" class="app-container" element-loading-text="正在生成计划，请稍候">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px" size="small">
      <el-form-item label="计划名称" label-width="120" prop="planName">
        <el-input
          v-model="queryParams.planName"
          clearable
          placeholder="请输入计划名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年" prop="planYear">
        <el-date-picker
          v-model="year"
          :clearable="false"
          format="yyyy"
          placeholder="请选择年份"
          type="year"
          @change="handleQuery">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="月" prop="planQuarter">
        <el-date-picker
          v-model="month"
          :clearable="false"
          format="MM"
          placeholder="选择月"
          popper-class="picker-date"
          type="month"
          @change="handleQuery">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select
          v-model="queryParams.status"
          clearable
          placeholder="请选择确认状态"
          @change="handleQuery">
          <el-option label="未确认" value="0"/>
          <el-option label="已确认" value="1"/>
        </el-select>
      </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="['patrol:plan:add']"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          @click="handleAdd"
        >新增
        </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-hasPermi="['patrol:plan:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['patrol:plan:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="planList" :height="clientHeight" border
              @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="计划名称" prop="planName">
        <template slot-scope="scope">
          <a style="text-decoration: underline" @click="toLink(scope.row.patrolType,scope.row.id)">{{
              scope.row.planName
            }}</a>
        </template>
      </el-table-column>
      <el-table-column align="center" label="月" prop="planQuarter"/>
      <el-table-column align="center" label="检测数量" prop="devNum" width="400">
        <template slot-scope="scope">
            <div style="display: flex;width: 100%">
                <el-progress :stroke-width="28" :text-inside="true"
                             :percentage="parseFloat((scope.row.checkedNum/scope.row.devNum*100).toFixed(2))" style="width: 80%;"
                             status="success"></el-progress>
                <span style="width: 20%;">{{ scope.row.checkedNum }}/{{ scope.row.devNum }}</span></div>
        </template>
      </el-table-column><!--
      <el-table-column align="center" label="已检测数量" prop="checkedNum"/>
      <el-table-column align="center" label="未检测数量" prop="uncheckedNum"/>-->
      <el-table-column align="center" label="截止日期" prop="deadline">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadline, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="确认人" prop="confirmed"/>
      <el-table-column align="center" label="确认时间" prop="confirmDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-if="!scope.row.confirmed"
            v-hasPermi="['patrol:plan:edit']"
            icon="el-icon-check"
            size="mini"
            type="text"
            @click="handleConform(scope.row)"
          >确认
          </el-button>
          <el-tag v-else type="success">已确认</el-tag>
          <!--         <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['patrol:plan:edit']"
          >修改</el-button>-->
          <el-button
            v-if="scope.row.planName.indexOf('固定计划')<0&&scope.row.status==0"
            v-hasPermi="['patrol:plan:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除
          </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="500px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年" prop="planYear">
          <el-date-picker
            v-model="form.planYear"
            format="yyyy"
            placeholder="请选择年份"
            type="year"
            value-format="yyyy"
            @change="getMainList">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="月" prop="planQuarter">
          <el-date-picker
            v-model="form.planQuarter"
            format="MM"
            placeholder="请选择月份"
            popper-class="picker-date"
            type="month"
            value-format="MM">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检测计划" prop="mainId">
          <el-select v-model="form.mainId" placeholder="请选择检测设施" @change="getCycle">
            <el-option
              v-for="item in mainList"
              :key="item.id"
              :label="item.devName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="计划名称" prop="planName">
          <el-input v-model="form.planName" placeholder="请输入计划名称"/>
        </el-form-item>
        <el-form-item v-if="dfFlag" label="周期" prop="detectionFrequency">
          <el-radio v-model="form.detectionFrequency" label="1">上半月</el-radio>
          <el-radio v-model="form.detectionFrequency" label="2">下半月</el-radio>
        </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 {addPlan, conformPlan, delPlan, getPlan, listPlan, updatePlan} from "@/api/patrol/patrol/plan";
import {listAllMain} from "@/api/patrol/patrol/main";

export default {
  name: "Plan",
  data() {
    return {
      // 遮罩层
      loading: true,
      fullscreenLoading: false,
      // 选中数组
      ids: [],
      month: new Date(),
      year: new Date(),
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      dfFlag: false,
      // 显示搜索条件
      showSearch: false,
      // 页面高度
      clientHeight: 300,
      // 总条数
      total: 0,
      // 检查计划表格数据
      planList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        planName: null,
        planYear: null,
        planQuarter: null,
        confirmed: null,
        devNum: null,
        detectionFrequency: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        remarks: null,
        deptId: null
      },
      mainList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        detectionFrequency: [
          {required: true, message: "周期不能为空", trigger: "blur"}
        ],
        planQuarter: [
          {required: true, message: "月不能为空", trigger: "blur"}
        ],
        mainId: [
          {required: true, message: "检测计划不能为空", trigger: "blur"}
        ],
        planYear: [
          {required: true, message: "年度不能为空", trigger: "blur"}
        ],
        planName: [
          {required: true, message: "计划名称不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    //设置表格高度对应屏幕高度
    this.$nextTick(() => {
      this.clientHeight = document.body.clientHeight - 270
    });
    this.getList();
  },
  methods: {
    format(a,b) {
      return a+'/'+b;
    },
    toLink(patrolType, id) {
      switch (patrolType) {
        case '1':
          this.$router.push({
            path: '/patrol/record/fire/annihilator',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '2':
          this.$router.push({
            path: '/patrol/record/fire/cannon',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '3':
          this.$router.push({
            path: '/patrol/record/fire/postFire',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '4':
          this.$router.push({
            path: '/patrol/record/fire/hoseBox',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '5':
          this.$router.push({
            path: '/patrol/record/fire/standpipe',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '6':
          this.$router.push({
            path: '/patrol/record/fire/sprinkler',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '7':
          this.$router.push({
            path: '/patrol/record/emergency/cabinet',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '8':
          this.$router.push({
            path: '/patrol/record/fire/alarm',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '9':
          this.$router.push({
            path: '/patrol/record/fire/shield',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '10':
          this.$router.push({
            path: '/patrol/record/emergency/paga',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '11':
          this.$router.push({
            path: '/patrol/record/emergency/cctv',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '12':
          this.$router.push({
            path: '/patrol/record/emergency/eyewash',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '13':
          this.$router.push({
            path: '/patrol/record/fire/sandbox',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '14':
          this.$router.push({
            path: '/patrol/record/fire/door',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '15':
          this.$router.push({
            path: '/patrol/record/fire/weathervane',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '16':
          this.$router.push({
            path: '/patrol/record/fire/curtain',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '17':
          this.$router.push({
            path: '/patrol/record/emergency/cofferdam',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '18':
          this.$router.push({
            path: '/patrol/record/commonuse/tester',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '19':
          this.$router.push({
            path: '/patrol/record/commonuse/earplug',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '20':
          this.$router.push({
            path: '/patrol/record/commonuse/movement',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '21':
          this.$router.push({
            path: '/patrol/record/commonuse/bicycle',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '22':
          this.$router.push({
            path: '/patrol/record/commonuse/cover',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '23':
          this.$router.push({
            path: '/patrol/record/commonuse/walkitTalkie',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '24':
          this.$router.push({
            path: '/patrol/record/commonuse/safetyBelt',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '25':
          this.$router.push({
            path: '/patrol/record/commonuse/downspout',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '26':
          this.$router.push({
            path: '/patrol/record/commonuse/pump',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
        case '27':
          this.$router.push({
            path: '/patrol/record/fire/aidkit',
            query: {month: this.month.getMonth(), year: this.year.getFullYear(), planId: id}
          });
          break;
      }
    },
    handleConform(row) {
      this.$confirm('是否确认检查计划已完成？').then(function () {
        return conformPlan(row);
      }).then(() => {
        this.getList();
        this.msgSuccess("确认成功");
      }).catch(() => {
      });
    },
    /** 查询检查计划列表 */
    getList() {
      this.loading = true;
      this.queryParams.planQuarter = this.month.getMonth() + 1;
      this.queryParams.planYear = this.year.getFullYear();
      listPlan(this.queryParams).then(response => {
        this.planList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getMainList(year) {
      console.log(year)
      if (!year) {
        year = new Date().getFullYear();
      }
      listAllMain({year: year}).then(response => {
        this.mainList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        planName: null,
        planYear: null,
        planQuarter: null,
        confirmed: null,
        devNum: null,
        detectionFrequency: '',
        delFlag: null,
        createrCode: null,
        createdate: null,
        updaterCode: null,
        updatedate: null,
        remarks: null,
        deptId: null,
        mainObj: {},
        mainId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.year = new Date();
      this.month = 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.getMainList();
      this.open = true;
      this.title = "添加临时检查计划";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPlan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改临时检查计划";
      });
    },
    getCycle() {
      this.mainList.forEach(item => {
        if (item.id === this.form.mainId) {
          this.form.mainObj = item
        }
      });
      if (this.form.mainObj.patrolType == "1") {
        this.dfFlag = true;
      } else {
        this.dfFlag = false;
      }
      let s = '';
      if (this.form.detectionFrequency != '') {
        s = this.form.detectionFrequency == 1 ? '-上半月-' : '-下半月-';
      }
      this.form.planName = this.form.planYear + '年-' + this.form.planQuarter + '月' + s + "-临时计划-" + this.form.mainObj.devName;
      this.form.patrolType = this.form.mainObj.patrolType;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePlan(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.fullscreenLoading = true;
            addPlan(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.fullscreenLoading = false;
              this.open = false;
              this.getList();
            }).catch(res => {
              this.fullscreenLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该检查计划？').then(function () {
        return delPlan(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download2('patrol/plan/export', {
        ...this.queryParams
      }, `plan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss">
.picker-date {
  .el-date-picker__header {
    span:nth-child(3) { /*第三个标签是span的标签，把它隐藏*/
      display: none;
    }

    button:nth-child(1) {
      display: none;
    }

    button:nth-child(5) {
      display: none;
    }
  }

}
</style>
