<template>
  <div>
    <!--<button class="btn btn-xs" v-show="!criteriaShow" @click="show()">-->
      <!--<span class="glyphicon glyphicon-chevron-down" style="color: #000;"></span>-->
    <!--</button>-->
    <criteria-paged :model="model"  v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:criteria>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <!--<div class="col-sm-3 form-group form-input-group" style="width: 20%">-->
            <!--<label class="control-label">小区编号</label>-->
            <!--<input type="text" class="form-control" v-model="model.f_area_id" id="id" style="height:34px;margin-top: 8px"-->
            <!--placeholder="小区编号" condition="ta.id like '%{}%'">-->
            <!--</div>-->
            <div class="col-sm-3 form-group form-input-group" style="width: 20%">
              <label class="control-label">计划名称</label>
              <input type="text" class="form-control" v-model="model.f_plan_name" id="f_plan_name" style="height:34px;margin-top: 8px"
                     placeholder="计划名称" condition="f_plan_name like '%{}%'">
            </div>
            <div class="col-sm-3 form-group form-input-group" style="width: 20%">
              <label class="control-label">计划年份</label>
              <input type="text" class="form-control" v-model="model.f_plan_year" id="f_plan_year" style="height:34px;margin-top: 8px"
                     placeholder="计划年份" condition="f_plan_year = '{}'">
            </div>
            <div class="col-sm-3 form-group form-input-group" style="width: 20%">
              <label class="control-label">计划月份</label>
              <input type="text" class="form-control" v-model="model.f_plan_month" id="f_plan_month" style="height:34px;margin-top: 8px"
                     placeholder="计划月份" condition="f_plan_month = '{}'">
            </div>
            <div class="col-sm-3 form-group form-input-group" style="width: 20%">
              <label class="control-label">计划类型</label>
              <v-select placeholder='用户状态'
                        :value-single="true" style="margin-top: 8px"
                        :value.sync="model.f_plan_type"
                        :options='$parent.$parent.planTypes'
                        v-model="model.f_plan_type"
                        condition="f_no_checkplan = '{}'"
                        close-on-select clear-button> </v-select>
            </div>
            <div class="col-sm-3 form-group form-input-group" style="width: 20%">
              <label class="control-label">用户姓名</label>
              <input type="text" class="form-control" v-model="model.f_user_name" id="f_plan_name" style="height:34px;margin-top: 8px"
                     placeholder="用户姓名" condition="f_user_name like '%{}%'">
            </div>
        </div>
          <div class="row">
            <div class="col-sm-3 form-group form-input-group" style="width: 20%">
              <label class="control-label">卡&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;号</label>
              <input type="text" class="form-control" v-model="model.f_card_id" id="f_plan_name" style="height:34px;margin-top: 8px"
                     placeholder="卡号" condition="f_card_id = '{}'">
            </div>

          </div>
          <div class="row">
            <div class="span" style = "float:right;margin-top:5px">
              <button class="button_search button_spacing" @click="search()">查询</button>
              <button class="button_search button_spacing" @click="$parent.$parent.delete()">删除</button>
            </div>
          </div>

        </div>
      </criteria>
      <data-grid :model="model" partial="list" class="table table-bordered table table-hover" v-ref:grid >
        <template partial='head'>
          <tr>
            <!--<th><nobr>全选<input style="width: 22px;height: 22px;" type="checkbox" @click='$parent.$parent.$parent.changeStates(row)'  v-model='$parent.$parent.$parent.checkedAll'></nobr></th>-->
            <th><nobr></nobr></th>
            <th><nobr>计划名称</nobr></th>
            <th><nobr>计划类型</nobr></th>
            <th><nobr>用户姓名</nobr></th>
            <th><nobr>卡号</nobr></th>
            <th><nobr>小区名称</nobr></th>
            <th><nobr>用户地址</nobr></th>

          </tr>
        </template>
        <template partial='body'>
          <td style="text-align: center"><nobr v-if="row.f_state == '未检'"><input style="width: 22px;height: 22px;" type="checkbox" v-model="row.checked" v-bind:checked="row.checked"  @click="$parent.$parent.$parent.addChecked(row)"></nobr></td>
          <td style="text-align: center;">{{row.f_plan_name}}</td>
          <td style="text-align: center;">{{row.f_plan_type}}</td>
          <td  style="text-align: center;">{{row.f_user_name}}</td>
          <td  style="text-align: center">{{row.f_card_id}}</td>
          <td  style="text-align: center">{{row.f_residential_area}}</td>
          <td  style="text-align: center">{{row.f_address}}</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
  <!--<work-busy :is-busy="showWait"></work-busy>-->
</template>

<script>
  import { PagedList } from 'vue-client'
  import { HttpResetClass } from 'vue-client'
  import co from 'co'
  import * as Util from '../Util'
  import Vue from 'vue'
  import AppData from '../../stores/AppData'




  export default {
    title: '安检计划项删除',
    data() {
      return {
        // SafeCheckXZs: [{label: '未安检', value: '未安检'}, {label: '已安检', value: '已安检'}], // 安检筛选选项
        // SafeCheckSXs: [{label: '是', value: '是'}, {label: '否', value: '否'}, {label: '全部', value: ''},],
        // IsShares: [{label: '是', value: '是'}, {label: '否', value: '否'}],
        // tableStates:[{label: '全部', value: ''}, {label: '正常', value: '正常'}, {label: '停用', value: '停用'}],
        // dataSources:[{label: '用户档案', value: '用户档案'}, {label: '自建小区', value: '自建小区'}],
        model: new PagedList('rs/sql/searchCheckPlan',20,{}),
        checkboxModel: {},
        planTypes: [
          {label: '全部', value: ''},
          {label: '集中安检', value: '集中安检'},
          {label: '点火通气计划', value: '点火通气计划'},
          {label: '预约安检计划', value: '预约安检计划'},
          {label: '预约开户计划', value: '预约开户计划'},
          {label: '重点用户跟进计划', value: '重点用户跟进计划'},
          {label: '一级隐患跟进计划', value: '一级隐患跟进计划'},
          {label: '偷盗气用户跟进计划', value: '偷盗气用户跟进计划'},
          {label: '临街商铺安检计划', value: '临街商铺安检计划'},
          {label: '大锅灶安检计划', value: '大锅灶安检计划'}
        ],
      }
    },
    props: {
      checkplan: { // 传入一条安检计划信息
        type: Object
      },
      additemshow:{
        type:Boolean,
        default:false
      }
    },
    ready() {
    },
    methods: {
      itemAddBack(){
        this.$emit('finish')
      },
      itemAdd(val){
        let _gen = AddItem(this,val)
        return co(_gen)
      },
      // 选择安检员
      IsShareschange(val){
        this.IsShare = val
        if(val == '是'){
          this.checker = null
        }
      },
      checkerchange(val) {
        this.checkboxModel.f_checker = val.name
        this.checkboxModel.f_checker_id = val.id
      },
      search(args) {
        this.model.search(args.condition, args.model)
      },
      delete(){
        if(this.checkrows.length == 0){
          this.$showMessage('请选择需要删除的用户')
          return
        }
        this.$showMessage('确认删除选中计划项？删除后无法恢复', ['confirm', 'cancel']).then((res) => {
            console.log('res=====>' + res)
            if (res === 'confirm') {
              let condition = ''
              for (let i = 0;i<this.checkrows.length;i++){
                if(condition == ''){
                  condition = condition + `id in ( '${this.checkrows[i].id}'`
                }else{
                  condition += `,'${this.checkrows[i].id}'`
                }
              }
              condition = condition + ')'
              console.log('condition',condition)
              this.$resetpost('rs/logic/safeCheckDeletePlanItem',{data:{condition:condition}}).then((res)=>{
                console.log('res===>',res)
                if(res.data.code ==200){
                  this.$showMessage('删除成功')
                  this.$refs.paged.$refs.criteria.search()
                }
              })
            }
          }
        )
      },
      // checkstatus() {
      //   return [{label: '全部', value: ''}, ...this.$appdata.getParam('安检状态')]
      // },
      // usertypes() {
      //   return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]
      // },
      // userstates() {
      //   return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户状态')]
      // },
      show() {
        this.criteriaShow = true
      },
      hidden() {
        this.criteriaShow = false
      },
      confirm() {
        if (!this.checkboxModel.f_plan_name) {
          this.$showMessage('请填写安检计划名称!')
          return
        }
        // if(this.IsShare == '否'){
        //   if (!this.checkboxModel.f_checker_id) {
        //     this.$showMessage('请选择安检员！')
        //     return
        //   }
        // }
        this.showWait = true
        let gen = saveGen(this)
        return co(gen)
      },
      changeStates() {
        if (this.checkedAll) {//实现反选/
          this.checkrows = [];
          for (let i = 0; i < this.$refs.paged.model.rows.length; i++) {
            this.$set('$refs.paged.model.rows[' + i + '].checked', false)
          }
        } else {//实现全选
          for (let i = 0; i < this.$refs.paged.model.rows.length; i++) {
            this.$set('$refs.paged.model.rows[' + i + '].checked', true)
          }
          let _this = this
          let condition = this.$refs.paged.$refs.criteria.condition
          let url = this.model.url
          this.$resetpost(`${url}?pageSize=9999999`, {
            data: {
              condition: condition,
              groupitem: '',
              orderitem: 'f_residential_area',
              // f_usertype:this.model.f_usertype,
              f_filialeids: this.model.f_filialeids,
              month: this.model.month,
              // safeCycle:this.model.safeCycle,
              f_checktime_start:this.model.f_checktime_start,
              f_checktime_end:this.model.f_checktime_end,
              SafeCheckSX: this.model.SafeCheckSX,
              // f_last_check_state: this.model.f_last_check_state
            },
          },{resolveMsg: null, rejectMsg: null}, -1).then(
            (respanse) => {
              if (respanse.data.length >= 5000) {
                this.$showMessage("数据量太大，请重新选择")
              } else {
                _this.checkrows = []
                for (let j = 0; j < respanse.data.length; j++) {
                  _this.checkrows.push(respanse.data[j])
                }
                console.log(_this.checkrows);
              }
            })
        }
      },
      addChecked(item) {
        console.log(item);
        if (!item.checked) {
          this.checkrows.push({id:item.id});
          console.log(this.checkrows);
        } else {
          for (let i = 0; i < this.checkrows.length; i++) {
            if (item.id == this.checkrows[i].id) {
              this.checkrows.splice(i, 1)
            }
          }
        }
      },
      ToshowModal(f_residential_area) {
        this.areaname=f_residential_area
        this.creatPlanWay = 'single'
        // if (this.checkrows.length > 0) {
        this.showModal = true
        // } else {
        // this.$showMessage("请选择需要安检的用户!")
        // }
      },
      FiltrationPlan(){
        if(this.model.rows.length == 0){
          this.$showMessage("暂无可生成的计划！")
          return
        }
        this.creatPlanWay = 'filtration'
        this.showModal = true
      },
      AllPlan() {
        if(this.model.rows.length == 0){
          this.$showMessage("暂无可生成的计划！")
          return
        }
        this.isAll = 'all'
        this.showModal = true
      },
      cancel() {
        this.showModal = false
      },
      // areas() {
      //   console.log('登陆人信息',this.$login.f)
      //   let rs = []
      //   for (let i = 0; i < this.$login.f.f_allArea.length; i++) {
      //     let temp = {
      //       label: this.$login.f.f_allArea[i].label,
      //       value: this.$login.f.f_allArea[i].label
      //     }
      //     rs.push(temp)
      //   }
      //   return [{label: '全部', value: ''}, ...rs]
      // },
      initAdjustables() {
        console.log('初始化调压箱', this.$GetSaleParam)
        let arr = []
        let filter = this.$login.f.f_orgids
        this.$GetSaleParam.getAdjustable(filter).forEach((item) => {
          let temp = {}
          temp.label = `[${item.value.f_adjustable_id}]-${item.label}`
          temp.value = `${item.value.id}`
          arr.push(temp)
        })
        this.adjustables = [{label: '全部', value: ''}, ...arr]
      },
      CreadteArea(){
        this.showCreate = true
      },
      confirmCreate(){
        if(this.f_residential_area || this.f_street){
          let row = {
            f_residential_area : this.f_residential_area,
            f_street : this.f_street
          }
          this.$resetpost('rs/entity/t_create_area', row).then((res) => {
            console.log('res...===??..,,///]]]===---000999>',res)
            this.$showMessage('新增成功')
            this.model.url = 'rs/sql/FetchCreateAreaByOrgCheckerByArea'
            this.$refs.paged.$refs.criteria.search()
            this.showCreate = false
          })
        }

      },
      cancelCreate(){
        this.showCreate = false
      },
      addBuilding(){

      }
    },

    watch: {
      // 'model.f_usertype'(val){
      //   if(val == '民用'){
      //     this.model.safeCycle = this.civil
      //   }else if(val == '非民用'){
      //     this.model.safeCycle = this.civilian
      //   }
      // },
      'model.SafeCheckXZ'(val) {
        this.model.SafeCheckSC = ''
        if (val == '已安检') {
          this.model.SafeCheckCQ = ''
          this.model.SafeCheckSX = {
            f_check_end: '', // 安检时间  查询起始条件
            f_check_start: ''// 安检时间  查询终止条件
          }
        } else {
          // 当使用筛选条件时，清空不需要的条件选项
          this.model.f_issue_start = ''
          this.model.f_issue_end = ''
          this.$refs.paged.$refs.criteria.model.f_check_start = ''
          this.$refs.paged.$refs.criteria.model.f_check_end = ''
          // this.$refs.paged.$refs.criteria.model.f_last_check_state = ''
          this.model.month = ''
          if(val == '应安检'){
            this.model.SafeCheckSC = '否'

            this.model.f_issue_start = Util.toStartDateString()
            this.model.f_issue_end = Util.toStandardDateString()
          }
          // this.model.f_checktime_start = ''
          // this.model.f_checktime_end = ''
        }
      },
      'model.SafeCheckSC'(val) {
        if (val == '') {
          this.model.f_issue_start = ''
          this.model.f_issue_end = ''
        }
      },
      // 'checker'(val){
      //   console.log("checker",this.checker)
      //   if(val){
      //     this.checkboxModel.f_checker = this.checker[0].name
      //     this.checkboxModel.f_checker_id = this.checker[0].id
      //   }
      // },
      'model.rows'() {
        // if (!this.checkedAll) {//实现反选/
        this.checkrows = [];
        for (let i = 0; i < this.$refs.paged.model.rows.length; i++) {
          var k = false
          for (let j = 0; j < this.checkrows.length; j++) {
            if (this.$refs.paged.model.rows[i].f_userinfo_id == this.checkrows[j].f_userinfo_id) {
              this.$set('$refs.paged.model.rows[' + i + '].checked', true)
              k = true
              continue
            }
          }
          if (!k) {
            this.$set('$refs.paged.model.rows[' + i + '].checked', false)
          }
        }
        // } else {//实现全选
        //   // this.checkboxModel = {};
        //   for (let i = 0; i < this.$refs.paged.model.rows.length; i++) {
        //     this.$set('$refs.paged.model.rows[' + i + '].checked', true)
        //   }
        // }
      }
    },
    computed: {
      selected() {
        return this.$refs.paged.$refs.grid.selected
      }
    }
  }
</script>
<style scoped>
  .newcolor{
    color:red;
  }
  .table th{
    background-color: #f2f6fa;
    color: black;
  }
</style>
