<template>
  <div id="unit" class="flex-row">
    <div class="basic-main">
      <div class="flex">
        <criteria-paged :model="model" v-ref:paged>
          <criteria partial='criteria' @condition-changed='$parent.selfSearch' v-ref:cri >
            <div novalidate class="form-horizontal select-overspread container-fluid auto" partial>
              <div class="row">
                <div class="col-sm-2  form-group">
                  <label for="f_userinfo_code" class="font_normal_body">用户编号</label>
                  <input id="f_userinfo_code" class="input_search" style="width:60%" placeholder='用户编号' type="text"
                         v-model="model.f_userinfo_code" condition="f_userinfo_code like '%{}%'">
                </div>
                <div class="col-sm-2  form-group">
                  <label for="f_user_name" class="font_normal_body">用户姓名</label>
                  <input id="f_user_name" class="input_search" style="width:60%" placeholder='用户姓名' type="text"
                         v-model="model.f_user_name" condition="f_user_name = '{}'">
                </div>
                <div class="col-sm-2  form-group">
                  <label for="f_address" class="font_normal_body">用户地址</label>
                  <input id="f_address" class="input_search" style="width:60%" placeholder='用户地址' type="text"
                         v-model="model.f_address" condition="a.f_address like '%{}%'">
                </div>
                <div class="col-sm-2  form-group">
                  <label for="f_user_type" class="font_normal_body"  title="参数:用户类型">&ensp;用户类型</label>
                  <v-select :value.sync="model.f_user_type"
                            v-model="model.f_user_type"
                            :options='$parent.$parent.usertypes' placeholder='用户类型'
                            condition="f_user_type = '{}'"
                            close-on-select></v-select>
                </div>
                <div class="span" style="float:right;">
                  <button class="button_search button_spacing" @click="search()">查询</button>
                  <button  class="button_clear button_spacing" @click="$parent.$parent.selectInputtor">修改抄表员</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="row" v-show="$parent.$parent.criteriaShow">
<!--                <div class="col-sm-2  form-group">-->
<!--                  <label for="area" class="font_normal_body">小区&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>-->
<!--                  <input id="area" class="input_search" style="width:60%" placeholder='小区' type="text"-->
<!--                         v-model="model.f_residential_area" condition="f_residential_area like '%{}%'">-->
<!--                </div>-->
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">&nbsp;&nbsp;&nbsp;小区&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                  <v-select :value.sync="model.f_residential_area"
                            class="select_list select"
                            enter-push
                            v-model="model.f_residential_area"
                            style="width: 60%"
                            multiple = "true"
                            :options='$parent.$parent.residentialArea' placeholder='选择小区'
                            close-on-select v-el:cc>
                  </v-select>
                </div>
                <div class="col-sm-2  form-group">
                  <label for="f_print" class="font_normal_body" style="text-align: center">抄表员&nbsp;&nbsp;&nbsp;</label>
                  <v-select :value.sync="model.f_inputtor"
                            v-model="model.f_inputtor"
                            :options='$parent.$parent.inputtores' placeholder='请选择'
                            condition="f_inputtor = '{}'"
                            close-on-select></v-select>
                </div>
                <div class="col-sm-2  form-group">
                  <label for="f_building" class="font_normal_body">楼栋&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                  <input id="f_building" class="input_search" style="width:60%" placeholder='楼栋' type="text"
                         v-model="model.f_building" condition="a.f_building like '%{}%'">
                </div>
                <div class="col-sm-2  form-group">
                  <label for="f_unit" class="font_normal_body">单元&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                  <input id="f_unit" class="input_search" style="width:60%" placeholder='单元' type="text"
                         v-model="model.f_unit" condition="a.f_unit like '%{}%'">
                </div>
                <div class="col-sm-2  form-group">
                  <label for="f_position" class="font_normal_body"  title="参数:安装位置">&ensp;安装位置</label>
<!--                  <v-select :value.sync="row.f_position" v-model="row.f_position"-->
<!--                            :options='positions' placeholder='安装位置'-->
<!--                            close-on-select></v-select>-->
                  <v-select :value.sync="model.f_position"
                            v-model="model.f_position"
                            :options='$parent.$parent.positions' placeholder='安装位置'
                            condition="f_position = '{}'"
                            close-on-select></v-select>
                </div>
              </div>
            </div>
          </criteria>

          <data-grid :model="model" partial='list' class="list_area table_sy" v-ref:grid>
            <template partial='head'>
              <tr>
<!--                <th style="padding: 0;border: 0">-->
<!--                  <input type="checkbox" id="f_operator" :checked="$parent.$parent.$parent.isall()"-->
<!--                         @change="$parent.$parent.$parent.select()" style="width: 22px;height: 22px"/></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;padding: 0;border: 0">-->
<!--                <input type='checkbox' @change="$parent.$parent.$parent.selectOne($event,row,$index)"-->
<!--                       :checked="$parent.$parent.$parent.all||$parent.$parent.$parent.ischecked(row)"-->
<!--                       style="width: 22px;height: 22px;"/>-->
<!--              </td>-->
              <td style="text-align: center;">
                <nobr>{{row.f_userinfo_code}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_user_name}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_residential_area}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_address}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_position}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_inputtor}}</nobr>
              </td>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>
      </div>
    </div>
    <modal :show.sync="uploadshow" v-ref:modal backdrop="false" width="50%">
      <header slot="modal-header" class="modal-header">
<!--        <button type="button" class="close" @click="close"><span>&times;</span></button>-->
        <h4 class="modal-title">抄表员修改</h4>
      </header>
      <article slot="modal-body" class="modal-body">
          <form class="form-horizontal">
          <div class="row auto">
            <div class="col-sm-4">
            <label class="font_normal_body">抄表员</label>
              <v-select :value.sync="inputtorModel.inputtor" v-model='inputtorModel.inputtor'
                        :options='inputtores' placeholder='抄表员'
                        close-on-select></v-select>
            </div>
            <div class="span" style="float:right;">
              <button class="button_search button_spacing" type="button" @click="close">取消</button>
              <button class="button_search button_spacing" type="button" @click="confirm">确定</button>
            </div>
          </div>
          </form>
      </article>
      <footer slot="modal-footer" class="modal-footer">
      </footer>
    </modal>
  </div>

</template>

<script>
  import {PagedList} from 'vue-client'
  import {HttpResetClass} from 'vue-client'

  let readySomething = async function (self) {
    self.$refs.paged.$refs.cri.search()
  }
  export default {
    title: '批量修改抄表员',
    data () {
      return {
        f_orgid: this.$login.f.orgid,
        model: new PagedList('rs/sql/sale_getUserFilesInputtor', 20),
        all: false,
        positions: this.$appdata.getParam('安装位置'),
        usertypes: [{label: '全部', value: ''}, ...this.$appdata.getParam('用户类型')],
        rowsdata: [],
        area: [],
        residentialArea: [],
        selecteddata: [],
        criteriaShow: false,
        length: 99999999,
        uploadshow: false,
        inputtorModel: {
          inputtor: []
        },
        condition: '1=1'
      }
    },
    ready () {
      this.getaddress()
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      async  getaddress () {
        console.log('开始获取小区')
        let HttpReset = new HttpResetClass()
        var data = await HttpReset.load('POST', 'rs/sql/manage_getarealist', {
          data: {
            condition: `1=1 and s.f_filialeid = '${this.$login.f.orgid}'`
          }
        }, {resolveMsg: null, rejectMsg: '获取小区失败!'})
        console.log('小区', data)
        let house = []
        house.push()
        for (let row of data.data) {
          // console.log('开始保存小区')
          house.push({label: row.f_residential_area, value: row.f_residential_area})
        }
        this.residentialArea = house
      },
      hidden () {
        this.criteriaShow = !this.criteriaShow
      },
      close () {
        this.uploadshow = false
      },
      async selectInputtor () {
        this.uploadshow = true
      },
      selfSearch (args) {
        this.orgCondtionStr = `and uf.f_filialeid = '${this.f_orgid}'`

        args.condition = `${args.condition} ` + this.orgCondtionStr
        this.area = this.$refs.paged.$refs.cri.model.f_residential_area
        console.log('=====小区model绑定====', this.area)
        if (this.area.length !== 0) {
          let str = JSON.stringify(this.area)
          str = str.replace(/"/g, `'`)
          str = str.replace(/\[/g, ``)
          str = str.replace(/\]/g, ``)
          console.log('=====小区model22222222222绑定====', str)
          // 查询多个小区时条件
          args.condition += ` and f_residential_area in  ( ${str} )`
        }
        this.condition = args.condition
        this.model.search(args.condition, args.model)
        // this.all = false
        // this.rowsdata = []
        // this.selecteddata = []
        // this.length = 99999999
      },
      // async select() {
      //   if(this.all){
      //     this.selecteddata = []
      //     this.rowsdata = []
      //     this.all = false
      //     this.length = 99999999
      //   }else {
      //     let HttpReset = new HttpResetClass()
      //     await HttpReset.load('POST', 'rs/sql/sale_getUserFilesInputtor?pageNo=1&pageSize=9999999', {
      //       data: {
      //         condition: `1=1 and uf.f_filialeid = ` + this.f_orgid
      //       }
      //     }, {resolveMsg: null, rejectMsg: '全选获取数据异常，请重试!'}).then((req) => {
      //       this.selecteddata = []
      //       this.rowsdata = []
      //       req.data.forEach((row, n) => {
      //         this.selecteddata.push(row.f_userfiles_id)
      //         this.rowsdata.push({
      //           f_userfiles_id: row.f_userfiles_id,
      //         })
      //       })
      //       this.all = true
      //     })
      //   }
      // },
      // selectOne(event, row, i) {
      //   console.log(this.length === 99999999,"===========",this.model.count)
      //   if(this.length === 99999999){
      //     this.length = this.model.count
      //   }
      //   if(this.all){
      //     this.all = !this.all
      //   }
      //   if (event.target.checked) {
      //     this.selecteddata.push(row.f_userfiles_id)
      //     this.rowsdata.push({f_userfiles_id:row.f_userfiles_id})
      //     if(this.length === this.selecteddata.length){
      //       this.all = true
      //     }
      //   } else {
      //     this.all = false
      //     this.selecteddata.splice(this.selecteddata.indexOf(row.f_userfiles_id),1)
      //     this.rowsdata.splice(parseInt(this.selecteddata.indexOf(row.f_userfiles_id)),1)
      //   }
      // },
      async confirm () {
        let HttpReset = new HttpResetClass()
        await HttpReset.load('POST', 'rs/logic/sale_updateUserFilesInputtor', {
          data: {
            condition: this.condition,
            f_inputtor: this.inputtorModel.inputtor[0]
          }
        }, {resolveMsg: '修改抄表员成功', rejectMsg: '批量修改抄表员失败，请重试!'}).then((req) => {
          this.$refs.paged.$refs.cri.search()
          this.uploadshow = false
        })
      }
    },
    computed: {
      inputtores () {
        // 获取抄表员
        console.log('获取抄表员', this.$login.f)
        let rs = []
        if (this.$login.f.f_gasman.length > 0) {
          for (let i = 0; i < this.$login.f.f_gasman.length; i++) {
            let temp = {
              label: this.$login.f.f_gasman[i].name,
              value: this.$login.f.f_gasman[i].name
            }
            rs.push(temp)
          }
        }
        return [{label: '全部', value: ''}, ...rs]
      }
      // ischecked(){
      //   return function (row) {
      //     return this.all||this.selecteddata.includes(row.f_userfiles_id)
      //   }
      // },
      // isall(){
      //   return function () {
      //     return this.all
      //   }
      // }
    }
  }
</script>
