<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="上报时间：">
         <el-date-picker
          style="margin-top:5px;width:340px"
          v-model="dateRange"
          size="small"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="处理状态">
        <el-select v-model.trim="queryParams.status"  size="small" style="width:120px">
          <el-option value="1" label="未处理"></el-option>
          <el-option value="2" label="已处理:通过"></el-option>
          <el-option value="3" label="已处理：未通过"></el-option>
        </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-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:admin:add']"
        >新增</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:admin:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="20">
      <el-col :span="2.4">
        <el-col class="event">
            事件总数<span class="color1 colorc">{{collectList.total}}</span>
        </el-col>
      </el-col>
      <el-col :span="2.4">
        <el-col class="event">
            已处理数<span class="color2 colorc">{{collectList.checkCount}}</span>
        </el-col>
      </el-col>
      <el-col :span="2.4" >
        <el-col class="event">
            未处理数<span class="color3 colorc">{{collectList.noCheckCount}}</span>
        </el-col>
      </el-col>
      <el-col :span="2.4">
        <el-col class="event">
            处置率<span class="color4 colorc " v-if="collectList.checkRadio">{{Number((collectList.checkRadio)*100).toFixed(2)}}%</span>
            <span class="color4 colorc " else-if="collectList.checkRadio">0</span>
        </el-col>
      </el-col>
    </el-row>
    <el-table v-loading="loading" :data="adminList" style="margin-top:20px;" height="calc(100vh - 34vh)">
      <el-table-column label="id" align="center" prop="id"  show-overflow-tooltip />
      <el-table-column label="单位名称" align="center" prop="companyName" show-overflow-tooltip />
      <el-table-column label="工单名称" align="center" prop="workOrderName" show-overflow-tooltip />
      <el-table-column label="工单内容" align="center" prop="workOrderContent" show-overflow-tooltip />
      <el-table-column label="工单类型" align="center" prop="workOrderType" show-overflow-tooltip>
        <template slot-scope="scope">
          <span :class="scope.row.workOrderType == 1 ? 'color3' : scope.row.workOrderType == 2 ? 'color4' : 'color2'">{{scope.row.workOrderType == 1 ? "告警" : scope.row.clzt == 2 ? "故障" : "其它"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="上报时间" align="center" prop="addTime" show-overflow-tooltip />
      <el-table-column label="处理状态" align="center" prop="clzt" >
        <template slot-scope="scope">
          <span :class="scope.row.clzt == 1 ? 'color3' : scope.row.clzt == 2 ? 'color4' : 'color2'">{{scope.row.clzt == 1 ? "未处理" : scope.row.clzt == 2 ? "已处理:通过" : "已处理:未通过"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="处置内容" align="center" prop="clnr" show-overflow-tooltip />
      <el-table-column label="处理人" align="center" prop="czPeople" show-overflow-tooltip/>
      <el-table-column label="处理时间" align="center" prop="czTime" show-overflow-tooltip />
      <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="['system:admin:edit']"
          >处置</el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:admin:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增对话框 -->
    <el-dialog :title="title" :visible.sync="open1" width="600px" append-to-body :before-close="cancel" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="工单名称" prop="workOrderName">
              <el-input v-model="form.workOrderName" />
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="工单内容" prop="workOrderContent">
              <el-input v-model="form.workOrderContent" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12" >
            <el-form-item label="工单类型" prop="workOrderType">
              <el-select v-model="form.workOrderType"  size="small">
                <el-option value="1" label="告警"></el-option>
                <el-option value="2" label="故障"></el-option>
                <el-option value="3" label="其它"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="所属单位">
              <el-select v-model="form.companyCode"  placeholder="请选择所属单位">
                <el-option
                  v-for="item in companyList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </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>
    <!-- 修改对话框 -->
    <el-dialog :title="title" :visible.sync="open2" width="600px" append-to-body :before-close="cancel" :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="工单名称" prop="workOrderName">
              <el-input v-model="form.workOrderName" :disabled="true" />
            </el-form-item>
          </el-col>
           <el-col :span="12">
            <el-form-item label="上报时间" prop="addTime">
              <el-input v-model="form.addTime" :disabled="true" />
            </el-form-item>
          </el-col>

        </el-row>
        <el-row :gutter="20">
          <el-col :span="18" >
            <el-form-item label="处置内容" prop="clnr">
              <el-input  type="textarea" maxlength="50" show-word-limit :rows="5" v-model="form.clnr" placeholder="请填写处理内容" @input="onInput()"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="审批" prop="isAlarm">
              <el-radio v-model="form.clzt" :label="2">已处理：通过</el-radio>
              <el-radio v-model="form.clzt" :label="3">已处理：未通过</el-radio>
            </el-form-item>
          </el-col>
        </el-row>

      </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>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />
    <!-- 导出-->
    <a href="" download="" id="xz" style="position: absolute;z-index: -1;opacity: 0;"></a>
  </div>
</template>
<script>
import { addWorkOrder, delWorkOrder, updateWorkOrder, getWorkOrder, exportWorkOrder, collectWorkOrder } from "@/api/business/OM/workMange";
import { listChannel } from "@/api/system/product";
import { BASEPATH } from "@/assets/js/common.js";
export default {
  name: "WorkOrder",
  data() {
    return {
      companyList:[],
      piliang:false,
      exportLoading:false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      adminList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open1: false,
      open2:false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        name: undefined,
        type:2
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {

      },
      //综合数据
      collectList:[

      ]
    };
  },
  created() {
    this.getList();//初始化table
    this.init()//初始化
    // this.company();//初始化单位选项
    // this.facilityType();//初始化类别选项
  },
  methods: {
    //强制el-input刷新
    onInput(){
        this.$forceUpdate();
    },
    // 取消按钮
    cancel() {
      this.open1 = false;
      this.open2 = false;
      this.reset();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        name:undefined,
        current:1,
        size:10,
        type:2
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      let data = this.addDateRange(this.queryParams,this.dateRange)
      if(JSON.stringify(data.params) == "{}"){
        delete data.params
      }else{
        data.endTime = data.params.endTime
        data.startTime = data.params.beginTime
      }
      getWorkOrder(data).then(response => {
        this.adminList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },


    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      if(row.isAlarm === true){
        row.isAlarm = true
      }else{
        row.isAlarm = false
      }
      this.title = "处置";
      this.form = JSON.parse(JSON.stringify(row))
      this.open2 = true;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open1 = true;
      this.title = "新增";
    },
    /** 提交按钮 */
    submitForm(row) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          if (this.form.id != undefined) {
            this.form.alarmStatus = 1
            updateWorkOrder(this.form).then(response => {
              this.loading = false,
              this.msgSuccess("修改成功");
              this.open2 = false;
              this.getList();
            }).catch(()=>{
              this.loading = false,
              this.open2 = false;
            })
          }else{
            // this.form.companyName = "东信"
            // this.form.companyCode = this.form.company.id
            // console.log(this.form)
            addWorkOrder(this.form).then(response => {
              this.loading = false,
              this.msgSuccess("修改成功");
              this.open1 = false;
              this.getList();
            }).catch(()=>{
              this.loading = false,
              this.open1 = false;
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWorkOrder(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },


    /** 导出按钮操作 */
    handleExport() {
      let queryParams = {...this.queryParams};
      delete queryParams.current
      delete queryParams.size
      this.exportLoading = true;
      exportWorkOrder(queryParams).then(response =>{
        this.exportLoading = false;
        document.getElementById("xz").href = BASEPATH() + response.data
        document.getElementById("xz").download = response.data
        document.getElementById("xz").click()
      })
    },
    init(){
      /* 公司列表 */
      listChannel(this.queryParams).then(response => {
        if(response.data.records.length>0){
          this.companyList = response.data.records.map(val =>{
            return {
              id:val.ownerId,
              name:val.ownerName,
              address:val.ownerAddress,
            }
          })
        }
      });
      /** 综合列表列表 */
        collectWorkOrder({}).then(response => {
          this.collectList = response.data
        });
    }
  }
};
</script>

<style>
.event{
  border:1px solid #ccc;
  padding:1.5vh 0;
  font-size:0.9vw;
}
.colorc{
  font-size:1.2vw;
  margin-left:4px;
}
.color1{
  color:#6F99FA!important;
}
.color2{
  color:#DE8C54!important
}
.color3{
  color:#FF5801!important
}
.color4{
  color:#37FF01!important
}
</style>
