<template>
  <div style="flex:1;" class="flex">
    <criteria-paged :model="model"  v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.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: 25%;padding: 0px 6px 0px 6px">
              <label class="control-label">计划名称</label>
              <input type="text" class="form-control" v-model="model.f_plan_name" id="f_plan_name"
                     placeholder="计划名称" style="height:34px;margin-top: 8px"
                     condition="f_plan_name like '%{}%'">
            </div>
            <div class="col-sm-3 form-group form-input-group" style="width: 25%;padding: 0px 6px 0px 6px">
              <label class="control-label">是否下发</label>
              <v-select :value.sync="model.f_issued" style="margin-top: 8px"
                        :options='$parent.$parent.op_issued' placeholder='是否下发'
                        :value-single="true"
                        close-on-select clear-button
                        v-model="model.f_issued" condition="f_issued='{}'"> </v-select>
            </div>
            <div class="col-sm-3 form-group form-input-group" style="width: 25%;padding: 0px 6px 0px 6px">
              <label class="control-label">下发人</label>
              <input type="text" class="form-control" v-model="model.f_send_operator" id="f_send_operator"
                     placeholder="下发人" style="height:34px;margin-top: 8px"
                     condition="f_send_operator = '{}'">
            </div>
            <!--<div class="col-sm-3 form-group form-input-group" style="width: 25%;padding: 0px 6px 0px 6px">-->
              <!--<label class="control-label">安&nbsp;&nbsp;检&nbsp;员</label>-->
              <!--<v-select :value.sync="model.f_checker_id" v-model='model.f_checker_id'-->
                        <!--:value-single="true" style="margin-top: 8px"-->
                        <!--:options='$parent.$parent.checkersid' placeholder='安检员'-->
                        <!--close-on-select-->
                        <!--condition="f_checker_id = '{}'"></v-select>-->
            <!--</div>-->
            <div class="col-sm-3 form-group form-input-group" style="width: 25%;padding: 0px 6px 0px 6px">
              <label class="control-label">计划类型</label>
              <v-select :value.sync="model.f_plan_type" style="margin-top: 8px"
                        :options='$parent.$parent.plan_types' placeholder='计划类型'
                        :value-single="true"
                        close-on-select clear-button
                        v-model="model.f_plan_type" condition="f_plan_type='{}'"> </v-select>
            </div>
            <div class="col-sm-6">
              <div class="span" style = "float:left;margin-top:8px">
                <button class="button_search button_spacing" @click="search()">查询</button>
                <button class="button_export button_spacing" @click="$parent.$parent.distribute()">下发计划</button>
<!--                <button class="button_search button_spacing" v-show="$parent.$parent.endif && model.f_issued=='是'"  @click="$parent.$parent.settingend('完成')">归置</button>-->
               </div>
            </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>计划名称</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>{{row.f_plan_name}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.f_issued}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.f_checker}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.f_plan_type}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.f_issue_time}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.f_send_operator}}</nobr></td>
          <td style="text-align: center"><nobr>{{row.f_end_type}}</nobr></td>
          <!--<td><span class="glyphicon glyphicon-trash" @click="$parent.$parent.$parent.delete(row)" v-show="row.f_issued == '否'"></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-->
            <!--<button type="button" class="btn btn-link" @click="$parent.$parent.$parent.modify(row)" v-show="row.f_checker != null">修改</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-->
            <!--<button type="button" class="btn btn-link" @click="$parent.$parent.$parent.itemAdd(row)">添加</button></td>-->
        </template>
      </data-grid>
    </criteria-paged>
    <modal :show.sync="showModal" backdrop="false">
      <div slot="modal-header" class="modal-header">
        <h4 class="modal-title">
          修改安检员
        </h4>
      </div>
      <div slot="modal-body" class="modal-body">
        <div class="row">
          <div class="col-sm-4  form-group form-fix-width">
            <label class="control-label">安检员</label>
            <v-select :value.sync="modifychecker" v-model='modifychecker'
                      :value-single="true"
                      :options='checkers' placeholder='安检员'
                      close-on-select></v-select>
          </div>
        </div>
      </div>
      <div slot="modal-footer" class="modal-footer text-right">
        <button  class="btn btn-success width-80" @click="ok()">确认</button>
        <button  class="btn btn-success width-80" @click="cancel()">取消</button>
      </div>
    </modal>
    <modal :show.sync="showChecker" backdrop="false">
      <div slot="modal-header" class="modal-header">
        <h4 class="modal-title">
          选择安检员
        </h4>
      </div>
      <div slot="modal-body" class="modal-body">
        <div class="row">
          <div class="col-sm-4  form-group form-fix-width">
            <label class="control-label">安检员</label>
            <v-select :value.sync="checkerObj" v-model='checkerObj'
                      :value-single="true"
                      :options='checkers' placeholder='安检员'
                      close-on-select></v-select>
          </div>
        </div>
      </div>
      <div slot="modal-footer" class="modal-footer text-right">
        <button  class="btn btn-success width-80" @click="save()">确认</button>
        <button  class="btn btn-success width-80" @click="cancelChoose()">取消</button>
      </div>
    </modal>
  </div>
  <work-busy :is-busy="showWait"></work-busy>
</template>

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

  let syncDelete = function * (self,row) {
    let data = {
      row:row,
      url:'http://',
      delinfo: [
        {
          "del_id": row.id,
          "table_name": "T_CHECK_PLAN",
          "del_time": Util.toStandardTimeString()
        }
      ]
    }
    console.log('data',data)
    self.$showMessage('删除数据不可恢复，确认删除吗？', ['confirm', 'cancel']).then((res)=>{
      if (res === 'confirm') {
        self.showWait = true
        self.$resetpost('rs/logic/deleteCheckPlan',data).then(()=>{
          self.$showMessage('删除成功！')
          self.showWait = false
          self.$refs.paged.$refs.criteria.search()
        })
      }})
  }

  let saveGen = function * (self) {
    console.log('进来了')
    self.NowSelected.f_checker = self.checkerObj.name
    self.NowSelected.f_checker_id = self.checkerObj.id
    self.NowSelected.f_issued = '是'
    self.NowSelected.f_issue_time = Util.toStandardTimeString()
    self.NowSelected.f_send_operator = self.$login.f.name
    yield self.$resetpost('rs/logic/BespeakOrderCheckPlan', {data: self.NowSelected}, {resolveMsg: '下发计划成功！', rejectMsg: '下发计划失败！'}).then((respanse)=>{
    if(respanse.data.code == 200){
      self.$showMessage("集中安检下发成功!")
      self.showChecker = false
      self.model.search(self.$refs.paged.$refs.criteria.condition)
    }else{
      self.$showMessage("集中安检下发失败!")
      self.showChecker = false
    }
    self.NowSelected = {}
    self.checkerObj = {}
  })
  }

  let select = function * (self) {
    let getGasman = yield self.$resetpost('/rs/search', {
      source: 'this.getParentByType($organization$).getChildByName($安检员$).getUsers()',
      userid: self.$login.f.id
    }, {resolveMsg: null, rejectMsg: null})
    console.log('获取安检员', getGasman)
    getGasman.data.forEach((checker)=>{
      self.checkers.push({label:checker.name,value:checker})
      self.checkersid.push({label:checker.name,value:checker.id})
  })
    self.checkersid.push({label:'全部',value:''})
  }
  export default {
    title: '计划下发',
    data() {
      let options = []
      let year = (new Date()).getFullYear()
      for (let i = year - 8; i <= year + 1; i++)
        options.push({label: i + '', value: i + ''})
      let moptions = []
      for (let i = 1; i <= 12; i++)
        moptions.push({label: i + '', value: i + ''})
      let model = new PagedList('rs/sql/集中安检下发', 20, {
        items: '"*"',
        tablename: '"t_check_plan"',
        groupitem: '""',
        orderitem: '"f_plan_month,f_issue_time"',
        f_subcompany: 'this.f_subcompany'
      })
      model.f_plan_year = year + ''
      model.f_plan_month = moptions[new Date().getMonth()].value,
      model.f_subcompany = this.$login.f.f_fengongsi
      model.chosenOrg = {id: 0, name: ''}
      return {
        showCreate:false,         //向计划内添加用户弹出框
        modifychecker:{},         // 修改安检人
        selectyplan:{},            // 修改的计划
        checkerObj:{},
        checker: this.$login.f,
        showModal:false,
        checkers: [],
        checkersid:[],
        chosenOrg: {id: 0, name: ''},
        f: this.$login.f,
        NowSelected:{},
        showWait:false,
        c_year_list: options,
        c_month_list: moptions,
        model: model,
        f_effective_start_time:'',
        f_effective_end_time:'',
        op_issued: [
          {label: '全部', value: ''},
          {label: '是', value: '是'},
          {label: '否', value: '否'}
        ],
        plan_types:[
          {label: '全部', value: ''},
          {label: '预约安检计划', value: '预约安检计划'},
          {label: '预约开户计划', value: '预约开户计划'}
        ],
        endif:false,
        clearendif:false,
        showChecker:false //选择安检员
      }
    },
    methods:{
      itemAdd(row){
        this.$emit('additem',row)
      },
      //归置计划状态
      settingend(flag){
        console.log(this.model.rows,'ces')
        if(this.model.rows.length<1){
          this.$showMessage('请进行查询后,进行归置!')
          return
        }
        let _http = new HttpResetClass()
        let self=this
        _http.load('POST','rs/logic/setPlanType', {f_end_type:flag}, {resolveMsg: null, rejectMsg: null}).then((res) => {
          console.log(res)
          if(res.data.mgs==200){
            window.alert('归置成功!')
          }
          self.model.search(self.$refs.paged.$refs.criteria.condition)
        })
      },
      // 修改计划安检员
      modify(row){
        console.log('row',row)
        this.f_effective_start_time = row.f_effective_start_time
        this.f_effective_end_time = row.f_effective_end_time
        this.selectyplan = row
        this.showModal = true
      },
      cancelChoose(){
        this.showModal = false
        this.modifychecker = {}
        this.f_effective_start_time = ''
        this.f_effective_end_time = ''
      },
      ok(){
        let _this = this
        console.log(this.modifychecker)
        if(!this.modifychecker.id){
          this.$showMessage('请选择安检员！')
          return
        }
        // if(!this.f_effective_start_time){
        //   this.$showMessage('请选择有效开始时间！')
        //   return
        // }
        // if(!this.f_effective_end_time){
        //   this.$showMessage('请选择有效结束时间！')
        //   return
        // }
        let data = {
          id : this.selectyplan.id,
          f_checker:this.modifychecker.name,
          f_checker_id:this.modifychecker.id,
          f_effective_start_time:this.f_effective_start_time,
          f_effective_end_time:this.f_effective_end_time
        }
        console.log('data==>',data)
        let _http = new HttpResetClass()
        _http.load('POST','rs/entity/t_check_plan', data, {resolveMsg: null, rejectMsg: null}).then((res) => {
          _this.showModal = false
          _this.modifychecker = {}
          _this.f_effective_start_time = ''
          _this.f_effective_end_time = ''
          _this.$refs.paged.$refs.criteria.search()
        })
      },
      save(){
        let gen = saveGen(this)
        co(gen)
      },
      delete(row){
        let gen = syncDelete(this,row)
        return co(gen)
      },
      search (args) {
        // if(!this.$refs.paged.$refs.criteria.model.f_issued){
        //   args.condition +=  "and f_issued =  '否'"
        // }
        this.model.search(args.condition + " and (f_plan_type = '预约安检计划' or f_plan_type = '预约开户计划') ", args.model)
      },
      distribute() {
        this.NowSelected = this.$refs.paged.$refs.grid.selected
        if(this.NowSelected==null){
          this.$showMessage('请选择下发计划!')
          return
        }
        console.log(this.NowSelected)
        let http = new HttpResetClass()
        http.load('POST','rs/sql/safecheckSingleTable', {data:{tablename:'t_check_plan_item',condition:`f_plan_id = '${this.NowSelected.id}'`}}, {resolveMsg: null, rejectMsg: null}).then((res) => {
          if(res.data.length == 0){
            this.$showMessage("该计划无用户！")
            return
          }
          if (!this.NowSelected) {
            this.$showMessage("请选择要下发的安检计划！")
          } else {
            if (this.NowSelected.f_issued == '是') {
              this.$showMessage("该计划已经被下发！")
            } else {
              this.showChecker = true
            }
          }
      })
      }
    },
    ready() {

      var newmonth=Util.toStandardDateString().substring(5,7)
      // var newmonth='12'
      if(newmonth=='12'){this.endif=true}
      this.$refs.paged.$refs.criteria.model.f_issued = '否'
      let gen = select(this)
      return co(gen)
    },
    watch: {
      'checker'() {
        console.log("f_checker", this.checker)
        this.model.chosenOrg = this.checker[0]
      }
    },
    computed: {
      selected() {
        return this.$refs.paged.$refs.grid.selected
      }
    }
  }
</script>
<style  scoped>
  .table th{
    background-color: #f2f6fa;
    color: black;
  }
</style>
