<template>
  <div class="flex-row">
   <div class="basic-main">
    <criteria-paged :model="model" v-ref: paged@sort="sort">
      <criteria partial='criteria' class="search_area" @condition-changed='$parent.selfSearch' 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_userinfo_code"
                     style="width: 60%" placeholder="客户编号" condition="f_userinfo_code='{}'">
            </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_name"
                     style="width: 60%" placeholder="客户名称" 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"
                     style="width: 60%" placeholder="客户电话" 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: auto;margin-top:8px;float: right">
                  <export-excel   :data="$parent.$parent.searchData"
                                  :field="$parent.$parent.excelHeaders"
                                  sqlurl="api/af-safecheck/logic/exportfile" sql-name="QueryCheckPaper" template-name='安检计划明细'></export-excel>
              <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>
            <v-select id="f_entry"
                      class="select_list select"
                      placeholder='安检状态' style="width: 60%"
                      v-model='model.f_entry'
                      :value.sync="$parent.$parent.model.f_entry"
                      :options='$parent.$parent.f_entry_status'
                      condition=""
                      :value-single="true"
                      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>
            <datepicker
              class="select_list select"
              :value.sync="model.f_check_start"
              placeholder='开始时间' style="width: 60%"
              :disabled-days-of-week="[]"
              :format="'yyyy-MM-dd'"
              :show-rest-button="reset"
              v-model="model.f_check_start"
              condition="f_offsite_time > ='{} 00:00:00'">
            </datepicker>
          </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='结束时间' style="width: 60%"
              :disabled-days-of-week="[]"
              :format="'yyyy-MM-dd'"
              :show-rest-button="reset"
              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" style="width: 60%"
                      class="select_list select"
                      :options='$parent.$parent.checkersid' placeholder='安检员'
                      close-on-select
                      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 :value.sync="model.f_safecheck_type" v-model='model.f_safecheck_type'-->
                      <!--:value-single="true" style="width: 60%"-->
                      <!--class="select_list select"-->
                      <!--:options='$parent.$parent.safetype' placeholder='安检类型'-->
                      <!--close-on-select-->
                      <!--condition="f_safecheck_type = '{}'"></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><nobr>客户编号</nobr></th>
            <th><nobr>客户名称</nobr></th>
            <th><nobr>客户类型</nobr></th>
            <th><data-order field="f_address" name="客户地址"></data-order></th>
            <th><nobr>安检状态</nobr></th>
            <th><data-order field="f_offsite_time" name="安检时间"></data-order></th>
            <th><nobr>安检员</nobr></th>
            <th><nobr>操作</nobr></th>
          </tr>
        </template>
        <template partial='body'>

          <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_check_type}}</td>
          <td style="text-align: center">{{row.f_address}}</td>
          <td style="text-align: center">{{row.f_entry_status}}</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">
            <button class="button_spacing button_search-1" @click='$parent.$parent.$parent.change(row)'>查看</button>
            <button type="button" class="button_new-1 button_spacing" data-toggle="dropdown"
                    @click="$parent.$parent.$parent.print(row)">
              打印
            </button>
          </td>
        </template>
      </data-grid>
    </criteria-paged>
    <check-msg v-if="showcheckmsg" :show="showcheckmsg" :data="checkInfo"></check-msg>
  </div>
  </div>
</template>

<script>
    import {PagedList} from 'vue-client'
    import Vue from 'vue'
    import co from 'co'
    import AppData from '../../stores/AppData'
    import * as Util from '../Util'
    import getNowDate from "../../utils/getNowDate";
    import HttpResetClass from "vue-client/src/plugins/HttpResetClass";

    //获取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,文件找不到异常R
            if (error.status !== 704) {
                throw error
            }
        }
    }

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

    export default {
        title: '民用送气结果查看',
        data() {
            let model = new PagedList('api/af-safecheck/sql/QueryCheckPaper', 20, {
                f_plan_id: 'this.f_plan_id',
                f_approved: 'this.f_approved',
                f_entry_status: 'this.f_entry',
                f_no_checkplan: 'this.f_no_checkplan',
                f_filialeid: 'this.f_filialeid',
                orderitem: "'f_offsite_time desc'"
            })
            model.f_plan_id = ''
            model.f_approved = ''
            model.f_entry = ''
            model.f_no_checkplan = ''
            model.f_filialeid = this.$login.f.orgid
            model.f_check_type='民用'
          // let searchData={
          //     f_plan_id: this.model.f_checker_id==undefined?"":this.model.f_checker_id,
          //     f_approved: this.model.f_approved,
          //     f_entry_status: this.model.f_entry,
          //     f_no_checkplan:this.model.f_no_checkplan[0],
          //     f_filialeid: this.model.f_filialeid,
          //     //orderitem: this.model.f_offsite_time,
          //     //condition:this.model.condition
          // }
            // model.f_subcompany = this.$login.f.f_fengongsi
            return {
              searchData: {
                condition:"1=1",
                orderitem: "f_offsite_time desc",
                f_plan_id: "",
                f_approved: '',
                f_entry_status: '',
                f_no_checkplan: '',
                f_filialeid:this.$login.f.orgid
              },
                model: model,
                //searchData:searchData,
                excelHeaders: {
                'f_userinfo_code': '客户编号',
                'f_user_name': '客户姓名',
                'f_check_type': '客户类型',
                'f_address': '客户地址',
                'f_entry_status':'安检状态',
                'f_offsite_time':'安检时间',
                'f_checker_name': '安检员'
                },
                fun: this.$login.f,
                checkersid: [],
                criteriaShow:false,
                showModal: false,
                f_approval_states: this.$appdata.getParam('安检单审核状态'),
                f_entry_status: this.$appdata.getParam('安检状态'),
                f_checkplans: this.$appdata.getParam('有无计划'),
                showcheckmsg: false,
                checkInfo: {},
                orderDefault: "'f_offsite_time'",
                orderFields: {
                  f_offsite_time: 'no'
                },
            }
        },
        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-message', {f_plan_id: row.f_check_plan_id, item: row, role: 'view'}, 'self')
                    } else {
                        this.$goto('paper-message-nocivil', {
                            f_plan_id: row.f_check_plan_id,
                            item: row,
                            role: 'view'
                        }, 'self')
                    }
                }).catch(
                    (respanse) => {
                        alert('获取配置文件失败')
                    })
            },
            readConfig() {
                let gen = asyncReady(this)
                return co(gen)
            },
            showChooser() {
                this.showModal = true
            },
            hidden(){
                this.criteriaShow=!this.criteriaShow
            },
            refresh() {
                this.$refs.paged.$refs.criteria.search()
            },
            ok() {
                this.showModal = false
                if (this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow.id) {
                    this.model.f_plan_id = this.$refs.paged.$refs.criteria.$refs.modal.$children[0].selectedRow.id
                }
            },
            cancel() {
                this.showModal = false
                this.model.f_check_plan = {f_plan_name: '', id: ''}
            },
            print(row) {
                this.showcheckmsg = true
                this.checkInfo = row
            },
            remind() {
                this.$resetpost('api/af-safecheck/sql/getRemind', {data: {condition: ` 1 = 1 and f_filialeid = '${this.$login.f.orgid}'`}}, {
                    resolveMsg: null,
                    rejectMsg: null
                }).then((res) => {

                    if (res.data.length > 0) {
                        let a = 0
                        let b = 0
                        let c = 0

                        for (let row of res.data) {
                            let bb = JSON.parse(row.f_last_check_result)
                            if (!bb.result) {
                                bb.result = ''
                            }
                            row.danger = bb
                            if (row.f_last_check_result.indexOf('A')) {
                                a = a + 1
                                console.log('包含A')
                                if (row.nocheckday > 5 && row.nocheckday <= 7) {
                                    b = b + 1
                                    row.color = 'yellow'
                                } else if (row.nocheckday > 7) {
                                    c = c + 1
                                    row.color = 'red'
                                }
                                continue
                            }
                            if (row.f_last_check_result.indexOf('B')) {
                                a = a + 1
                                console.log('包含B')
                                if (row.nocheckday > 12 && row.nocheckday <= 14) {
                                    b = b + 1
                                    row.color = 'yellow'
                                } else if (row.nocheckday > 14) {
                                    c = c + 1
                                    row.color = 'red'
                                }
                                continue
                            }
                            if (row.f_last_check_result.indexOf('C')) {
                                a = a + 1
                                console.log('包含C')
                                if (row.nocheckday > 28 && row.nocheckday <= 30) {
                                    b = b + 1
                                    row.color = 'yellow'
                                } else if (row.nocheckday > 30) {
                                    c = c + 1
                                    row.color = 'red'
                                }
                                continue
                            }
                        }
                        this.$showMessage('目前有' + res.data.length + '户存在隐患需要及时安检，其中' + b + '户需要在两天内安检，'
                            + c + '户已超时，未在规定时间内安检，是否立刻前往计划生成页面生成计划？', ['confirm', 'cancel']).then((res1) => {
                            if (res1 === 'confirm') {
                                console.log('查询到的数据', res)
                                this.$goto('create-plan', {rowsdata: res.data}, 'self', null)
                            }
                        })

                    }


                })
            },
            sort (field, rule) {
              // 将所有排序方式设为不排序，实现相互排斥
              for (let key in this.orderFields) {
                if (key === field) {
                  this.orderFields[key] = rule
                } else {
                  this.orderFields[key] = 'no'
                }
              }
              // 如果新规则不排序，还原为默认排序
              if (rule === 'no') {
                this.model.paramSource.orderitem = `'${this.orderDefault}'`
              } else {
                this.model.paramSource.orderitem = `'${field} ${rule}'`
              }
              this.search()
            },
            search() {
              this.$refs.paged.$refs.criteria.search()
            },
          selfSearch(args){
              //console.log("9999999999999999")
              args.condition+=" and f_check_type='民用' and f_safecheck_type='送气类型'"
            this.model.search(args.condition, args.model, args.condValue)
          }
        },

        ready() {
            console.log('这里是PaperList', this.$login.f)
            this.function = this.f
            console.log(this.f)
            //this.remind()
            let gen = select(this)
            co(gen)
        },
        watch: {
            'model.f_no_checkplan'(val) {
                if (this.model.f_no_checkplan == '有计划安检') {
                    this.showChooser()
                }
            },
          'model.condition'(val) {
              if(val){
                this.searchData.condition = val
              }

          },
          'model.f_entry'(val) {
              if(val){
                this.searchData.f_entry = val
              }
          },
          'model.f_plan_id'(val) {
            if(val){
              this.searchData.f_plan_id = val
            }
          },
          'model.f_approved'(val) {
            if(val){
              this.searchData.f_approved = val
            }
          },
          'model.f_no_checkplan'(val) {
            if(val){
              this.searchData.f_no_checkplan = val
            }
          },
        }

    }
</script>
