<template>
  <div class="app-container">
    <el-row :gutter="10">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="areaName" placeholder="请输入服务区名称" clearable size="small" prefix-icon="el-icon-search"
            style="margin-bottom: 20px" />
        </div>
        <div class="head-container">
          <el-tree ref="tree" :data="areaOptions" default-expand-all :expand-on-click-node="false" :filter-node-method="filterNode"
            node-key="id" highlight-current @node-click="handleNodeClick" />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
          label-width="68px">
          <el-form-item label="任务代码" prop="taskCode">
            <el-input v-model="queryParams.taskCode" placeholder="请输入任务代码" clearable
              @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="结果状态" prop="resultStatus">
            <el-select v-model="queryParams.resultStatus" placeholder="请选择结果状态" clearable>
              <el-option v-for="dict in dict.type.inspection_result" :key="dict.value" :label="dict.label"
                :value="dict.value" />
            </el-select>
          </el-form-item>
          <el-form-item label="提交人" prop="submitter">
            <el-input v-model="queryParams.submitter" placeholder="请输入提交人" clearable
              @keyup.enter.native="handleQuery" />
          </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-hasPermi="['ems:inspection-report: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-hasPermi="['ems:inspection-report: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-hasPermi="['ems:inspection-report: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="['ems:inspection-report:export']">导出
            </el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="inspectionReportList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="任务代码" align="left" prop="taskCode" />
          <el-table-column label="结果状态" align="center" prop="resultStatus">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.inspection_result" :value="scope.row.resultStatus" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="结果描述" align="center" prop="resultMsg" /> -->
          <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.finishTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="提交时间" align="center" prop="subTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.subTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="提交人" align="center" prop="submitter" />
          <el-table-column label="操作" align="center" 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="['ems:inspection-report:edit']">修改
              </el-button>

              <el-button size="mini" type="text" icon="el-icon-delete" class="deleteBtn"
                @click="handleDelete(scope.row)" v-hasPermi="['ems:inspection-report: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-col>
    </el-row>

    <!-- 添加或修改巡检报告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务代码" prop="taskCode">
          <el-input v-model="form.taskCode" placeholder="请输入任务代码" />
        </el-form-item>
        <el-form-item label="结果状态" prop="resultStatus">
          <el-radio-group v-model="form.resultStatus">
            <el-radio v-for="dict in dict.type.inspection_result" :key="dict.value" :label="parseInt(dict.value)">{{
              dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="结果描述">
          <editor v-model="form.resultMsg" :min-height="192" />
        </el-form-item>
        <el-form-item label="完成时间" prop="finishTime">
          <el-date-picker clearable v-model="form.finishTime" type="date" value-format="yyyy-MM-dd"
            placeholder="请选择完成时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提交时间" prop="subTime">
          <el-date-picker clearable v-model="form.subTime" type="date" value-format="yyyy-MM-dd" placeholder="请选择提交时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提交人" prop="submitter">
          <el-input v-model="form.submitter" 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 {
  addInspectionReport, delInspectionReport, getInspectionReport, listInspectionReport, updateInspectionReport,
} from '@/api/task/inspectionReport';
import {Base64} from 'js-base64';
import {areaTreeSelect} from '@/api/basecfg/area'
export default {
  name: 'InspectionReport',
  dicts: ['inspection_result'],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检报告表格数据
      inspectionReportList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskCode: null,
        resultStatus: null,
        submitter: null,
        areaCode:null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskCode: [
          {
            required: true,
            message: '任务代码不能为空',
            trigger: 'blur',
          },
        ],
        resultStatus: [
          {
            required: true,
            message: '结果状态不能为空',
            trigger: 'change',
          },
        ],
      },
      areaName: undefined,
      areaOptions: [],
    };
  },
  watch: {
     // 根据名称筛选区域树
     areaName (val) {
      this.$refs.tree.filter(val)
    }
  },
 async created () {
    await this.getAreaTreeByTag('Area')
    this.getList();
  },
  methods: {
    /** 查询巡检报告列表 */
    getList () {
      this.loading = true;
      listInspectionReport(this.queryParams).then(response => {
        this.inspectionReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询区域树结构 */
    async getAreaTreeByTag (tier) {
      await areaTreeSelect(tier).then(response => {
        this.areaOptions = [{
          id: '-1',
          label: '全部',
          children: []
        }].concat(response.data)
        this.queryParams.areaCode = '-1'
      })
    },
    // 筛选节点
    filterNode (value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    },
    handleNodeClick (data, node) {
      this.queryParams.areaCode = data.id
      this.getList()
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        taskCode: null,
        resultStatus: null,
        resultMsg: null,
        finishTime: null,
        subTime: null,
        submitter: 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;
      getInspectionReport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改巡检报告';
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.resultMsg) {
            this.form.resultMsg = Base64.encode(this.form.resultMsg);
          }
          if (this.form.id != null) {
            updateInspectionReport(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addInspectionReport(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 delInspectionReport(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('删除成功');
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('ems/inspectionReport/export', {
        ...this.queryParams,
      }, `inspectionReport_${new Date().getTime()}.xlsx`);
    },
  },
};
</script>
