<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:160px" placeholder="请选择处理状态">
          <el-option value="0" label="未处理"></el-option>
          <el-option value="1" label="已处理:通过"></el-option>
          <el-option value="2" label="已处理：未通过"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="执行班组">
        <el-select
          v-model="queryParams.teamId"
          placeholder="请选择执行班组"
          size="small"
          style="width:160px"
        >
          <el-option
            v-for="dict in teamList"
            :key="dict.id"
            :label="dict.teamName"
            :value="dict.id"

          />
        </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" v-loading="loading2">
      <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">{{((collectList.checkRadio)*100).toFixed(2)}}%</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="workOrderName" show-overflow-tooltip />
      <el-table-column label="工单类型" align="center" prop="workOrderName"  />
      <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 == '0' ? 'color3' : scope.row.clzt == '1' ? 'color4' : 'color2'">{{scope.row.clzt == '0' ? "未处理" : scope.row.clzt == '1' ? "已处理:通过" : "已处理:未通过"}}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行班组" align="center" prop="teamName" show-overflow-tooltip />
      <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 v-if="scope.row.clzt == '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:admin:edit']"
          >处置</el-button>
          <el-button v-if="scope.row.clzt != '0' "
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:admin:edit']"
            disabled
          >处置</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"  placeholder="请输入工单名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="工单内容" prop="workOrderContent">
              <el-input v-model="form.workOrderContent" placeholder="请输入工单内容"/>
            </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" placeholder="请选择工单类型">
                <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="执行班组" prop="teamId">
              <el-select
                v-model="form.teamId"
                placeholder="请选择执行班组"
                size="small"
              >
                <el-option
                  v-for="dict in teamList"
                  :key="dict.id"
                  :label="dict.teamName"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" >
            <el-form-item label="所属单位" prop="companyCode">
              <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="rules2" 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-col :span="12">
            <el-form-item label="执行班组" prop="teamId">
              <el-select
                v-model="form.teamId"
                placeholder="请选择执行班组"
                size="small"
                :disabled="true"
              >
                <el-option
                  v-for="dict in teamList"
                  :key="dict.id"
                  :label="dict.teamName"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="formRow.clzt =='1' || formRow.clzt =='2'">
            <el-form-item label="处置内容" prop="clnr">
              <el-input  type="textarea" maxlength="50" show-word-limit :rows="5" v-model="form.clnr" placeholder="请填写处理内容" @input="onInput()" disabled/>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="formRow.clzt =='0'">
            <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-col :span="12" v-if="formData.type =='1'">
            <el-form-item label="误报" prop="isAlarm">
              <el-radio v-model="form.isAlarm" :label="false" v-if="formRow.clzt =='1' || formRow.clzt =='2'" disabled>否</el-radio>
              <el-radio v-model="form.isAlarm" :label="true" v-if="formRow.clzt =='1' || formRow.clzt =='2'" disabled>是</el-radio>
              <el-radio v-model="form.isAlarm" :label="false" v-if="formRow.clzt =='0'">否</el-radio>
              <el-radio v-model="form.isAlarm" :label="true" v-if="formRow.clzt =='0'">是</el-radio>
            </el-form-item>
          </el-col>
          <el-col :span="24" v-if="formData.type !='1'">
            <el-form-item label="审批" prop="clzt">
              <el-select
                v-model="form.clzt"
                placeholder="请选择审批状态"
                size="small"
                :disabled="formRow.clzt =='1' || formRow.clzt =='2'"
              >
              <el-option
                  v-for="dict in clztList"
                  :key="dict.id"
                  :label="dict.name"
                  :value="dict.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer" v-if="formRow.clzt == '0'">
        <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, workOrderOne, baCwInfoUpdate, maintianLogUpdate, tbAlarmId, tbAlarmDG, tbAlarmPL } from "@/api/business/OM/workMange";
import { getDevopsTeam } from "@/api/business/OM/MochaITOM/crew";
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,
        status:undefined,
      },
      // 日期范围
      dateRange: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workOrderName:[
          { required: true, message: "工单不能为空", trigger: "blur" }
        ],
        workOrderContent: [
          { required: true, message: "工单内容不能为空", trigger: "blur" }
        ],
        workOrderType: [
          { required: true, message: "工单类型不能为空", trigger: ["blur",'change'] }
        ],
        teamId: [
          { required: true, message: "执行班组不能为空", trigger: ["blur",'change'] }
        ],
        companyCode: [
          { required: true, message: "所属单位不能为空", trigger: ["blur",'change'] }
        ],
      },
      // 表单校验
      rules2: {
        clnr:[
          { required: true, message: "处置内容不能为空", trigger: "blur" }
        ],
        clzt: [
          { required: true, message: "审批内容不能为空", trigger: ["blur",'change'] }
        ],

      },
      //综合数据
      collectList:[],
      loading2:true,
      loading:true,
      //班组
      teamList:[],
      clztList:[
        {id:1,name:"已处理：通过"},
        {id:2,name:"已处理：未通过"},
      ],
      formData:[],
      piliang:false,
      formRow:{}
    };
  },
  created() {
    this.init()//初始化
    if(JSON.stringify(this.$route.query) !="{}"){ //跳转至工单查询
      this.formData = this.$route.query
      this.workOrderOne(this.$route.query.id,this.$route.query.type)
    }else{
      this.formData = {}
      this.getList();//初始化table
    }
  },
  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,
        status:undefined
      }
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    //班组
    getDevopsTeam(){
      getDevopsTeam(this.queryParams).then(response => {
        if(response.data.records.length>0){
          this.teamList = response.data.records;
        }
      })
    },
    /** 查询参数列表 */
    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;
      });
      this.getDevopsTeam()
    },
    /** 工单详情查询 */
    workOrderOne(id,type){
      this.getDevopsTeam()
      if(this.formData.type == "1"){
        getWorkOrder({id:id,size:1,current:1}).then(res=>{
          if(res.data.records.length == 0){
            alert("历史数据，未纳入匹配工单")
            this.adminList = []
            this.loading = false;
          }else{
            this.handleUpdate(res.data.records[0])
            this.adminList.push(res.data.records[0]);
            this.loading = false;
          }

        })
      }else{
        workOrderOne({"businessId":id,"type":type}).then(res=>{
          if(JSON.stringify(res.data) == "{}"){
            alert("历史数据，未纳入匹配工单")
            this.adminList = []
            this.loading = false;
          }else{
            this.handleUpdate(res.data)
            this.adminList.push(res.data);
            console.log(this.adminList)
            this.loading = false;
          }

        })
      }

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      if(row.isAlarm === true){
        row.isAlarm = true
      }else{
        row.isAlarm = false
      }
      this.title = "处置";
      this.form = JSON.parse(JSON.stringify(row))
      this.formRow = JSON.parse(JSON.stringify(row))
      console.log(this.formRow)
      //this.form.clzt = 1 //默认处置通过

      if(this.formData.type == "1"){
        this.form.workOrderName = "消防隐患"
        this.open2 = true;
      }else if(this.form.workOrderType == "1"){
        this.formData.type = "1"
        this.workOrderOne(row.id,1)
        this.open2 = true;
       }else if(this.form.workOrderType == "2"){
        this.formData.type = "2"
        this.form.clzt = 1
        this.open2 = true;
       }else if(this.form.workOrderType == "3"){
        this.formData.type = "3"
        this.form.clzt = 1
        this.open2 = true;
      }else{


      }


    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open1 = true;
      this.title = "新增";
    },
    /** 提交按钮 */
    submitForm(row) {
      console.log(this.form)
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.loading = true
          if (this.form.id != undefined) {
            let type = this.formData.type
            console.log(type)
            if(type=="1"){
              this.form.clzt = 1
              updateWorkOrder(this.form).then(response => {
                this.loading = false,
                this.open2 = false;
                this.getList();
              })

            }
            if(type=="2"){
              console.log(2,this.form)
              maintianLogUpdate(this.form).then(response => {
                 this.loading = false,
                this.msgSuccess("修改成功");
                this.open2 = false;
                this.getList();
              })
            }
            if(type=="3"){
              baCwInfoUpdate(this.form).then(response => {
                 this.loading = false,
                this.msgSuccess("修改成功");
                this.open2 = false;
                this.getList();
              })
            }
          }else{
            this.form.clzt = 1
            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
        setTimeout(() => {
          this.loading2 = false
        }, 500);
      });
    }
  }
};
</script>

