<template>
  <div class="flex" id="tooltip">
    <criteria-paged :model="model" v-ref:paged>
      <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri>
       <div novalidate class="form-inline auto" partial>
         <div >
           <input  type="number" class="form-control" v-model="model.f_userinfo_id" placeholder='客户编号'
           condition="f_userinfo_code = '{}'" v-next-el='yhxm'
           :size="model.f_userinfo_code ? model.f_userinfo_code.length  : 4">
         </div>
         <div >
           <input  type="text" class="form-control" v-model="model.f_user_name" placeholder='用户姓名'
           condition="f_user_name like '%{}%'" v-next-el='yhxm'
           :size="model.f_user_name ? model.f_user_name.length  : 4">
         </div>
         <div >
           <datepicker id="f_createdate" placeholder="生成日期"
             v-model="model.f_createdate"
             :value.sync="model.f_createdate"
             :disabled-days-of-Week="[]"
             condition="convert(varchar(10),f_createdate,120) = '{}'"
             :format="'yyyy-MM-dd'"
             :show-reset-button="reset">
          </datepicker>
         </div>
         <div >
           <v-select id="f_state"
             v-model="model.f_state"
             placeholder='发送状态'
             :value.sync="model.f_state"
             :options='$parent.$parent.sendType'
             condition= "f_state = '{}'"
             close-on-select>
           </v-select>
         </div>
         <div >
           <button class="btn btn-success" @click="$parent.$parent.search()" v-el:cba>查询</button>
           <button class="btn btn-success" @click="$parent.$parent.sendMessage()" v-el:cba>发送</button>
           <button class="btn btn-success" @click="$parent.$parent.delete()" v-el:cba>取消发送</button>
           <export-excel :data="{items: 'f_userinfo_id,f_user_name,f_user_phone,f_user_id, f_gas,f_preamount,f_balance,f_arreamount,f_arrears_date,f_create_date,f_send_content,f_send,f_send_date, f_filiale,f_outlets',
           orderitem: 'id',tablename: 't_sms',
           condition: condition}" bean='短信详情导出' sqlurl="rs/logic/exportfile"></export-excel>
         </div>
       </div>
      </criteria>
      <data-grid  :model="model" partial='list' v-ref:grid>
        <template partial='head'>
          <tr>
            <th>
              <input type="checkbox"  @click="$parent.$parent.$parent.isAll(model.pageIndex)">
            </th>
             <th><nobr>序号</nobr></th>
             <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">
            <input type="checkbox" v-model="$parent.$parent.$parent.ids"
            :value="row.id">
          </td>
          <td style="text-align:center">{{$index+1}}</td>
          <td style="text-align:center">{{row.f_userinfo_code}}&nbsp;&nbsp;{{row.f_user_name}}&nbsp;&nbsp;{{row.f_phone}}</td>
          <td style="text-align:center"><span title="{{row.f_content}}">{{row.f_content.substring(0,3)}}...</span></td>
          <td style="text-align:center">{{row.f_createdate}}</td>
          <td style="text-align:center">{{row.f_state}}</td>
          <td style="text-align:center">{{row.f_senddate}}</td>
          <td style="text-align:center">{{row.f_operatorname}}</td>
        </template>
      </data-grid>
    </criteria-paged>
  </div>
</template>

<script>
/**
*阶梯水价查询列表
*/
import { PagedList } from 'vue-client'


export default {
  data () {
    return {
      model: new PagedList('rs/sql/singleTable_OrderBy', 20,
       {orderitem: '"' + this.orderitem + '"',
        items: '"' + this.items + '"', tablename: '"' + this.tablename + '"'}),
      f_state: '未发送',
      ids: [],
      pages: []
    }
  },
  props: {
    items: {
      type: String,
      default: '*'
    },
    orderitem: {
      type: String,
      default: 'id desc'
    },
    tablename: {
      type: String,
      default: '(select s.*,u.f_userinfo_code,u.f_user_name from t_sms s,t_userinfo u where s.f_userid=u.f_userinfo_id) t'
    },
    condition: {
      type: String
    }
  },
  ready () {
    this.search()
  },
  methods: {
    delete () {
      let tempcondition = this.model.condition
      if (this.ids.length !== 0) {
        this.model.condition = this.model.condition + 'and id in (' + this.ids + ')'
      }
      this.$resetpost('rs/logic/deleteSms', {condition: this.model.condition}, {resolveMsg: '取消发送成功', rejectMsg: '取消发送失败'}).then((res) => {
        this.$dispatch('success')
      }).catch((error) => {
        this.$dispatch('error', error)
      })
      this.ids = []
      this.model.condition = tempcondition
    },
    search () {
      this.$refs.paged.$refs.cri.search()
    },
    selfSearch (args) {
      // this.condition = ' and convert(varchar(10),f_create_date,120) = ' + "'" + this.$login.toStandardDateString() + "'"
      if (!this.$login.f.parent.parent.name.includes('统一资源管理')) {
        if (args.model.f_state[0] !== '回复') {
          args.condition = `${args.condition} and f_filialeids like '${this.$login.f.orgid}%'`
        }
      }
      // args.condition += this.condition
      this.model.search(args.condition, this.model)
    },
    sendMessage () {
      let tempcondition = this.model.condition
      if (this.ids.length !== 0) {
        this.model.condition = this.model.condition + 'and id in (' + this.ids + ')'
      }
      this.$resetpost('rs/logic/sendSms', {condition: this.model.condition}).then((res) => {
        this.$dispatch('success')
      }).catch((error) => {
        this.$dispatch('error')
      })
      this.ids = []
      this.model.condition = tempcondition
    },
    isAll (page) {
      if (this.pages.includes(page)) { // 已添加,取消动作
        // 从ids中删除本页所有数据的ids
        this.addOrRemove(false)
        this.pages = this.pages.filter((item) => {
          return item !== page
        })
      } else { // 未添加
        this.pages.push(page) // 添加id，添加动作
        this.addOrRemove(true)
      }
    },
    addOrRemove (is) {
      let that = this
      if (is) { // 添加本页所有数据到ids中
        this.model.rows.forEach((row) => {
          if (!that.ids.includes(row.id)) {
            that.ids.push(row.id)
          }
        })
      } else { // 从ids中取消所有的数据
        this.model.rows.forEach((row) => {
          that.ids = that.ids.filter((item) => {
            return item !== row.id
          })
        })
      }
    }
  },
  computed: {
    sendType () {
      return [{label: '全部', value: ''}, ...this.$appdata.getParam('发送状态')]
    }
  }
}
</script>
<style>
  #tooltip {
    height: auto;
  }
</style>
