<template>
  <div>
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='search' v-ref:criteria>
        <validator name='v' partial>
          <div  class="form-inline auto" >
            <div class="form-group" >
              <v-select id="f_subcompany"
                        placeholder='请选择分公司'
                        :value.sync="$parent.$parent.model.f_subcompany"
                        :options='$parent.$parent.f_divisions'
                        close-on-select clear-button> </v-select>
            </div>
            <div class="form-group">
              <v-select id="f_user_type"
                        placeholder='请选择用户类型'
                        :value.sync="$parent.$parent.model.f_user_type"
                        :options='$parent.$parent.user_types'
                        close-on-select clear-button>
            </div>
            <div class="form-group">
              <datepicker id="startdate"
                          v-model='model.date1'
                          :value.sync="model.date1"
                          placeholder="开始时间"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="form-group">
              <datepicker id="enddate"
                          v-model='model.date2'
                          :value.sync="model.date2"
                          placeholder="结束时间"
                          :disabled-days-of-Week="[]"
                          :format="'yyyy-MM-dd'"
                          :show-reset-button="reset">
              </datepicker>
            </div>
            <div class="form-group">
              <input type="text" class="form-control" v-model="$parent.$parent.model.f_check_plan.f_plan_name" @click="$parent.$parent.showChooser"  readonly="readonly"
                     placeholder="安检计划">
            </div>
            <div class="form-group">
              <v-select id="f_user_type"
                        placeholder='请选择隐患类型'
                        :value.sync="$parent.$parent.model.f_defect"
                        :options='$parent.$parent.defects_option'
                        close-on-select clear-button> </v-select>
            </div>
            <div class="form-group">
              <button class="btn btn-success width-80" @click="search()">查询</button>
            </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.f"  v-ref:chooser></plan-chooser>
              </div>
              <div slot="modal-footer" class="modal-footer">
                <button type="button" class="btn btn-success width-80" @click="$parent.$parent.ok">确认</button>
                <button type="button" class="btn btn-success width-80" @click="$parent.$parent.cancel">取消</button>
              </div>
            </modal>
          </div>
        </validator>
      </criteria>
      <data-grid :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>
          </tr>
        </template>
        <template partial='body'>
          <td><button class="btn btn-default"  @click='$parent.$parent.$parent.change(row)'>详细</button></td>
          <td>{{row.f_user_name}}</td>
          <td>{{row.f_address}}</td>
          <td>{{row.f_offsite_time}}</td>
          <td>{{row.f_device_type}}</td>
          <td>{{row.f_defect_level}}</td>
          <td>{{row.f_defect}}</td>
          <td>{{row.f_resolve_type}}</td>
          <td>
            <button type="button" class="btn btn-default"  @click='$parent.$parent.$parent.see(row)' v-if="row.ratification == '已处理'">查看</button>
            <button type="button" class="btn btn-primary"  @click='$parent.$parent.$parent.resolve(row)' v-if="row.ratification != '已处理'">处理</button></td>
        </template>
      </data-grid>
    </criteria-paged>
    <defect-modal v-show="show" :defect.sync="defect" ></defect-modal>
  </div>
</template>

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

  export default {
    title: '隐患查询',
    props: ['f'],
    data () {
      let model = new PagedList('rs/sql/隐患明细查询', 20,
        {date1: 'this.model.date1', date2: 'this.model.date2',
          f_defect_level: 'this.f_defect_', f_subcompany: 'this.f_subcompany[0]',
          f_filialeids:'this.f_filialeids',
          f_user_type: 'this.f_user_type[0]', f_plan_id: 'this.f_check_plan.id'})
      model.f_subcompany = (this.$login.f.f_fengongsi == '' ?['']:this.$login.f.f_fengongsi)
      model.f_filialeids = this.$login.f.f_orgids
      model.f_user_type = ['']
      model.date1 = ''
      model.date2 = ''
      model.f_check_plan = {f_plan_name: '', id: ''}
      model.f_defect = ['']
      model.f_defect_ = ''
      return {
        defect:{},
        model: model,
        f_divisions:this.$login.f.f_fengongsis != '' ? [{label: this.$login.f.f_fengongsi, value: this.$login.f.f_fengongsi}]: AppData.getParam('安检系统用子公司', [{label:'请选择分公司',value:''}]),
        user_types: AppData.getParam('安检用户类型'),
        showModal: false,
        show: false,
        defects_option: AppData.getParam('隐患级别')
      }
    },
    watch : {
      'model.f_defect' (val) {
        if(val[0].length == 0)
          return
        if(val[0].length == 1)
          this.model.f_defect_ = 'like \'' + val[0] + '%\''
        else
          this.model.f_defect_ = ' = \'' + val[0].substring(0, val[0].indexOf('-')) + '\''
      }
    },
    methods: {
      showChooser () {
        this.showModal = true
      },
      ok () {
        this.showModal = false
        this.model.f_check_plan = this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow
      },
      cancel () {
        this.showModal = false
        this.model.f_check_plan = {f_plan_name: '', id: ''}
      },
      resolve(row){
        this.show = true
        row.flag = false
        this.defect = JSON.parse(JSON.stringify(row))
      },
      see(row){
        this.show = true
        row.flag = true
        this.defect = JSON.parse(JSON.stringify(row))
      },
      todo(row){
        this.show = false
        this.$resetpost('rs/entity/t_check_paper_defect',row).then((res)=>{
          this.$refs.paged.$refs.criteria.search()
        })

      },
      undo(){
        this.show = false
      },
      change(row) {
        row.id = row.f_check_item_id
        if (row.f_user_type == '民用') {
          this.$goto('check-paper', {f_plan_id: row.f_check_plan_id, item: row, role: 'view'}, 'self')
        } else {
          this.$goto('check-paper-noncivil', {f_plan_id: row.f_check_plan_id, item: row, role: 'view'}, 'self')
        }
      },
    }
  }
</script>
