<template>
  <div class="flex" style="height: 98%">
    <criteria-paged :model="model" v-ref:paged>
      <criteria @condition-changed='$parent.selfSearch' partial='criteria' v-ref:cri>
        <div class="form-horizontal select-overspread container-fluid auto" novalidate partial>
          <div class="row">
            <!--            <res-select-group :show-component="['company','department','operator']" :initres="$parent.$parent.initres"  @re-res="$parent.$parent.getRes" v-ref:sel></res-select-group>-->

            <div class="form-group col-sm-4" >
              <label class="font_normal_body">组织机构</label>
              <res-select :initresid='$parent.$parent.curorgid'
                          @res-select="$parent.$parent.getorg"
                          class="select select_list"
                          restype='organization'
                          style="width: 60%">
              </res-select>
            </div>
            <div class="form-group col-sm-4" >
              <label class="font_normal_body">规则名称</label>
              <input type="text" class="input_search" style="width:60%" v-model="model.f_rule_name"
                     condition="f_rule_name like '%{}%'" placeholder='规则名称'>
            </div>
            <div class="form-group col-sm-4" style="float:right;white-space: nowrap;width: auto" >
              <button @click="$parent.$parent.search()" class="button_search button_spacing" v-el:cx>查询</button>
              <button @click="$parent.$parent.add()" class="button_new button_spacing">新增</button>
            </div>
          </div>
        </div>
      </criteria>
      <data-grid :model="model" class="list_area table_sy"  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>
            <th>
              <nobr>操作</nobr>
            </th>
            <!--      <th>
                    <nobr>历史记录</nobr>
                  </th> -->
          </tr>
        </template>
        <template partial='body'>
          <tr>
            <td style="text-align:center">{{$index + 1}}</td>
            <td style="text-align:center">{{row.id}}</td>
            <td style="text-align:center">{{row.f_rule_name}}</td>
            <td style="text-align:center"><nobr>{{row.f_day}}</nobr></td>
            <td style="text-align:center"><nobr>{{row.f_orgname}}</nobr></td>
            <td style="text-align:center"><nobr>
              <button @click.stop="$parent.$parent.$parent.operate(row)" class="btn btn-link"><b>{{row.hushu}}</b></button>
            </nobr></td>
            <td style="text-align:center"><nobr>
              <button @click.stop="$parent.$parent.$parent.update(row)"
                      class="btn btn-link">修改
              </button>
            </nobr></td>
          </tr>
        </template>
        <template partial='foot'></template>
      </data-grid>
    </criteria-paged>
  </div>

  <modal v-if="show" :show.sync="show" style="height: auto">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="show=!show"><span>&times;</span></button>
      <h4 class="modal-title">新增违约金规则</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <div class="row">
        <div class="col-sm-4 form-group">
          <label class="font_normal_body">规则名称</label>
          <input type="text" class="input_search" style="width:60%" v-model="inputrow.f_rule_name"
                 @blur="findname" placeholder='规则名称'>
        </div>
        <div class="col-sm-4 form-group">
          <label class="font_normal_body">间隔天数</label>
          <input type="number" class="input_search" style="width:60%" v-model="inputrow.f_day"
                 placeholder='日期'>
        </div>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <button type="button" class="button_new button_spacing" @click="confirm()">生成</button>
      <button type="button" class="button_clear" @click='cancel()'>取消</button>
    </footer>
  </modal>

  <modal v-if="updateshow" :show.sync="updateshow" style="height: auto">
    <header slot="modal-header" class="modal-header">
      <button type="button" class="close" @click="updateshow=!updateshow"><span>&times;</span></button>
      <h4 class="modal-title">新增违约金规则</h4>
    </header>
    <article slot="modal-body" class="modal-body">
      <div class="row">
        <div class="col-sm-4 form-group">
          <label class="font_normal_body">规则名称</label>
          <input type="text" class="input_search" style="width:60%" v-model="updaterow.f_rule_name"
                 @blur="findname" placeholder='规则名称'>
        </div>

        <div class="col-sm-4 form-group">
          <label class="font_normal_body">间隔天数</label>
          <input type="number" class="input_search" style="width:60%" v-model="updaterow.f_day"
                 placeholder='日期'>
        </div>
      </div>
    </article>
    <footer slot="modal-footer" class="modal-footer">
      <button type="button" class="button_new button_spacing" @click="updateconfirm()">修改</button>
      <button type="button" class="button_clear" @click='cancel()'>取消</button>
    </footer>
  </modal>
</template>
<script>
  /**
   *表册查询列表
   */
  import {PagedList} from 'vue-client'
  import Vue from 'vue'

  let asyncReadyGen = async function (self) {
    // self.loadMeterBooks()
    self.initSlice(self.$login.f.orgid)
    await self.getinputtores()
    await self.$GetSaleParam.initinputtor()
    self.$refs.paged.$refs.cri.model.performDate = self.$login.toStandardDateString()
    self.search()
  }

  export default {
    title: '违约金规则',
    data () {
      return {
        model: new PagedList('rs/sql/getOverdueSet', 30, {items: '"*"', tablename: '"t_overdue_set"', orderitem: '"f_operate_date  desc"'}),
        show: false,
        inputrow : {
          f_rule_name: '',
          f_month: '',
          f_day: ''
        },
        curorgid: [this.$login.f.orgid],
        f_orgid: this.$login.f.orgid,
        updateshow: false,
        updaterow:{}

      }
    },
    ready () {

    },
    methods: {

      operate(row){
        this.$emit('openuser',row)
      },
      update(row){
        this.updaterow = row
        this.updateshow =true
      },
      async findname (val) {
        let res = await this.$resetpost('rs/sql/saleSingleTable', {
          data: {
            tablename: 't_overdue_set',
            condition: `f_rule_name = '${this.inputrow.f_rule_name}' and f_orgid = '${this.$login.f.orgid}' `
          }
        }, {resolveMsg: null, rejectMsg: ''})
        if(res.data.length >0){
          this.inputrow.f_rule_name = ''
          this.$showAlert('规则名已存在', 'warning', 2000)
        }
      },
      search () {
        this.$refs.paged.$refs.cri.search()
      },
      getorg (val) {
        this.f_orgid = this.$login.convertToIn(val)
        this.$parent.f_orgid = this.f_orgid
      },
      cancel () {
        this.show = false
        this.updateshow = false
        this.inputrow = {
          f_rule_name: '',
          f_month: '',
          f_day: ''
        }
        this.updaterow = {}
        this.search()
      },
      async updateconfirm(){

        if(this.updaterow.f_rule_name === ''){
          this.$showAlert('请输入规则名', 'warning', 2000)
          return
        }
        if(this.updaterow.f_day === ''){
          this.$showAlert('请输入间隔天数', 'warning', 2000)
          return
        }
        await this.$resetpost('rs/entity/t_overdue_set',this.updaterow)
        this.updateshow = false
        this.updaterow = {}
        this.search()
      },
      async confirm () {
        if(this.inputrow.f_rule_name === ''){
          this.$showAlert('请输入规则名', 'warning', 2000)
          return
        }
        if(this.inputrow.f_day === ''){
          this.$showAlert('请输入间隔天数', 'warning', 2000)
          return
        }
        let param = {
          f_rule_name: this.inputrow.f_rule_name,
          f_month: this.inputrow.f_month,
          f_day: this.inputrow.f_day,
          f_operate_date: this.$login.toStandardTimeString(),
          f_operator: this.$login.f.name,
          f_operatorid: this.$login.f.id,
          f_orgid: this.$login.f.orgid,
          f_orgname: this.$login.f.orgs,
          f_depid: this.$login.f.depids,
          f_depname: this.$login.f.deps
        }
        await this.$resetpost('rs/entity/t_overdue_set',param)
        this.show = false
        this.inputrow = {
          f_rule_name: '',
          f_month: '',
          f_day: ''
        }
        this.search()
      },
      selfSearch (args) {
        if (!this.f_orgid) {
          this.getorg([this.$login.f.orgid])
        }
        if (this.f_orgid) {
          args.condition = `f_orgid in ${this.f_orgid} and ${args.condition}`
        }
        this.model.search(args.condition, args.model)
        this.$dispatch('close')
      },
      add() {
        this.show = true
      }
    },
    watch: {

    },
    computed: {

    }
  }
</script>
