<template>
  <div class="basic-main">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' class="search_area" @condition-changed='search' v-ref:criteria>
        <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
          <div class="row">
            <div class="col-sm-4" style="width: 20%">
              <label class="font_normal_body">用户姓名</label>
              <input type="text" class="input_search" v-model="model.f_user_name"
                     placeholder="用户姓名" style="width: 60%"
                     condition="f_user_name like '%{}%'">
            </div>
            <div class="col-sm-4" style="width: 20%">
              <label class="font_normal_body">用户电话</label>
              <input type="text" class="input_search" v-model="model.f_user_phone"
                     placeholder="用户电话" style="width: 60%"
                     condition="f_user_phone like '%{}%'">
            </div>
            <div class="col-sm-4" style="width: 20%">
              <label class="font_normal_body">安检计划</label>
              <input type="text" class="input_search" v-model="$parent.$parent.model.f_check_plan.f_plan_name"
                     @click="$parent.$parent.showChooser" style="width: 60%"
                     placeholder="安检计划" readonly="readonly">
            </div>
            <div class="col-sm-4" style="width: 20%">
              <label class="font_normal_body">开始时间</label>
              <datepicker
                :value.sync="model.f_check_start"
                placeholder='开始时间'
                :disabled-days-of-week="[]"
                :format="'yyyy-MM-dd'"
                :show-rest-button="reset"
                v-model="model.f_check_start" style="width: 60%"
                condition="f_offsite_time > ='{} 00:00:00'">
              </datepicker>
            </div>
            <div class="col-sm-4 form-group form-input-group" style="width: auto;margin-top:8px;float: right">
              <button class="button_spacing button_search" @click="search()">查询</button>
              <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 class="col-sm-4" style="width: 20%" v-if="$parent.$parent.criteriaShow">
            <label class="font_normal_body">结束时间</label>
            <datepicker
              :value.sync="model.f_check_end"
              placeholder='结束时间'
              :disabled-days-of-week="[]"
              :format="'yyyy-MM-dd'"
              :show-rest-button="reset" style="width: 60%"
              v-model="model.f_check_end"
              condition="f_offsite_time <=  '{} 23:59:59'">
            </datepicker>
          </div>
          <div class="col-sm-4" style="width: 20%" v-if="$parent.$parent.criteriaShow">
            <label class="font_normal_body">安&nbsp;&nbsp;检&nbsp;员</label>
            <v-select :value.sync="model.f_checker_id" v-model='model.f_checker_id'
                      :value-single="true"
                      class="select_list select"
                      :options='$parent.$parent.checkersid' placeholder='安检员'
                      close-on-select style="width: 60%"
                      condition="f_checker_id = '{}'"></v-select>
          </div>
          <div class="col-sm-4" style="width: 20%" v-if="$parent.$parent.criteriaShow">
            <label class="font_normal_body">安检状态</label>
            <v-select id="f_entry"
                      class="select_list select"
                      placeholder='安检状态'
                      v-model='model.f_entry'
                      :value.sync="$parent.$parent.model.f_entry"
                      :options='$parent.$parent.f_entry_status'
                      condition="" style="width: 60%"
                      close-on-select clear-button></v-select>
          </div>
          <div class="col-sm-4" style="width: 20%" v-if="$parent.$parent.criteriaShow">
            <label class="font_normal_body">审核状态</label>
            <v-select id="f_approved"
                      class="select_list select"
                      placeholder='审核状态'
                      v-model='model.f_approved'
                      :value.sync="$parent.$parent.model.f_approved"
                      :options='$parent.$parent.f_approval_states'
                      condition="" style="width: 60%"
                      close-on-select clear-button></v-select>
          </div>
          <modal :show.sync="$parent.$parent.showModal" v-ref:modal>
            <div slot="modal-header" class="modal-header">
              <h4 class="modal-title">
                选择计划
              </h4>
            </div>
            <div slot="modal-body" class="modal-body">
              <plan-chooser :f.sync="$parent.$parent.fun" v-ref:chooser></plan-chooser>
            </div>
            <div slot="modal-footer" class="modal-footer" style="text-align: center">
              <button class="button_search" @click="$parent.$parent.ok">确认</button>
              <button class="button_search" @click="$parent.$parent.cancel">取消</button>
            </div>
          </modal>
        </div>
      </criteria>
      <data-grid class="list_area table_sy" style="overflow: hidden" :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>操作</th>
            <th>安检状态</th>
            <th>审核状态</th>
            <th>用户编号</th>
            <th>用户名称</th>
            <th>入户时间</th>
            <th>离开时间</th>
            <th>安检人</th>
            <th>用户类型</th>
            <th>详细地址</th>
          </tr>
        </template>
        <template partial='body'>
          <td>
            <button class="button_spacing button_search-1" @click='$parent.$parent.$parent.change(row)'>查看</button>
            <button class="button_new-1 button_spacing" @click='$parent.$parent.$parent.verify(row)'
                    v-show="row.f_approved == '未审核'">审核
            </button>
          </td>
          <td style="text-align: center">{{row.f_entry_status}}</td>
          <td style="text-align: center">{{row.f_approved}}</td>
          <td style="text-align: center">{{row.f_userinfo_code}}</td>
          <td style="text-align: center">{{row.f_user_name}}</td>
          <td style="text-align: center">{{row.f_onsite_time}}</td>
          <td style="text-align: center">{{row.f_offsite_time}}</td>
          <td style="text-align: center">{{row.f_checker_name}}</td>
          <td style="text-align: center">{{row.f_check_type}}</td>
          <td style="text-align: center">{{row.f_address}}</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

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

    //获取json配置文件
    let asyncReady = async function (self) {
        // 获取配置信息
        try {
            let res = await Vue.resetget(`/phone/rs/vue`, {data: {}}, {resolveMsg: null, rejectMsg: null})
            Vue.config = res.data
        } catch (error) {
            // 忽略704,文件找不到异常
            if (error.status !== 704) {
                throw error
            }
        }
    }

    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})
        console.log('获取安检员', getGasman)
        getGasman.data.forEach((checker) => {
            self.checkersid.push({label: checker.name, value: checker.id})
        })
        self.checkersid.push({label: '全部', value: ''})
    }

    export default {
        title: '安检单审核',
        data() {
            let model = new PagedList('api/af-safecheck/sql/QueryCheckPaper', 20, {
                f_plan_id: 'this.f_check_plan.id',
                f_approved: 'this.f_approved[0]',
                f_entry_status: 'this.f_entry[0]',
                f_no_checkplan: 'this.f_no_checkplan[0]',
              f_filialeid: 'this.f_filialeid'
            })
            model.f_check_plan = {f_plan_name: '', id: ''}
            model.f_approved = ['']
            model.f_entry = ['']
            model.f_no_checkplan = ['']
            model.f_filialeid = this.$login.f.orgid
            model.f_subcompany = this.$login.f.f_fengongsi
            return {
                model: model,
                criteriaShow:false,
                f: this.$login.f,
                showModal: false,
                checkersid: [],
                f_approval_states: this.$appdata.getParam('安检单审核状态'),
                f_entry_status: this.$appdata.getParam('安检状态'),
                f_checkplans: this.$appdata.getParam('有无计划'),
            }
        },
        methods: {
            change(row) {
                Vue.$login = {jwt: row.f_checker_id}
                asyncReady(this).then(() => {
                        row.f_upload_state = '已传'
                        if (row.f_check_type == '民用') {
                            this.$goto('paper-set', {f_plan_id: row.f_check_plan_id, item: row, role: 'view'}, 'self')
                        } else {
                            this.$goto('paper-set-noncivil', {
                                f_plan_id: row.f_check_plan_id,
                                item: row,
                                role: 'view'
                            }, 'self')
                        }
                    }
                ).catch(
                    (respanse) => {
                        alert('获取配置文件失败')
                    })
            },
            verify(row) {
                this.$goto('verify-form', {item: row, name: this.f.name}, 'self', this.refresh)
            },
            hidden(){
              this.criteriaShow=!this.criteriaShow
            },
            refresh() {
                this.$refs.paged.$refs.criteria.search()
            },
            showChooser() {
                this.showModal = true
            },
            ok() {
                this.showModal = false
                if (this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow.id) {
                    this.model.f_check_plan = this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow
                }
            },
            readConfig() {
                let gen = asyncReady(this)
                return co(gen)
            },
            cancel() {
                this.showModal = false
                this.model.f_check_plan = {f_plan_name: '', id: ''}
            }
        },
        ready() {
            let gen = select(this)
            return co(gen)
        },
        watch: {
            'model.f_no_checkplan'(val) {
                if (this.model.f_no_checkplan[0] == '有计划安检') {
                    this.showChooser()
                }
            }
        }
    }
</script>
