<template>
  <div class="app-container scroll-auto">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
       <el-form-item label="月计划编号" prop="planCode" label-width="120">
           <el-input
             v-model="queryParams.planCode"
             placeholder="请输入月计划编号"
             clearable
           
           />
         </el-form-item>
       <el-form-item label="周计划编号" prop="planCodeW" label-width="120">
         <el-input
           v-model="queryParams.planCodeW"
           placeholder="请输周计划编号"
           clearable
         />
       </el-form-item>
         </el-form-item>
       
         </el-form-item>
         <el-form-item label="区县单位" prop="county">
           <el-input
             v-model="queryParams.county"
             placeholder="请输入区县单位"
             clearable
           />
         </el-form-item>
         <el-form-item label="供电所" prop="station">
           <el-input
             v-model="queryParams.station"
             placeholder="请输入所属供电所"
             clearable
           />
         </el-form-item>
        
         <el-form-item label="线路名称" prop="lineName">
           <el-input
             v-model="queryParams.lineName"
             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-col :span="1.5">
        <el-button
           type="warning"
           plain
           icon="el-icon-upload"
           size="mini"
        :disabled="maintenancePalan"
           @click="uploadMaintenanceFile()"
         >上传检修方案</el-button>
		 
		 <el-button
		   type="danger"
		   plain
		   icon="el-icon-upload"
		   size="mini"
		  :disabled="maintenancePalan"
		   @click="uploadApproveFile"
		 >上传审批单</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

     <el-table v-loading="loading" :data="processList" @selection-change="handleSelectionChange"
         :row-class-name="getRowClassName"
     >
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column label="月计划编号" align="center" prop="planCode"  width="150" fixed>
     	  <template slot-scope="scope">
     			<a @click="table_show(scope.row, scope.$index)" style="color: #00afff;cursor: pointer">{{scope.row.planCode}}</a>
     	  </template>
       </el-table-column>
	   <el-table-column label="周计划编号" align="center" prop="planCodeW"  width="150" fixed>
	    </el-table-column>
         <el-table-column label="停电设备数" align="center" prop="powerOutageDevices" fixed/>
         <el-table-column label="月计划停电时户数" align="center" prop="perHourHouseholdsM" fixed/>
         <el-table-column label="周计划停电时户数" align="center" prop="perHourHouseholdsW" fixed/>
         <el-table-column label="市州单位" align="center" prop="city" width="150" />
         <el-table-column label="区县单位" align="center" prop="county" width="180"/>
         <el-table-column label="供电服务站/所" align="center" prop="station" width="120" :show-overflow-tooltip="true"/>
         <el-table-column label="线路编号" align="center" prop="lineCode"  width="120"/>
         <el-table-column label="线路名称" align="center" prop="lineName" width="150" :show-overflow-tooltip="true"/>
       <el-table-column label="实际时户数" width="" align="center" prop="hourNum" />
       <el-table-column label="是否超目标" width="130"align="center">
     	  <template slot-scope="scope" >
     	  		<el-tag type="danger" effect="dark" v-if="scope.row.hourNum>0 && scope.row.hourNum>scope.row.perHourHouseholdsW">超目标</el-tag>
     	  		<el-tag type="success" effect="dark" v-else-if="scope.row.hourNum>0 && scope.row.hourNum<scope.row.perHourHouseholdsW">正常</el-tag>
     			<el-tag type="info" v-else>未知</el-tag>
     	  </template>
       </el-table-column>
         <el-table-column label="申报单位" align="center" prop="declarAgency"  width="150"/>
	 <el-table-column label="检修计划" align="center">
		 <el-table-column label="检修方案" align="center" prop="isMaintenance">
			  <template slot-scope="scope">
					<span v-if="scope.row.isMaintenance=='1'">待上传</span>
					<span type="info" effect="dark"  v-if="scope.row.isMaintenance=='0'">不需要</span>
					<span type="success" effect="dark"  v-if="scope.row.isMaintenance=='2'">已上传</span>
			  </template>
		   </el-table-column>
		  <el-table-column label="审批单" align="center" prop="isApprove">
			  <template slot-scope="scope">
					<span v-if="scope.row.isApprove=='1'">待上传</span>
					<span type="info" effect="dark"  v-if="scope.row.isApprove=='0'">不需要</span>
					<span type="success" effect="dark"  v-if="scope.row.isApprove=='2'">已上传</span>
			  </template>
		   </el-table-column>
	   </el-table-column>
	   <el-table-column label="考核金额" align="center">
	     <el-table-column
	       label="党政一把手"
	       prop="examiAmount[0]"
	       align="center"
	       :show-overflow-tooltip="true"
	     >
	     </el-table-column>
	     <el-table-column
	       label="分管副经理"
	       prop="examiAmount[1]"
	       align="center"
	       :show-overflow-tooltip="true"
	     >
	     </el-table-column>
	     <el-table-column
	       label="部室主任"
	       prop="examiAmount[2]"
	       align="center"
	       :show-overflow-tooltip="true"
	     />
	     <el-table-column
	       label="部室分管主任"
	       prop="examiAmount[3]"
	       align="center"
	       :show-overflow-tooltip="true"
	     >
	     </el-table-column>
	     <el-table-column
	       label="部室责任专责"
	       prop="examiAmount[4]"
	       align="center"
	       :show-overflow-tooltip="true"
	     />
	     <el-table-column
	       label="总负责人"
	       prop="examiAmount[5]"
	       align="center"
	       :show-overflow-tooltip="true"
	     />
	     <el-table-column
	       label="工作负责人"
	       prop="examiAmount[6]"
	       align="center"
	     />
	     <el-table-column
	       label="设备主人"
	       prop="examiAmount[7]"
	       align="center"
	       :show-overflow-tooltip="true"
	     >
	     </el-table-column>
	   </el-table-column>
         <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="['powerdistribution:process:edit']"
             >修改</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-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
	  <el-form ref="form" :model="form" :rules="rules" label-width="100px">
	    <el-form-item label="月计划编号" prop="planCode">
	      <el-input v-model="form.planCode" placeholder="请输入月计划编号"  readonly/>
	    </el-form-item>
	<el-row :gutter="24">
		<el-col :lg="12">
			<el-form-item label="停电设备数" prop="powerOutageDevices">
			  <el-input-number v-model="form.powerOutageDevices" placeholder="请输入月计划停电设备数" min="0"/>
			</el-form-item>
		</el-col>
		<el-col :lg="12">
	    <el-form-item label="停电时户数" prop="perHourHouseholdsM">
			<el-input-number v-model="form.perHourHouseholdsM" placeholder="请输入月计划时户数" min="0"/>
	    </el-form-item>
	</el-col>
	</el-row>
	    <el-row :gutter="24">
		<el-col :lg="12">
	    <el-form-item label="市州单位" prop="city">
	      <el-input v-model="form.city" placeholder="请输入市州单位" readonly/>
	    </el-form-item>
		</el-col>
	<el-col :lg="12">
	    <el-form-item label="区县单位" prop="county">
	      <el-input v-model="form.county" placeholder="请输入区县单位" readonly/>
	    </el-form-item>
		</el-col>
	</el-row>
	<el-row :gutter="24">
		<el-col :lg="12">
	    <el-form-item label="供电服务站/所" prop="station">
	      <el-input v-model="form.station" placeholder="请输入所属供电所" readonly/>
	    </el-form-item>
		</el-col>
	<el-col :lg="12">
	<el-form-item label="申报单位" prop="declarAgency">
	  <el-input v-model="form.declarAgency" placeholder="请输入申报单位" readonly/>
	</el-form-item>
		</el-col>
	</el-row>
	<el-row :gutter="24">
		<el-col :lg="12">
	    <el-form-item label="线路编号" prop="lineCode">
	      <el-input v-model="form.lineCode" placeholder="请输入线路编号" readonly/>
	    </el-form-item>
		</el-col>
		<el-col :lg="12">
	    <el-form-item label="线路名称" prop="lineName">
	      <el-input v-model="form.lineName" placeholder="请输入线路名称" readonly/>
	    </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="fileShow" :visible.sync="uploadVisible" width="550px" append-to-body>
		  <el-form ref="uploadForm" :model="uploadForm" label-width="100px">
			  <el-form-item label="月计划编号" prop="planCode">
			    <el-input v-model="uploadForm.planCode" placeholder="请输入月计划编号"  readonly/>
			  </el-form-item>
			  <el-form-item :label="label">
			      <fileUpload  :fileSize="10" :limit="1" :process="process" @success="handleSuccess"/>
			  </el-form-item>
		  </el-form>
		  <div slot="footer" class="dialog-footer">
		    <el-button type="primary" @click="submitForm">确 定</el-button>
		    <el-button  @click="uploadVisible=false" >取 消</el-button>
		  </div>
	 </el-dialog>
	<recordDetails v-if="dialog.detailsSave" ref="detailsDialog" @closed="dialog.detailsSave=false"></recordDetails>
	</div>
	
</template>

<script>
import { listProcess, getProcess, delProcess, addProcess, updateProcess } from "@/api/powerdistribution/process";
import recordDetails from './details'
import fileUpload from "./components/fileUpload"
export default {
  name: "Process",
  components: {
  	recordDetails,
	fileUpload
  },
  data() {
    return {
		dialog: {
			detailsSave:false
		},
	  uploadVisible: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
	  
	  // 检修计划
	  maintenancePalan: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检修计划全流程管理表格数据
      processList: [],
      // 弹出层标题
      title: "",
	  fileShow: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planCode: null,
		planCodeW:null,
        powerOutageDevices: null,
        perHourHouseholdsM: null,
        perHourHouseholdsW: null,
        city: null,
        county: null,
        station: null,
        lineCode: null,
        lineName: null,
        perHourHouseholdsArtif: null,
        declarAgency: null,
        isMaintenance: null,
        isApprove: null,
        maintenanceFilePath: null,
        approveFilePath: null,
      },
      // 表单参数
      form: {},
	  uploadForm: {},
	  process:{},
	  label: '',
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询检修计划全流程管理列表 */
    getList() {
      this.loading = true;
      listProcess(this.queryParams).then(response => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        planCode: null,
		planCodeW: null,
        powerOutageDevices: null,
        perHourHouseholdsM: null,
        perHourHouseholdsW: null,
        city: null,
        county: null,
        station: null,
        lineCode: null,
        lineName: null,
        perHourHouseholdsArtif: null,
        declarAgency: null,
        isMaintenance: null,
        isApprove: null,
        maintenanceFilePath: null,
        approveFilePath: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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
	  if(selection.length==1 && selection[0].perHourHouseholdsM>150){
		   this.maintenancePalan=false;
		   this.process= selection[0];
	  }else{
		   this.maintenancePalan=true;
		   this.process=null;
	  }

    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检修计划全流程管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProcess(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改时户数/停电设备数";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["uploadForm"].validate(valid => {
        if (valid) {
          if (this.uploadForm.id != null) {
            updateProcess(this.uploadForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.uploadVisible = false;
              this.getList();
            });
          } else {
            addProcess(this.uploadForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.uploadVisible = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除检修计划全流程管理编号为"' + ids + '"的数据项？').then(function() {
        return delProcess(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('powerdistribution/process/export', {
        ...this.queryParams
      }, `process_${new Date().getTime()}.xlsx`)
    },
	//查看
	table_show(row){
		this.dialog.detailsSave = true
		this.$nextTick(() => {
			this.$refs.detailsDialog.open().setData(row)
		})
	},
	    getRowClassName({ row }) {
	      // 根据row的某些属性决定是否高亮
	      if (row.perHourHouseholdsM-row.perHourHouseholdsW!=0) {
	        return 'highlight-row';
	      }
	      return '';
	    },
		uploadMaintenanceFile(){
			this.uploadForm.uploadType=0;
			Object.assign(this.uploadForm, this.process)
			this.uploadVisible=true;
			this.fileShow="检修文件上传";
			this.label="检修文件"
		},
		uploadApproveFile(){
			this.uploadForm.uploadType=1;
			Object.assign(this.uploadForm, this.process)
			this.uploadVisible=true;
			this.fileShow="审批手续上传";
			this.label="审批手续"
		},
		async handleSuccess(data) {
			Object.assign(data, this.uploadForm)
			if(this.uploadForm.uploadType==0){
				this.uploadForm.maintenanceFilePath=data.fileUrl;
				this.uploadForm.maintenanceFileName=data.originalFilename;
			}
			if(this.uploadForm.uploadType==1){
				this.uploadForm.approveFilePath=data.fileUrl;
				this.uploadForm.approveFileName=data.originalFilename;
			}
		},
  }
};
</script>
<style>
/* 通过自定义类名添加高亮样式 */
.highlight-row {
  background-color: #ff0000; /* 浅绿色背景色 */
  color: #ff0000; /* 绿色文字 */
}
</style>