<template>
  <div id="unit" class="flex-row">
    <div class="basic-main" @keyup.enter="search">
      <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">
                <res-select-group :initres="$parent.$parent.initres" @re-res="$parent.$parent.getRes"
                                  v-ref:sel></res-select-group>

                <div class="span" style="float:right;">
                  <button v-if="this.$login.r.includes('批量停用')" class="button_search button_spacing"  @click="$parent.$parent.batch_audit()">批量修改</button>
                  <button class="button_search button_spacing" @click="search()">查询</button>
                  <button class="button_clear  button_spacing" @click="$parent.$parent.clear()">清空</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 class="font_normal_body">客户编号</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_userinfo_code"
                         condition="f_userinfo_code = '{}' " placeholder="客户编号">
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户名称</label>
                  <input type="text" style="width:60%" class="input_search" v-model="model.f_user_name"
                         condition="f_user_name = '{}'" placeholder="客户名称">
                </div>

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

                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">客户类型</label>
                  <v-select :value.sync="model.f_user_type"
                            @change="$parent.$parent.userTypeChange()"
                            :options='$parent.$parent.usertypes' placeholder='请选择' v-model="model.f_user_type"
                            condition="f_user_type = '{}'"
                            :value-single="true"
                            close-on-select></v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">用气性质</label>
                  <v-select :value.sync="model.f_gasproperties" v-model="model.f_gasproperties"
                            :options='$parent.$parent.gasproperties' placeholder='请选择'
                            condition="f_gasproperties = '{}'"
                            close-on-select></v-select>
                </div>
                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">停用原因</label>
                  <v-select :value.sync="model.f_comments" v-model="model.f_comments"
                            :options='$parent.$parent.comments' placeholder='请选择'
                            condition="da.f_comments = '{}'"
                            close-on-select></v-select>
                </div>

                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">是否欠费</label>
                  <v-select :value.sync="model.f_pay_state" v-model="model.f_pay_state"
                            :options='$parent.$parent.paystate' placeholder='请选择'
                            condition="da.f_pay_state = '{}'"
                            close-on-select></v-select>
                </div>

                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">是否长期未安检</label>
                  <v-select :value.sync="model.f_check_day" v-model="model.f_check_day"
                            :options='$parent.$parent.checkState' placeholder='请选择'
                            condition="f_check_day > '{}'"
                            close-on-select></v-select>
                </div>

                <div class="col-sm-2 form-group">
                  <label class="font_normal_body">用户状态</label>
                  <v-select :value.sync="model.f_table_state" v-model="model.f_table_state"
                            :options='$parent.$parent.tablestate' placeholder='请选择'
                            condition="da.f_table_state = '{}'"
                            close-on-select></v-select>
                </div>

                <div class="col-sm-2 form-group">
                  <label for="startDate" class="font_normal_body" title="开始日期">停用日期</label>
                  <datepicker id="startDate" placeholder="开始日期" style="width:60%"
                              v-model="model.startDate"
                              :value.sync="model.startDate"
                              :format="'yyyy-MM-dd HH:mm:ss'"
                              condition="da.f_operate_date >= '{}' "
                              :show-reset-button="true">
                  </datepicker>
                </div>
                <div class="col-sm-2 form-group">
                  <label for="endDate" class="font_normal_body" title="结束日期">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                  <datepicker id="endDate" placeholder="结束日期" style="width:60%"
                              v-model="model.endDate"
                              :value.sync="model.endDate"
                              condition="da.f_operate_date <= '{}' "
                              :format="'yyyy-MM-dd HH:mm:ss'"
                              :show-reset-button="true">
                  </datepicker>
                </div>
              </div>
            </div>
          </criteria>

          <data-grid :model="model" partial='list' class="list_area table_sy" :optional="true" 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>
                <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">
                <nobr>{{$index+1}}</nobr>
              </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_user_phone}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_address}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_user_type}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_gasproperties}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_table_state}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_comments}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_operate_date!=='1900-01-01 00:00:00' ? row.f_operate_date : ''}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_pay_state}}</nobr>
              </td>
              <td style="text-align: center;">
                <nobr>{{row.f_oughtfee}}</nobr>
              </td>
            </template>
            <template partial='foot'></template>
          </data-grid>
        </criteria-paged>

        <modal v-if="tempfalg" :show.sync="tempfalg" v-ref:modal backdrop="false">
          <header slot="modal-header" class="modal-header">
            <h4 class="modal-title">批量停用</h4>
          </header>
          <article slot="modal-body" class="modal-body">
            <validator name='v'>
                <form class="form-horizontal select-overspread">
                  <div class="form-group">
                    <label class=" col-sm-3 control-label">停用类型:</label>
                    <div class="col-sm-4">
                        <v-select v-model="f_disable_type"
                                  placeholder='请选择'
                                  :value.sync="disableModel.f_disable_type"
                                  :options='disableTypes'
                                  :value-single="true"
                                  close-on-select clear-button>
                        </v-select>
                    </div>
                  </div>
                  <div class="form-group">
                    <label class=" col-sm-3 control-label">停用原因:</label>
                    <div class="col-sm-4">
                      <v-select v-model="f_comments"
                                placeholder='请选择'
                                :value.sync="disableModel.f_comments"
                                :options='comments'
                                :value-single="true"
                                close-on-select clear-button>
                      </v-select>
                    </div>
                  </div>

                  <div class="form-group">
                    <label class=" col-sm-3 control-label">备注:</label>
                    <div class="col-sm-8">
                    <textarea class="form-control" v-model="disableModel.f_othereason" rows="3"
                              placeholder="备注" ></textarea>
                    </div>
                  </div>
<!--                <div class="form-group">-->
<!--                  <label class="col-sm-3 control-label">时间周期:</label>-->
<!--                  <div class="form-group col-sm-3">-->
<!--                      <datepicker-->
<!--                        id="startDate1" placeholder="开始日期" style="width:100%"-->
<!--                        v-model="disableModel.startDate"-->
<!--                        :value.sync="disableModel.startDate"-->
<!--                        :format="'yyyy-MM-dd HH:mm:ss'"-->
<!--                        :show-reset-button="true"-->
<!--                      >-->
<!--                      </datepicker>-->
<!--                  </div>-->
<!--                  <label class="col-sm-1 control-label">&nbsp;&nbsp;&nbsp;至&nbsp;&nbsp;&nbsp;</label>-->
<!--                  <div class="form-group col-sm-3">-->

<!--                    <datepicker-->
<!--                      id="endDate1" placeholder="结束日期" style="width:100%"-->
<!--                      v-model="disableModel.endDate"-->
<!--                      :value.sync="disableModel.endDate"-->
<!--                      :format="'yyyy-MM-dd HH:mm:ss'"-->
<!--                      :show-reset-button="true"-->
<!--                    >-->
<!--                    </datepicker>-->
<!--                  </div>-->
<!--                </div>-->

<!--                <div v-if="disableModel.f_disable_type === '欠费'" class="form-group">-->
<!--                  <label class=" col-sm-3 control-label">总金额:</label>-->
<!--                  <div class="col-sm-4">-->
<!--                    <input class="form-control" v-model="disableModel.f_totalcost" rows="3"-->
<!--                           disabled-->
<!--                           placeholder="总金额" />-->
<!--                  </div>-->
<!--              </div>-->

              </form>
            </validator>
          </article>
          <footer slot="modal-footer" class="modal-footer">
            <button v-show="tempfalg" type="button" class="button_search" @click='confirm()'>确认</button>
            <button v-show="tempfalg" type="button" class="button_clear" @click='close'>取消</button>
          </footer>
        </modal>
      </div>
    </div>
  </div>
</template>

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

  let readySomething = async function (self) {
    await self.$LoadParams.loadParam()
    self.initParams()
  }
  export default {
    title: '批量停用',
    data() {
      return {
        gasproperties: [],
        showinfo: false,
        data: {},
        show: false,
        rowdata: {},
        initres: {
          org: [this.$login.f.orgid],
          dep: [],
          user: []
        },
        model: new PagedList('rs/sql/batchDisableQuery', 20, {
          startDate: 'this.model.startDate',
          endDate: 'this.model.endDate'
        }),
        resdata: {
          depresid: [],
          userresid: [],
          f_orgid: this.$login.f.orgid,
          f_depid: '',
          f_operatorid: '',
          operatorid: '',
          depid: '',
          orgname: '',
          depname: '',
          operatorname: ''
        },
        criteriaShow: false,
        orgCondtionStr: '',
        // 下拉框
        meterbrands: [],
        prices: [],
        disableModel: {
          f_disable_type: '欠费',
          startDate: '',
          endDate: '',
          f_totalcost: '',
          f_comments: '',
          f_othereason:''
        },
        tempfalg: false,
        flag: false
      }
    },
    ready() {
      readySomething(this).then(() => {
        this.$emit('ready')
      }).catch((error) => {
        this.$emit('error', error)
      })
    },
    methods: {
      search() {
        this.$refs.paged.$refs.cri.search()
      },
      batch_audit () {
        if (this.$refs.paged.$refs.grid.getRowData().length !== 0) {
          this.tempfalg = true
          this.disableModel.startDate = this.$login.toStandardYearMonth() + '-01 00:00:00'
          this.disableModel.endDate = this.$login.toStandardDateString() + ' 23:59:59'
        }
      },
      close () {
        this.tempfalg = false
      },
      async confirm () {
        this.tempfalg = false
        console.log('model----',this.$refs.paged.$refs.grid.getRowData())
        let param = []
        this.$refs.paged.$refs.grid.getRowData().forEach((item) => {
          let temp = {}
          temp.f_userinfo_id = item.f_userinfo_id
          temp.f_user_name = item.f_user_name
          temp.f_disable_type = this.disableModel.f_disable_type
          temp.f_comments = this.disableModel.f_comments
          temp.f_othereason = this.disableModel.f_othereason
          temp.f_operator= this.$login.f.name
          temp. f_operatorid= this.$login.f.id
          temp. f_orgid= this.$login.f.orgid
          temp.f_orgname= this.$login.f.orgs
          temp. f_depid= this.$login.f.depids
          temp. f_depname= this.$login.f.deps
          param.push(temp)
        })
        console.log(param)
        await this.$resetpost('rs/logic/owe_disable', {data: param}, {
          rejectMsg: '同步失败',
          resolveMsg: '同步成功'
        })
        await this.search()
      },
      cancel() {
        this.flag = false
      },
      showmsg(obj) {
        this.rowdata = obj
        this.show = true
      },
      userTypeChange() {
        this.gasproperties = []
        if (this.$refs.paged.$refs.cri.model !== null) {
          this.$refs.paged.$refs.cri.model.f_gasproperties = ''
          this.gasproperties = this.$appdata.getParam(this.$refs.paged.$refs.cri.model.f_user_type)
        } else {
          this.gasproperties = [{label: '全部', value: ''}]
        }
      },
      async arrearsQuery () {
        let param = {
          startDate: this.disableModel.startDate,
          endDate: this.disableModel.endDate,
          f_orgid: this.$login.f.orgid
        }
        await this.$resetpost('rs/sql/sale_owefees', {data: param}, {
          resolveMsg: null,
          rejectMsg: null
        }).then((res) => {
          if (res && res.data) {
            this.disableModel.f_totalcost = res.data[0].f_oughtfee
          }
        })
      },
      initParams() {
        // 初始化气表品牌
        let brandArr = []
        this.$GetSaleParam.getGasbrand().forEach((item) => {
          let temp = {}
          temp.label = item.label
          temp.value = item.value.id
          brandArr.push(temp)
        })
        this.meterbrands = [{label: '全部', value: ''}, ...brandArr]
        // 初始化气表价格
        this.prices = this.$GetSaleParam.getPrice(this.$login.f.orgid)
      },
      selfSearch(args) {
        args.condition = args.condition + this.orgCondtionStr
        this.model.search(args.condition, args.model)
      },
      clear() {
        // 清空部门和人员
        this.$refs.paged.$refs.cri.$refs.sel.$children[1].selectres = []
        // 部门和人员变为全选
        this.$refs.paged.$refs.cri.$refs.sel.$children[1].$children[0].isSelectAll = false
        this.$refs.paged.$refs.cri.$refs.sel.$children[2].$children[0].isSelectAll = false
        Object.keys(this.$refs.paged.$refs.cri.model).forEach((key) => {
          this.$refs.paged.$refs.cri.model[key] = []
        })
      },
      hidden() {
        this.criteriaShow = !this.criteriaShow
      },
      async getRes (condition, obj) {
        this.orgCondtionStr = condition
        this.orgname = obj.orgnames[0]
        this.depname = obj.depnames[0]
      },
      getRes1 (obj) {
        this.resdata.orgname = obj.res[0]
        this.resdata.depresid = obj.resids
        this.resdata.f_orgid = obj.resids
        this.resdata.userresid = obj.resids
      },
      getdep (obj, val) {
        this.resdata.depname = val
        this.resdata.f_depid = obj
      },
      getuser (obj, val) {
        this.resdata.operatorname = val
        this.resdata.f_operatorid = obj
      }
    },
    events: {},
    watch: {
      'disableModel.startDate' (val) {
        if (this.disableModel.f_disable_type === '欠费') {
          this.arrearsQuery()
        }
      },
      'disableModel.endDate' (val) {
        if (this.disableModel.f_disable_type === '欠费') {
          this.arrearsQuery()
        }
      }
    },
    computed: {
      getCondition() {
        return {
          startDate: this.$refs.paged.$refs.cri.model.startDate,
          endDate: this.$refs.paged.$refs.cri.model.endDate,
          condition: `${this.$refs.paged.$refs.cri.condition}` + this.orgCondtionStr,
          condValue: `${this.model.condValue}`
        }
      },
      usertypes() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('用户类型')]
      },
      disableTypes () {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('停用类型')]
      },
      comments () {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('停用原因')]
      },
      paystate () {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('是否欠费')]
      },
      checkState () {
        return [{label: ' 全部 ', value: ''}, {label: ' 是 ', value: this.$appdata.getSingleValue('是否长期未安检')}]
      },
      tablestate () {
        return [{label: ' 全部 ', value: ''}, {label: '正常', value: '正常'}, {label: '停用', value: '停用'}]
      },
      metertypes() {
        return [{label: ' 全部 ', value: ''}, ...this.$appdata.getParam('气表类型')]
      }
    }
  }
</script>
