<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="form-group col-sm-2" style="margin-bottom: 0px">
                <label class="font_normal_body">公&emsp;&emsp;司</label>
                <right-tree :width="'60%'" @re-res="getRes"></right-tree>
              </div>
              <div class="form-group col-sm-2"  >
                <label class="font_normal_body">标&emsp;&emsp;题</label>
                <input type="text" class="input_search" v-model="model.f_title"
                       style="width: 60%" placeholder="请输入标题" condition="f_title like '{}%'">
              </div>
              <div class="form-group col-sm-2"  >
                <label class="font_normal_body">下&emsp;发&emsp;人</label>
                <input type="text" class="input_search" v-model="model.f_send_people"
                       style="width: 60%" placeholder="请输入下发人" condition="f_send_people like '{}%'">
              </div>
              <div class="form-group col-sm-2" >
                <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_sendtime > ='{} 00:00:00'">
                </datepicker>
              </div>
              <div class="form-group col-sm-2"  >
                <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_sendtime <=  '{} 23:59:59'">
                </datepicker>
              </div>
              <div class="form-group col-sm-2" style="width: auto;margin-top:8px;float: right">
                <button class="button_spacing button_search" @click="search()">查询</button>
                <button class="button_spacing button_search" @click="$parent.$parent.addAno()">新增</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>
            <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>
                <nobr>下发时间</nobr>
              </th>
              <!--<th>
                <nobr>内容</nobr>
              </th>-->
              <th>
                <nobr>操作</nobr>
              </th>
            </tr>
          </template>
          <template partial='body'>

            <td style="text-align: center">{{row.id}}</td>
            <td style="text-align: center"><a @click="$parent.$parent.$parent.selAno(row)">{{row.f_title}}</a></td>
            <td style="text-align: center">{{row.f_send_people}}</td>
            <td style="text-align: center">{{row.f_sendtime}}</td>
            <!--<td style="text-align: center">{{row.f_content}}</td>-->
            <td style="text-align: center">
              <button class="button_spacing button_search-1" style="margin-right: 5px" @click='$parent.$parent.$parent.upAno(row)'>修改</button>
              <button class="button_spacing button_search-1" @click='$parent.$parent.$parent.delete(row)'>删除</button>
            </td>
          </template>
        </data-grid>
      </criteria-paged>
      <check-msg v-if="showcheckmsg" :show="showcheckmsg" :data="checkInfo"></check-msg>
      <modal :show.sync="showModal2" :backdrop="false">
        <div slot="modal-header" class="modal-header">
          <h4 class="modal-title">
            {{modalTitle}}
          </h4>
        </div>
        <div slot="modal-body" class="modal-body">
          <div v-if="true">
            <div class="row" style="margin-top: 10px">
              <label class="control-label font text-left" style="float:left;margin-left:20%;margin-top:5px">标&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题</label>
              <div class="col-xs-8 col-md-8">
                <input type="text" class="input_search" style="width: 70%;" v-model="f_title" />
              </div>
            </div>
            <div class="row" style="margin-top: 10px">
              <label  class="control-label font text-left" style="float:left;margin-left:20%;margin-top:5px">内&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;容</label>
              <div class="col-xs-8 col-md-8">
                <textarea style="width: 70%; height: 200px" v-model="f_content"></textarea>
              </div>
            </div>
          </div>
        </div>
        <div slot="modal-footer" class="modal-footer" style="text-align: center">
          <button class="button_search" style="margin-right: 20px" @click="ok">确认</button>
          <button class="button_search" @click="cancel">取消</button>
        </div>
      </modal>
      <modal :show.sync="showModal3">
        <div slot="modal-header" class="modal-header">
          <h4 class="modal-title">
             公告详情
          </h4>
        </div>
        <div slot="modal-body" class="modal-body">
          <div v-if="true">
            <div class="row" style="margin-top: 10px">
              <label class="control-label font text-left" style="float:left;margin-left:20%;margin-top:5px">标&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;题</label>
              <div class="col-xs-8 col-md-8">
                <input type="text" class="input_search" style="width: 70%;" v-model="selectRow.f_title" readonly />
              </div>
            </div>
            <div class="row" style="margin-top: 10px">
              <label class="control-label font text-left" style="float:left;margin-left:20%;margin-top:5px">下&ensp;发&ensp;人</label>
              <div class="col-xs-8 col-md-8">
                <input type="text" class="input_search" style="width: 70%;" v-model="selectRow.f_send_people" readonly />
              </div>
            </div>
            <div class="row" style="margin-top: 10px">
              <label class="control-label font text-left" style="float:left;margin-left:20%;margin-top:5px">下发时间</label>
              <div class="col-xs-8 col-md-8">
                <input type="text" class="input_search" style="width: 70%;" v-model="selectRow.f_sendtime" readonly />
              </div>
            </div>
            <div class="row" style="margin-top: 10px">
              <label  class="control-label font text-left" style="float:left;margin-left:20%;margin-top:5px">内&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;容</label>
              <div class="col-xs-8 col-md-8">
                <textarea style="width: 70%; height: 200px" v-model="selectRow.f_content" readonly></textarea>
              </div>
            </div>
          </div>
        </div>
        <div slot="modal-footer" class="modal-footer" style="text-align: center">
          <button class="button_search" @click="cancel">取消</button>
        </div>
      </modal>
    </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";
  import BenzAMRRecorder from 'benz-amr-recorder'



  export default {
    title: '公告管理',
    data() {
      let model = new PagedList('rs/sql/FetchAnnouncement', 20, {
        f_filialeids:"this.f_filialeid",
        orderitem: "'id desc'"
      })
      model.f_plan_id = ''
      model.f_approved = ''
      model.f_entry = ''
      model.f_no_checkplan = ''
      model.f_filialeid = `('305')`
      //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 {
        f_title:'',
        f_content:'',
        modalTitle:'',
        rowId:'',
        selectRow:{},
        modalOperator:'',
        audioshow:false,
        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_residential_area': '小区',
          'f_address': '客户地址',
          'f_entry_status': '安检状态',
          'f_offsite_time': '安检时间',
          'f_checker_name': '安检员'
        },
        fun: this.$login.f,
        checkersid: [],
        criteriaShow: false,
        showModal: false,
        showModal2:false,
        showModal3:false,
        showcheckmsg: false,
        checkInfo: {},
        orderDefault: "'f_offsite_time'",
        orderFields: {
          f_offsite_time: 'no'
        },
        sliceArea: [],
        cbc:[]
      }
    },
    methods: {
      getRes(obj) {
        this.model.f_filialeids = this.$login.convertToIn(obj.resids);
        //tag
      },
      showaudio(){
        //tag
        this.audioshow=!this.audioshow
        //tag
      },
      getmeterbook(val){
        this.cbc=[]
        this.cbc.push({
          label:'全部',
          value:''
        })
        if (val==''){

          this.cbcs.forEach(res=>{
            this.cbc.push({
              label:res.f_book_name,
              value:res.id
            })
          })
        }else{
          this.cbcs.forEach(res=>{
            if (res.f_book_slice_area==val){
              this.cbc.push({
                label:res.f_book_name,
                value:res.id
              })
            }
          })
        }
      },
      Mreadibook(val){
        ////tag
        ////tag
        this.sliceArea=[]
        if (val){
          new HttpResetClass().load('POST',`/rs/sql/tel_singleTable_OrderBy`, {data: {
              items: 'name',
              tablename: 't_zone',
              condition: `parentid  in ${val}`,
              orderitem: 'id'
            }}, {resolveMsg: null, rejectMsg: null}).then((res) => {
            this.sliceArea.push({
              label:'全部',
              value:''
            })
            res.data.forEach(ress=>{
              this.sliceArea.push({
                label:ress.name,
                value:ress.name
              })
            })

          })
        }
      },
      getAllMeterBook(val){
        ////tag
        ////tag
        this.cbcs=[]
        if (val){
          new HttpResetClass().load('POST',`/rs/sql/tel_singleTable_OrderBy`, {data: {
              items: 'f_book_name,f_book_slice_area,id',
              tablename: 't_meter_book',
              condition: `f_filiale_id  in ${val}`,
              orderitem: 'id'
            }}, {resolveMsg: null, rejectMsg: null}).then((res) => {
            res.data.forEach(ress=>{
              this.cbcs.push({
                id:ress.id,
                f_book_name:ress.f_book_name,
                f_book_slice_area:ress.f_book_slice_area
              })
            })
            this.cbc=[]
            this.cbc.push({
              label:'全部',
              value:''
            })
            this.cbcs.forEach(res=>{
              this.cbc.push({
                label:res.f_book_name,
                value:res.id
              })
            })
          })
        }
      },
      getRes(obj) {
        this.model.f_filialeid = this.$login.convertToIn(obj.resids);
        this.searchData.f_filialeid = this.$login.convertToIn(obj.resids);
        this.Mreadibook(this.model.f_filialeid)
        this.getAllMeterBook(this.model.f_filialeid)
        //this.$refs.paged.$refs.criteria.search()
      },

      delete(row) {
        this.$showMessage("确定要删除当前公告吗？",['confirm','cancel']).then((res)=>{
          if(res=='confirm'){
            let param={
              operator:"delete",
              id:row.id
            }
            new HttpResetClass().load('POST','/rs/logic/safe_UpdateAnnouncement',{data:param}).then((res)=>{
              this.$showMessage(res.data.msg)
              this.$refs.paged.$refs.criteria.search()
            })
          }
        })
      },
      readConfig() {
        let gen = asyncReady(this)
        return co(gen)
      },
      addAno() {
        this.modalTitle='公告发布'
        this.modalOperator='insert'
        this.showModal2 = true
      },
      upAno(row) {
        this.modalTitle='修改公告'
        this.modalOperator='update'
        this.f_title=row.f_title
        this.f_content=row.f_content
        this.rowId=row.id
        this.showModal2 = true
      },
      selAno(row) {
        this.selectRow=row
        this.showModal3 = true
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      refresh() {
        this.$refs.paged.$refs.criteria.search()
      },
      ok() {
        if(this.modalOperator=='insert'){
          if(!this.f_title){
            this.$showMessage("请输入公告标题！")
            return
          }
          if(!this.f_content){
            this.$showMessage("请输入公告内容！")
            return
          }
          let param={
            operator:"insert",
            f_title:this.f_title,
            f_content:this.f_content,
            f_send_people:'hwq',
            f_orgid:'305',
            f_orgname:'荣成港华燃气公司'
          }
          new HttpResetClass().load('POST','/rs/logic/safe_UpdateAnnouncement',{data:param}).then((res)=>{
            this.$showMessage(res.data.msg)
            this.f_title = ''
            this.f_content = ''
            this.showModal2=false
            this.$refs.paged.$refs.criteria.search()
          })
        }else if(this.modalOperator=='update'){
          if(!this.f_title){
            this.$showMessage("请输入公告标题！")
            return
          }
          if(!this.f_content){
            this.$showMessage("请输入公告内容！")
            return
          }
          let param={
            operator:"update",
            f_title:this.f_title,
            f_content:this.f_content,
            id:this.rowId
          }
          new HttpResetClass().load('POST','/rs/logic/safe_UpdateAnnouncement',{data:param}).then((res)=>{
            this.$showMessage(res.data.msg)
            this.f_title = ''
            this.f_content = ''
            this.showModal2=false
            this.$refs.paged.$refs.criteria.search()
          })
        }
      },
      cancel() {
        this.showModal2 = false
        this.showModal3 = false
        this.f_title = ''
        this.f_content = ''
      },
      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) {
        ////tag
        //args.condition+=" and f_check_type='民用'"
        this.model.search(args.condition, args.model, args.condValue)
      }
    },

    ready() {

    },
    watch: {
      'model.f_no_checkplan'(val) {
        if (this.model.f_no_checkplan){
          this.searchData.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_status = val
        }
      },
      'model.f_plan_id'(val) {
        if (val) {
          this.searchData.f_plan_id = val
        }
      },
      'model.f_slice_area'(val) {
        if (val) {

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

  }
</script>
