<template>
  <div>
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' class="search_area" @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-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}">
              <label class="font_normal_body">计划名称</label>
              <input type="text" v-model="model.f_plan_name" id="f_plan_name"
                     placeholder="计划名称" class="input_search" style="width: 60%"
                     condition="f_plan_name like '%{}%'">
            </div>
            <div :class="{'col-sm-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}">
              <label class="font_normal_body">是否下发</label>
              <v-select :value.sync="model.f_issued"
                        :options='$parent.$parent.op_issued' placeholder='是否下发'
                        :value-single="true"
                        close-on-select clear-button
                        v-model="model.f_issued" condition="f_issued='{}'" style="width:60%"
                        class="select select_list"></v-select>
            </div>
            <div :class="{'col-sm-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}"
                 v-if="!$parent.$parent.$parent.showItem">
              <label class="font_normal_body">计划年份</label>
              <v-select style="width:60% "
                        class="select select_list"
                        id="c_plan_year" :value.sync="model.f_plan_year"
                        v-model="model.f_plan_year"
                        :options='$parent.$parent.c_year_list'
                        placeholder='计划年份'
                        condition="f_plan_year={}"
                        close-on-select>
              </v-select>
            </div>
            <div :class="{'col-sm-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}">
              <div class="span" style="float: right;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(true)">归置</button>-->
                <div style="float: right" class="button_spacing"
                     :class="{'button_shrink_left':$parent.$parent.$parent.showItem,'button_shrink_right':!$parent.$parent.$parent.showItem}"
                     @click="$parent.$parent.hiddenr()"></div>
                <div style="float: right" class="button_spacing"
                     :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                     @click="$parent.$parent.hidden()"></div>
              </div>
            </div>
          </div>
          <div class="row" v-if="$parent.$parent.criteriaShow">
            <div :class="{'col-sm-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}"
                 v-if="$parent.$parent.$parent.showItem">
              <label class="font_normal_body">计划年份</label>
              <v-select style="width:60% "
                        class="select select_list"
                        id="c_plan_year" :value.sync="model.f_plan_year"
                        v-model="model.f_plan_year"
                        :options='$parent.$parent.c_year_list'
                        placeholder='计划年份'
                        condition="f_plan_year={}"
                        close-on-select>
              </v-select>

            </div>
            <div :class="{'col-sm-4':$parent.$parent.$parent.showItem,'col-sm-3':!$parent.$parent.$parent.showItem}">
              <label class="font_normal_body">计划月份</label>
              <v-select id="c_plan_month" :value.sync="model.f_plan_month" placeholder='计划月份'
                        :options='$parent.$parent.c_month_list' close-on-select clear-button style="width:60% "
                        class="select select_list"
                        v-model="model.f_plan_month" condition="f_plan_month='{}'"></v-select>
            </div>
            <div :class="{'col-sm-8':$parent.$parent.$parent.showItem,'col-sm-6':!$parent.$parent.$parent.showItem}">
              <role-selector-safe
                      role-name="安检员"
                      role-lable="安&nbsp;检&nbsp;员&nbsp;"
                      @re-res="$parent.$parent.getRes"
                      :value.sync="model.f_checker_id"
                      v-model="model.f_checker_id"
                      condition="f_checker = '{}'">
              </role-selector-safe>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" partial='list' class="list_area table_sy" style="overflow: hidden" 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.plan_count}}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{row.checked}}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{row.unchecked}}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{row.f_issued}}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{row.f_plan_year}}-{{row.f_plan_month}}</nobr>
          </td>
          <td style="text-align: center">
            <nobr>{{row.f_checker}}</nobr>
          </td>
          <td style="text-align: center" v-if="row.f_end_type=='true'">
            <nobr>完成</nobr>
          </td>
          <td style="text-align: center" v-if="row.f_end_type=='false'">
            <nobr>未完成</nobr>
          </td>
          <td style="text-align: center">
            <span @click="$parent.$parent.$parent.delete(row)" v-show="row.f_issued == '未下发'"><img
              src="../../../static/images/newStyle/clear.png"></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)"
                    v-show="row.f_checker != null">添加用户
            </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 col-md-4 col-lg-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 class="col-sm-4 col-md-4 col-lg-4   form-group form-fix-width">-->
          <!--<label class="control-label">有效起始</label>-->
          <!--<datepicker-->
          <!--:value.sync="f_effective_start_time"-->
          <!--placeholder='最后安检时间起始'-->
          <!--:disabled-days-of-week="[]"-->
          <!--:format="'yyyy-MM-dd'"-->
          <!--:show-rest-button="reset"-->
          <!--v-model = "f_effective_start_time">-->
          <!--</datepicker>-->
          <!--</div>-->
          <!--<div class="col-sm-4 col-md-4 col-lg-4   form-group form-fix-width">-->
          <!--<label class="control-label">有效结束</label>-->
          <!--<datepicker-->
          <!--:value.sync="f_effective_end_time"-->
          <!--placeholder='最后安检时间起始'-->
          <!--:disabled-days-of-week="[]"-->
          <!--:format="'yyyy-MM-dd'"-->
          <!--:show-rest-button="reset"-->
          <!--v-model = "f_effective_end_time">-->
          <!--</datepicker>-->
          <!--</div>-->
        </div>
      </div>
      <div slot="modal-footer" class="modal-footer">
        <button class="btn btn-success width-80" @click="ok()">确认</button>
        <button class="btn btn-success width-80" @click="cancel()">取消</button>
      </div>
    </modal>
  </div>
</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) {
        yield self.$resetpost('rs/logic/deleteCheckPlan', row, {
            warnMsg: '删除数据不可恢复，确认删除吗？',
            resolveMsg: '恭喜！删除成功！',
            rejectMsg: '删除失败：'
        })
        self.$refs.paged.$refs.criteria.search()
    }

    let saveGen = function* (self) {
        //tag
        self.NowSelected.f_issued = '是'
        self.NowSelected.f_issue_time = self.$login.toStandardTimeString()
        self.NowSelected.f_send_operator = self.$login.f.name
        yield self.$resetpost('rs/logic/BespeakCheckPlan', {data: self.NowSelected}, {
            resolveMsg: '下发计划成功！',
            rejectMsg: '下发计划失败！'
        }).then((respanse) => {
            if (respanse.data.code == 200) {
                self.$showMessage("预约计划下发成功!")
                self.model.search(self.$refs.paged.$refs.criteria.condition)
            } else {
                self.$showMessage("预约计划下发失败!")
            }
            self.NowSelected = {}
        })
    }

    // let select = function* (self) {
    //     let getGasman = yield self.$resetpost('/rs/search', {
    //         source: 'this.getParentByType($department$).getAllChildrens().where(row.getAttributes().get($rolestr$).indexOf($安检员$) != -1))',
    //         userid: self.$login.f.id
    //     }, {resolveMsg: null, rejectMsg: null})
    //     //tag
    //     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: ''})
    // }
    let select = function* (self) {
      //tag
      let http = new HttpResetClass()
      let getGasman = yield http.load('POST', '/rs/search', {
        source: 'this.getParentByType($department$).getAllChildrens().where(row.getAttributes().get($rolestr$).indexOf($安检员$) != -1))',
        userid: self.$login.f.id
      }, {resolveMsg: null, rejectMsg: null})
      //tag
      //tag
      getGasman.data.forEach((checker) => {
        self.checkers.push({label: checker.name, value: checker})
        self.checkersid.push({label: checker.name, value: checker.id})
      })
    }
    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: '"*"',
                groupitem: '""',
                orderitem: '"f_plan_month"',
              f_filialeid: 'this.f_filialeid',
              f_filialeids: 'this.f_filialeids'
            })
            model.f_plan_year = year + ''
            model.f_plan_month = moptions[new Date().getMonth()].value,
            model.f_filialeid = this.$login.f.orgid
            model.f_filialeids = '('+this.$login.f.orgid+')'
            model.chosenOrg = {id: 0, name: ''}
            return {
                showCreate: false,         //向计划内添加用户弹出框
                modifychecker: {},         // 修改安检人
                selectyplan: {},            // 修改的计划
                checker: this.$login.f,
                showModal: false,
                checkers: [],
                checkersid: [],
                criteriaShow: false,
                chosenOrg: {id: 0, name: ''},
                f: this.$login.f,
                NowSelected: {},
                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: '否'}
                ]
            }
        },
        methods: {
            itemAdd(row) {
                this.$parent.itemShow = true;
                this.$parent.checkplan = row
            },
            // 修改计划安检员
            modify(row) {
                //tag
                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
            },
            hidden() {
                this.criteriaShow = !this.criteriaShow
            },
            hiddenr() {
                this.$parent.showItem = !this.$parent.showItem
            },
            cancel() {
                this.showModal = false
                this.modifychecker = {}
                this.f_effective_start_time = ''
                this.f_effective_end_time = ''
            },
            ok() {
                let _this = this
                //tag
                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
                }
                //tag
                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()
                })
            },
            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 =  '否'"
                // }
              //tag
              //tag
                this.model.search(args.condition, args.model)
            },
            distribute() {
                this.NowSelected = this.$refs.paged.$refs.grid.selected
                let http = new HttpResetClass()
                http.load('POST', 'rs/sql/singleTable', {
                    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 {
                            let gen = saveGen(this)
                            co(gen)
                        }
                    }
                })
            },
            getRes(obj){
              this.model.f_filialeids = this.$login.convertToIn(obj.resids)
              //tag
              //tag
            }
        },
        ready() {
            //tag
            this.$refs.paged.$refs.criteria.model.f_issued = '否'
            let gen = select(this)
            return co(gen)
        },
        watch: {
            'checker'() {
                //tag
                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>
