<template >
  <div class="flex-row">
    <div @keyup.enter="search1" class="basic-main">
      <criteria-paged :model="model" v-ref:paged>
        <criteria @condition-changed='$parent.search' partial='criteria' v-ref:criteria>
          <div class="form-horizontal select-overspread container-fluid auto" novalidate partial @keyup.enter="search()">
            <div class="row">
              <div class="col-sm-2">
                <res-select-group :show-component="['company']" :initres="$parent.$parent.org" @re-res="$parent.$parent.getorg" style="width:60%"></res-select-group>
              </div>
              <div class="col-sm-2 form-group ">
                <label class="font_normal_body">客户编号</label>
                <input
                       class="input_search" condition="f_userinfo_code = '{}'"
                       placeholder='客户编号'
                       style="width:60%"
                       type="text"
                       v-model="model.f_userinfo_code">
              </div>

              <div class="col-sm-2 form-group">
                <label class="font_normal_body">客户名称</label>
                <input
                       class="input_search" condition="f_user_name like '%{}%'"
                       placeholder='客户名称'
                       style="width:60%"
                       type="text"
                       v-model="model.f_user_name"
                >
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">客户地址</label>
                <input  class="input_search"
                       condition="f_address like '%{}%'"
                       placeholder='客户地址'
                       style="width:60%"
                       type="text"
                       v-model="model.f_address"
                >
              </div>

              <div style="float:right">
                <button @click="search()" class="button_search" v-el:cba>查询</button>
                <button @click="$parent.$parent.clearmsg" class="button_clear">清空</button>
                <export-excel
                  :choose-col="true"
                  :data="{condition: $parent.$parent.condition,
                          f_orgid: $parent.$parent.f_orgid,
                          f_start_date: $parent.$parent.f_start_date,
                          f_end_date: $parent.$parent.f_end_date
                          }"
                  :header="$parent.$parent.other"
                  :field="$parent.$parent.getfield" sql-name="manage_getNewQueryInstruct" sqlurl="api/af-revenue/logic/openapi/exportfile"
                  template-name='指令查看'></export-excel>
                <div
                  :class="{'button_shrink_top':$parent.$parent.criteriaShow,'button_shrink_bottom':!$parent.$parent.criteriaShow}"
                  @click="$parent.$parent.hidden()" class="button_spacing" style="float: right"></div>
              </div>
            </div>
            <div class="row" v-show="$parent.$parent.criteriaShow">

<!--              <div class="col-sm-2">-->
<!--                <label class="font_normal_body">组织机构</label>-->
<!--                <res-select :initresid="$parent.$parent.org"-->
<!--                            style="width:60%"-->
<!--                            @res-select="$parent.$parent.getorg"-->
<!--                            restype='organization'>-->
<!--                </res-select>-->

<!--              </div>-->

              <!--                <div class="col-sm-2 form-group">-->
              <!--                  <label class="font_normal_body">客户类型</label>-->
              <!--                  <v-select style="width:60%" :value.sync="model.f_user_type" v-model="model.f_user_type"-->
              <!--                            placeholder='客户类型'-->
              <!--                            value-single="true"-->
              <!--                            :options='$parent.$parent.userType'-->
              <!--                            close-on-select-->
              <!--                            condition="f_user_type ='{}'"-->
              <!--                  ></v-select>-->
              <!--                </div>-->
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">用气性质</label>
                <v-select style="width:60%" :value.sync="model.f_gasproperties" v-model="model.f_gasproperties"
                          placeholder='用气性质'
                          value-single="true"
                          :options='$parent.$parent.Gasproperties'
                          close-on-select
                          condition="f_gasproperties = '{}'"
                ></v-select>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">表&ensp;&ensp;&ensp;&ensp;号</label>
                <input
                  class="input_search" condition="f_meternumber = '{}'"
                  placeholder='表号'
                  style="width:60%"
                  type="text"
                  v-model="model.f_meternumber">
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">气表品牌</label>
                <v-select
                  :options='$parent.$parent.meterbrand'

                  :value.sync="model.f_gasbrand"
                  close-on-select
                  condition="f_meter_brand='{}'"
                  placeholder='气表品牌'
                  style="width:60%"
                  v-model="model.f_gasbrand">
                </v-select>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">指令类型</label>
                <v-select :options='$parent.$parent.instructtype' :value.sync="model.f_instruct_type" close-on-select
                          condition="f_instruct_type='{}'"
                          placeholder='指令类型'
                          style="width:60%"
                          v-model="model.f_instruct_type"></v-select>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">执行状态</label>
                <v-select :options='$parent.$parent.instructstate' :value.sync="model.f_instruct_state" close-on-select
                          condition="f_instruct_state='{}'"
                          placeholder='执行状态'
                          style="width:60%"
                          v-model="model.f_instruct_state"
                ></v-select>
              </div>

              <div class="col-sm-2 form-group">
                <label class="font_normal_body">指令时间</label>
                <datepicker :format="'yyyy-MM-dd HH:mm:ss'" :value.sync="model.f_start_date"
                            condition="f_instruct_date >= '{}'"
                            placeholder="起始时间"
                            style="width:60%"
                            v-model="model.f_start_date"
                ></datepicker>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">&emsp;&emsp;至&emsp;</label>
                <datepicker :format="'yyyy-MM-dd HH:mm:ss'" :value.sync="model.f_end_date"
                            condition="f_instruct_date <= '{}'"
                            placeholder="结束时间"
                            style="width:60%"
                            v-model="model.f_end_date"
                ></datepicker>
              </div>
              <div class="col-sm-2 form-group">
                <label class="font_normal_body">指令标题</label>
                <v-select :options='$parent.$parent.instructtitle' :value.sync="model.f_instruct_title" close-on-select
                          condition="f_instruct_title='{}'"
                          placeholder='指令标题'
                          style="width:60%"
                          v-model="model.f_instruct_title"></v-select>
              </div>

              <div class="col-sm-2 form-group">
                <label class="font_normal_body">操&nbsp;作&nbsp;人</label>
                <v-select :options='$parent.$parent.operators' :value.sync="model.f_operator" close-on-select
                          condition="f_operator='{}'"
                          placeholder='操作人'
                          style="width:60%"
                          v-model="model.f_operator"></v-select>
              </div>
            </div>
          </div>
        </criteria>
        <data-grid :model="model" class="list_area table_sy" id="queryinstruct" partial='list' :modelval="$parent.$parent.modelval" v-ref:grid>
          <template partial='head'>
            <tr>
              <th style="text-align:center">
                <nobr>操作</nobr>
              </th>
              <th style="text-align:center">
                <nobr>序号</nobr>
              </th>
              <th style="text-align:center">
                <nobr>客户编号</nobr>
              </th>
              <th style="text-align:center">
                <nobr>客户名称</nobr>
              </th>
              <th style="text-align:center">
                <nobr>客户地址</nobr>
              </th>
              <th style="text-align:center">
                <nobr>表号</nobr>
              </th>
              <th style="text-align:center">
                <nobr>气表品牌</nobr>
              </th>
              <th style="text-align:center">
                <nobr>指令类型</nobr>
              </th>
              <th style="text-align:center">
                <nobr>指令标题</nobr>
              </th>
              <th style="text-align:center">
                <nobr>执行状态</nobr>
              </th>
              <th style="text-align:center"><nobr>执行结果</nobr></th>
              <th style="text-align:center"><nobr>指令生成人</nobr></th>
              <th style="text-align:center"><nobr>指令生成时间</nobr></th>
              <th style="text-align:center"><nobr>指令发送时间</nobr></th>
              <th style="text-align:center"><nobr>上表时间</nobr></th>
              <th style="text-align:center">
                <nobr>充值金额</nobr>
              </th>
              <th style="text-align:center">
                <nobr>操作内容</nobr>
              </th>
              <th style="text-align:center">
                <nobr>操作原因</nobr>
              </th>
              <th style="text-align:center">
                <nobr>操作人(记录)</nobr>
              </th>
              <th style="text-align:center">
                <nobr>操作时间(记录)</nobr>
              </th>
              <th style="text-align:center">
                <nobr>操作类型</nobr>
              </th>
              <th style="text-align:center">
                <nobr>记录状态</nobr>
              </th>
              <th style="text-align:center">
                <nobr>组织机构</nobr>
              </th>
              <th style="text-align:center;">
                <nobr>指令内容</nobr>
              </th>
              <th style="text-align:center">
                <nobr>指令详情</nobr>
              </th>
            </tr>
          </template>
          <template partial='body'>
            <tr>
              <td style="text-align:center" >
                <button v-if="row.f_instruct_state==='执行失败' && $login.r.includes('指令重发')" style=" height: 28px;line-height: 16px;" class="button_search button_spacing width-60" @click.stop="$parent.$parent.$parent.reSend(row)">指令重发</button>
              </td>
              <td style="text-align:center">
                <nobr>{{$index+1}}</nobr>
              </td>
              <td style="text-align:center">
                <span @click="$parent.$parent.$parent.dealmsg(row)"><a>{{row.f_userinfo_code}}</a></span>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_user_name}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_address}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_meternumber}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_meter_brand}}</nobr>
              </td>

              <td style="text-align:center">
                <nobr>{{row.f_instruct_type}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_instruct_title}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_instruct_state}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_receive_state}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_inputtor}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_instruct_date}}</nobr>
              </td>
              <td style="text-align:center"><nobr>{{row.f_send_date}}</nobr></td>
              <td style="text-align:center"><nobr>{{row.f_callback_date}}</nobr></td>
              <td style="text-align:center">
                <nobr>{{row.f_collection}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.note}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_info}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_operator}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_operate_date}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_operate_type}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_state}}</nobr>
              </td>
              <td style="text-align:center">
                <nobr>{{row.f_orgname}}</nobr>
              </td>
              <td style="text-align:center;">
                <nobr>{{row.f_instruct_content}}</nobr>
              </td>
              <td style="text-align:center">
                <button style=" height: 28px;line-height: 16px;" class="button_search button_spacing width-60" @click.stop="$parent.$parent.$parent.instructDetail(row)">指令详情</button>
              </td>
            </tr>
          </template>
        </data-grid>
      </criteria-paged>
    </div>
    <modal :show.sync="detailShow" v-if="detailShow" width="40%" style="width:auto;"  v-ref:modal middle backdrop="false">
      <article slot="modal-body">
        <p class="text-center" style="font-size: 15px" v-html="f_instruct_content"></p>
      </article>
      <footer slot="modal-footer" class="modal-footer">
      </footer>
    </modal>
  </div>
</template>
<script>
  import {PagedList} from 'vue-client'
  import * as Util from '../../Util'
  let readyGen = async function (self) {
    await self.$MagLoadParams.loadParam()
    await self.$MagGetSaleParam.initinputtor()
    self.initQueryParam()
    // self.getinstructtype()
    // self.getinstructtitle()
    // self.getinstructstate()
    await self.$getConfig(self, 'NewQueryInstruct')
    console.log('指令查看config', self.config)
    self.getfield = self.config.excelHeaders
    self.getOperators(self.$login.f.orgid)
    self.sumsmodel = self.$refs.paged.$refs.grid.model.sums;
    // self.reflash()
  }
  export default {
    title: '指令查看',
    data () {
      return {
        model: new PagedList('api/af-revenue/sql/manage_getNewQueryInstruct', 50),
        row: null,
        isLoading: true,
        searchshow: true,
        criteriaShow:false,
        Gasproperties:[],
        meterbrand: [],
        operators:[],
        other: [],
        sumsmodel: {},
        userType:[],
       // instructtype:[],
       // instructstate:[],
        org:{
          org:[this.$login.f.orgid],
          dep:[],
          user:[],
        },
        orgcondition:'',
        show: false,
        condition:'',
        f_orgid:'',
        f_start_date:'',
        f_end_date:'',
        getfield: {},
        config: {
          excelHeaders: {
            'f_userinfo_code': '客户编号','f_address': '客户地址','f_user_name': '客户姓名',
            'f_meternumber': '表号','f_meter_brand': '气表品牌','f_instruct_type': '指令类型',
            'f_instruct_state': '执行状态','f_receive_state': '执行结果','f_inputtor': '指令生成人','f_instruct_date': '指令时间',
            'note':'操作内容','f_info':'操作原因','f_operator':'操作人(记录)','f_operate_date':'操作时间(记录)','f_operate_type':'操作类型','f_state':'操作状态',
            'f_orgname': '组织机构',  'f_instruct_title': '指令标题'
          }
        },
       // instructtitle: [],
        detailShow: false,
        f_instruct_content: '',
        modelval: [
          '序号','客户编号','客户名称','客户地址','表号','气表品牌','指令类型','指令标题','执行状态','执行结果','指令生成人','指令生成时间',
          '指令发送时间','上表时间','充值金额','操作内容','操作原因','操作人(记录)','操作时间(记录)','操作类型','记录状态','组织机构'
        ]
      }
    },
    ready () {
      readyGen(this)
      this.$refs.paged.$refs.criteria.model.f_start_date = Util.toStandardDateString() + ' 00:00:00'
      this.$refs.paged.$refs.criteria.model.f_end_date = Util.toStandardDateString() + ' 23:59:59'
      this.$refs.paged.$refs.grid.modelval = this.modelval
      //this.$refs.paged.$refs.criteria.search()

      // 添加回车键监听
      document.addEventListener('keyup', this.handleKeyUp)
    },
    methods: {
      // 添加处理回车键的方法
      handleKeyUp(e) {
        if (e.key === 'Enter') {
          this.search()
        }
      },
      instructDetail(row){
        if (row.f_instruct_content != '{}') {
          let data = JSON.parse(row.f_instruct_content)
          console.log('看下具体的指令详情===>',data)
          let str = ''
          if (data.hasOwnProperty("Data")) {
            console.log('看下具体的指令详情2===>',data.Data)
            console.log('看下具体的指令详情2===>',typeof data.Data)
            for (let key in data.Data) {
              str =  str + key + ':' + data.Data[key] + '</br>'
            }
          }
          for (let key in data) {
            if (key != 'Data'){
              str =  str + key + ':' + data[key] + '</br>'
            }
          }
          this.f_instruct_content = str
          this.detailShow = true
        }else{
          this.$showMessage('当前指令没有内容！！！', ['confirm'])
        }
      },
      hidden(){
        this.criteriaShow = !this.criteriaShow
      },
      getorg(condition,obj) {
        this.orgcondition = condition.replace('f_orgid','f_orgid')
        this.orgcondition = this.orgcondition.replace('f_depid','f_depid')
        this.orgname = obj.orgnames[0]
      },
      initQueryParam () {
        let arr = []
        this.$MagGetSaleParam.getGasbrand().forEach((item) => {
          let temp = {}
          if(item.value.f_meter_type==='物联网表'){
            temp.label = item.label
            temp.value = item.value.f_meter_brand
            arr.push(temp )
          }

        })
        this.meterbrand = [{label: '全部', value: ''}, ...arr]
      },
      //获取资源数据
       getOperators(orgid) {
        this.operators = []
         this.$MagGetSaleParam.inputtors.forEach((res) => {
          if(res.rolestr != null &&  res.rolestr!='' ){
            if( res.f_orgid == orgid) {
              let temp = {
                label :res.name,
                value:res.name,
              }
              this.operators.push(temp)
            }
          }
        })
      },
      // getinstructtype(){
      //   this.instructtype=[]
      //   let load = new HttpResetClass()
      //   load.load('POST', 'api/af-revenue/sql/singleTableParam', {
      //     data: {
      //       items:'f_instruct_type',
      //       tablename:'t_instruct',
      //       condition:`f_instruct_type is not null and f_instruct_date>='${this.f_start_date}' and f_instruct_date<='${this.f_end_date}' group by f_instruct_type`
      //     }
      //   },{warnMsg:null,resolveMsg:null}).then((res) => {
      //     this.instructtype.push({label: '全部', value: ''})
      //     res.data.forEach((item) => {
      //       this.instructtype.push({label: item.f_instruct_type, value: item.f_instruct_type})
      //     })
      //   })
      //
      // },
      // getinstructtitle(){
      //   this.instructtype=[]
      //   let load = new HttpResetClass()
      //   load.load('POST', 'api/af-revenue/sql/singleTableParam', {
      //     data: {
      //       items:'f_instruct_title',
      //       tablename:'t_instruct',
      //       condition:'f_instruct_title is not null  group by f_instruct_title'
      //     }
      //   },{warnMsg:null,resolveMsg:null}).then((res) => {
      //     this.instructtitle.push({label: '全部', value: ''})
      //     res.data.forEach((item) => {
      //       this.instructtitle.push({label: item.f_instruct_title, value: item.f_instruct_title})
      //     })
      //   })
      //
      // },
      // getinstructstate(){
      //   this.instructstate=[]
      //   let load = new HttpResetClass()
      //   load.load('POST', 'api/af-revenue/sql/singleTableParam', {
      //     data: {
      //       items:'f_instruct_state',
      //       tablename:'t_instruct',
      //       condition:'f_instruct_state is not null  group by f_instruct_state'
      //     }
      //   },{warnMsg:null,resolveMsg:null}).then((res) => {
      //     this.instructstate.push({label: '全部', value: ''})
      //     res.data.forEach((item) => {
      //       this.instructstate.push({label: item.f_instruct_state, value: item.f_instruct_state})
      //     })
      //   })
      //
      // },
      clearmsg () {
        this.$refs.paged.$refs.criteria.model = {}
      },
      async reSend (row) {
        let sql = `update t_instruct set f_instruct_state = '待发送' WHERE id = ${row.id}`
        await this.$resetpost('rs/logic/runSQL', {sql: sql}, {resolveMsg: '操作成功', rejectMsg: '操作失败'})
        this.$refs.paged.$refs.criteria.search()
      },
      clickshow () {
        this.searchshow = !this.searchshow
      },
      dealmsg (val) {
        val.model = this.model.model
        this.$emit('deal-msg', val)
      },
      async search (args) {
        // this.obj.resids = this.obj.resids.replace(/\./g, ''),
        args.condition =  `${args.condition}`+this.orgcondition
        console.log(args.condition+"args.condition")
        this.condition = args.condition
        if( this.$refs.paged.$refs.criteria.model.f_start_date!=null &&  this.$refs.paged.$refs.criteria.model.f_start_date!=''){
          this.model.params.f_start_date = `'${this.$refs.paged.$refs.criteria.model.f_start_date}'`;
          this.f_start_date = `'${this.$refs.paged.$refs.criteria.model.f_start_date}'`;
        }else{
          this.model.params.f_start_date = '';
          this.f_start_date = '';
        }
        if( this.$refs.paged.$refs.criteria.model.f_end_date!=null &&  this.$refs.paged.$refs.criteria.model.f_end_date!=''){
          this.model.params.f_end_date = `'${this.$refs.paged.$refs.criteria.model.f_end_date}'`;
          this.f_end_date = `'${this.$refs.paged.$refs.criteria.model.f_end_date}'`;
        }else{
          this.model.params.f_end_date  = "";
          this.f_end_date  = "";
        }
        if(this.f_orgid_new == '' || this.f_orgid_new == null){
          this.model.params.f_orgid = `'${this.$login.f.orgid}'`;
          this.f_orgid = `${this.$login.f.orgid}`;
          //await this.$LoadParams.loadParam(this.$login.f.orgid)
        }else{
          this.model.params.f_orgid = `'${this.f_orgid_new}'`;
          this.f_orgid = `${this.f_orgid_new}`;
          //await this.$LoadParams.loadParam(this.f_orgid_new)
        }
        await this.model.search(args.condition, args.model)
        await this.initQueryParam();
        await this.$refs.paged.$refs.grid.InitializedData()
        await this.$refs.paged.$refs.grid.displayColumnSettings(undefined)

      },
      getotherfooter() {
        this.other = [];

        let firstLine = [`导出时间: ${this.$login.toStandardTimeString()}`];


        let secondLine = [
          `开始时间: ${this.$refs.paged.$refs.criteria.model.f_start_date}`,
          `结束时间: ${this.$refs.paged.$refs.criteria.model.f_end_date}`
        ];

        this.other.push(firstLine);
        this.other.push(secondLine);

        console.log('Header Data:', this.other);
      },

    },
    watch:{
      sumsmodel:{
        handler: function(val) {
          this.getotherfooter();
        },
        deep: true
      },
    },
    computed: {
      instructtype () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('指令类型')]
      },
      instructstate () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('执行状态')]
      },
      instructtitle () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('指令标题')]
      },
     userType () {
        return [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
       Gasproperties () {
      return [{label: '全部', value: ''}, ...this.$appdata.getParam('用气性质')]
    },
      mounted() {
        this.getotherfooter();
      }


    },
    beforeDestroy() {
      // 组件销毁前移除事件监听
      document.removeEventListener('keyup', this.handleKeyUp)
    }
  }
</script>
<style>
  .form-input-group label {
    text-align: right;
    width: auto;
  }
  .datapanel {
    color: #333;
    background-color: white;
    box-shadow: darkgrey 0.5px 0.5px 0.5px 0.5px ;
    padding: 10px 30px 10px 30px;
    border-radius:15px;
  }
</style>
