<template>
  <div class="LiftMaintenancePlan">
    <el-row class="EmergencyMainHeader">
        <el-col>
          <span>维保计划</span>
        </el-col>
      </el-row>
    <el-row class="marT10" :gutter="10">
      <el-col :xs="5" :sm="5" :md="5" :lg="4" :xl="3">
        <el-select v-model="listConditions.projectId" placeholder="请选择项目" clearable filterable no-data-text="无数据">
          <el-option
            v-for="item in options.ProjectID"
            :key="item.projectId"
            :label="item.projectName"
            :value="item.projectId"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :xs="5" :sm="5" :md="5" :lg="4" :xl="3">
        <el-select v-model="listConditions.regionId" placeholder="请选择区域" clearable filterable no-data-text="无数据">
          <el-option
            v-for="item in options.RegionList"
            :key="item.id"
            :label="item.areaName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :xs="5" :sm="5" :md="5" :lg="4" :xl="3">
        <el-select v-model="listConditions.workerId" placeholder="请选择维保工" @blur="getRolemtWork" no-data-text="无数据" clearable filterable>
          <el-option
            v-for="item in options.releMtwork"
            :key="item.userId"
            :label="item.name"
            :value="item.userId"
          ></el-option>
        </el-select>
      </el-col>
      <el-col :xs="5" :sm="5" :md="5" :lg="4" :xl="3">
        <!-- <el-select v-model="listConditions.planInterval" placeholder="请选择维保间隔">
          <el-option
            v-for="item in options.IntervalVal"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select> -->
          <el-input v-model="listConditions.planInterval" placeholder="请选择维保间隔"></el-input>
      </el-col>
      <el-col :xs="4" :sm="4" :md="4" :lg="4" :xl="3">
        <el-input v-model="listConditions.registrationCode" placeholder="输入注册代码"></el-input>
      </el-col>
    </el-row>
    <el-row :gutter="10" class="marT10">
      <el-col :xs="10" :sm="10" :md="10" :lg="8" :xl="6">
        <el-row type="flex" justify="start">
          <el-col :xs="6" :sm="6" :md="6" :lg="7" :xl="5" class="font12">下次保养时间</el-col>
          <el-col :xs="18" :sm="18" :md="18" :lg="17" :xl="19" class="inBorderRadius">
            <el-date-picker
              v-model="listConditions.timeDate"
              @change="changeDate"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
        </el-row>
      </el-col>
      <el-col :xs="14" :sm="14" :md="14" :lg="11" :xl="7">
        <el-button type="primary" plain size="small" @click="getSeek" round>查询</el-button>
        <el-button type="info" plain size="small" @click="resetConditions" round>重置</el-button>
        <el-button v-if="LiftMaintenancePlanBtn.LiftMaintenancePlanBtn66" type="danger" plain size="small" round @click="printPlan">维保计划打印</el-button>
        <el-button v-if="LiftMaintenancePlanBtn.LiftMaintenancePlanBtn67" type="danger" plain size="small" @click="emptyPlan" round>清空维保计划</el-button>
        <el-button v-if="LiftMaintenancePlanBtn.LiftMaintenancePlanBtn65" type="danger" plain size="small" round @click="exportPlan">导出</el-button>
      </el-col>
    </el-row>
    <el-row class="marT10" type="flex" justify="space-between" v-if="LiftMaintenancePlanBtn.LiftMaintenancePlanBtn68">
      <el-col :span="6">
        <el-button type="primary" plain size="small" @click="batchPlan" round>批量制订维保计划</el-button>
      </el-col>
    </el-row>
    <lift-table  class="liftTable" :list-data="listConditions" ref="mychild" :LiftMaintenancePlanBtn="LiftMaintenancePlanBtn"></lift-table>
    <!-- dialog -->
    <div>
    </div>


    
  </div>
  <!--  -->
</template>

<script>
import liftTable from "../../components/Maintenance/liftMainTable.vue";
export default {
  components: {
    liftTable: liftTable
  },
  name: "LiftMaintenancePlan",
  data() {
    return {
      
      options: {
        ProjectID: [],
        IntervalVal: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
        RegionList:[],
        releMtwork:[],
      },
      loading: false,
      pagesize: 15,
      currpage: 1,
      // tableData: [],
      DataSelection: [],
      DiaModels: {
        plans: false,
        MainInter: false,
        KeepT: false
      },
      PlanData:{
        PlanValDate: null,
        IntervalVal:null,
        keepType:null
      },
      IntervalVal: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
      keepType: [
        { status: 1, value: "半月保" },
        { status: 2, value: "季度保" },
        { status: 3, value: "半年保" },
        { status: 4, value: "年保" }
      ],
      listConditions:{
        // mtCompanyId:JSON.parse(sessionStorage.getItem("companyID")).id,
        mtCompanyId:JSON.parse(sessionStorage.getItem("companyID")).id,
        regionId:null,
        projectId:null,
        planInterval:null,
        beginTime:null,
        endTime:null,
        registrationCode:null,
        timeDate:[]
      },
      LiftMaintenancePlanBtn:{
        LiftMaintenancePlanBtn65:0,
        LiftMaintenancePlanBtn66:0,
        LiftMaintenancePlanBtn67:0,
        LiftMaintenancePlanBtn68:0,
        LiftMaintenancePlanBtn69:0,
        LiftMaintenancePlanBtn70:0,
      }
    };
  },
  created() {
    var date = new Date();
    var year = date.getFullYear();
    var month =(date.getMonth() + 1 )< 10 ? "0" +(date.getMonth() + 1 ) : (date.getMonth() + 1 );
    this.planPrintMonth = year+"-"+month

    var LiftMaintenancePlanBtn = {}
    this.getButton(64).then(result => {
      if (result.data.statusCode==="1") {
        LiftMaintenancePlanBtn = result.data.data
        LiftMaintenancePlanBtn.map((item)=>{
          if(item.id==65){
            this.LiftMaintenancePlanBtn.LiftMaintenancePlanBtn65 = item.checked?1:0 
          }else if(item.id==66){
            this.LiftMaintenancePlanBtn.LiftMaintenancePlanBtn66 = item.checked?1:0  
          }else if(item.id==67){
            this.LiftMaintenancePlanBtn.LiftMaintenancePlanBtn67 = item.checked?1:0  
          }else if(item.id==68){
            this.LiftMaintenancePlanBtn.LiftMaintenancePlanBtn68 = item.checked?1:0  
          }else if(item.id==69){
            this.LiftMaintenancePlanBtn.LiftMaintenancePlanBtn69 = item.checked?1:0  
          }else if(item.id==70){
            this.LiftMaintenancePlanBtn.LiftMaintenancePlanBtn70 = item.checked?1:0  
          }
        })
        
      }
    });
  },
  mounted() {
    this.getRegionList();
    this.getFoucsProject();
  },
  methods: {
    // 修改计划
    // 查询
    getSeek() {
      this.$refs.mychild.getSeek();
    },
    // 重置
     resetConditions(){
        this.listConditions={
          // mtCompanyId:JSON.parse(sessionStorage.getItem("companyID")).id,
          mtCompanyId:JSON.parse(sessionStorage.getItem("companyID")).id,
          regionId:null,
          projectId:null,
          planInterval:null,
          beginTime:null,
          endTime:null,
          registrationCode:null,
          timeDate:[]
        },
       this.$refs.mychild.updataReset();
    },
    // 
    getRolemtWork(){
      var url="system/companyuser/getCompanyMembers";
      var param={roleCode:'ROLEMTWORK',companyId:JSON.parse(sessionStorage.getItem("companyID")).id};
      this.axios.post(url,param).then(res=>{
        if(res.statusCode==="1"){
          this.options.releMtwork=res.data;
        }else{
          this.options.releMtwork=[];
        }
      })
    },
    // 批量设置下次维保日期
    PlanDeter() {
      if (this.DataSelection === null || this.DataSelection.length <= 0)
      this.$message.error("请选择电梯");
      this.DiaModels.plans = false;
    },
    // 批量设置保养间隔
    PlanMains() {
      if (this.DataSelection === null || this.DataSelection.length <= 0)
      this.$message.error("请选择电梯");
      this.DiaModels.MainInter = false;
    },
    // 更改时间
    changeDate(val){
     console.log(val);
     this.listConditions.beginTime=val?val[0]:'';
     this.listConditions.endTime=val?val[1]:'';
    },
    printPlan(){
      this.$refs.mychild.printPlan();
    },
    // 批量打印
     batchPlan(){
        this.$refs.mychild.batchPlan();
      },
    // 清空维保计划
    emptyPlan(){
      this.$refs.mychild.emptyPlan();
    },
    getFoucsProject(){
       var url = "business/project/projectIDList";
        var a = JSON.parse(sessionStorage.getItem("companyID")).id;
       var params = { mtCompanyId:a };
       this.axios.post(url,params).then(res=>{
           if (res.statusCode === "1") {
          this.options.ProjectID = res.data;
        } else {
          this.$message(res.message);
        }
       })
    },
    getRegionList(){
      var url = "enterprise/region/project/regions";
      var param = {
        mtCompanyId: JSON.parse(sessionStorage.getItem("companyID")).id
        // mtCompanyId:1001102100031
      };
      this.axios.post(url, param).then(result => {
        var data = result;
        if(data.statusCode==="1"||data.statusCode==="9"){
          this.options.RegionList = data.data
        }else{
          this.$message.error(data.message);
        }
      })
    },
    exportPlan(){
      if (this.$refs.mychild.DataSelection.length == 0) {
        this.$message({
          message: "请先选择电梯",
          type: "warning"
        });
      } else {
        var list =this.$refs.mychild.DataSelection.map(item=>{
          return item.id;
        });
        
        var url="business/maintenance/export/plan";
        var param = {
          ids: list
        };
        this.axios.post(url, param, { responseType: "arraybuffer" }).then(res => {
          const aLink = document.createElement("a");
          let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          aLink.href = URL.createObjectURL(blob);
          aLink.setAttribute("download", "维保计划");
          aLink.click();
        });
      }
    },
    // watch:{
    //   'listConditions.timeDate':function(newVal){
    //     console.log(newVal);
    //   },
    // }
  }
};
</script>
<style lang="stylus">

</style>