<template>
  <div class="auto" >
    <validator name='v'>
      <div novalidate class=" select-overspread container-fluid auto" partial>
        <ul class="nav nav-tabs">
          <li class="active a-tabs"><a href="#">低保信息</a></li>
        </ul>
        <form class="form-horizontal form-group">
          <div class="row">
            <div class="col-sm-6 form-group">
              <label class="font_normal_body">所属公司</label>
              <res-select :initresid='curorgid' @res-select="getorg" is-mul="false" restype='organization'></res-select>
            </div>
            <div class="col-sm-6 form-group" :class="[$v.f_userinfo_code.required ? 'has-error' : 'has-success']">
              <label for="f_userinfo_code" class="font_normal_body">用户编号</label>
              <input class="input_search" style="width:60%" type="text" class="input_search" style="width:60%"
                     v-model="model.f_userinfo_code"  @keyup.enter="getinfo()"
                     v-validate:f_userinfo_code='{required: true }' :disabled='isEdit' placeholder="输入编号,回车查询">
            </div>
            <div class="col-sm-6 form-group">
              <label for="f_user_name" class=" font_normal_body">用户姓名</label>
              <input class="input_search" style="width:60%" type="text" class="input_search" style="width:60%"
                     v-model="model.f_user_name" readonly
                     placeholder="用户姓名">

            </div>
          </div>

          <div class="row">
            <div class="col-sm-12 form-group" >
              <label for="f_address" class=" font_normal_body">地&emsp;&emsp;址</label>
              <input class="input_search" type="text" class="input_search" style="width:80%" v-model="model.f_address"
                     readonly
                     placeholder="地址">
            </div>

          </div>

          <div class="row" >
            <div class="col-sm-6 form-group" id="f_start_date" :class="[$v.f_start_date.required ? 'has-error' : 'has-success']">
              <input type="text" class="input_search" style="width:60%" v-show="false" v-model="model.f_start_date"
                     v-validate:f_start_date='{required: true }'>
              <label for="f_start_date" class=" font_normal_body">开始日期</label>
              <datepicker style="width: 60%"
                          placeholder="开始日期"
                          v-model="model.f_start_date"
                          :value.sync="model.f_start_date"
                          class="datepicker"
                          :format="'yyyy-MM-dd'"
                          :disabled='isEdit'>
              </datepicker>
            </div>
            <div class="col-sm-6 form-group" id="f_end_date" :class="[$v.f_end_date.required ? 'has-error' : 'has-success']">
              <input type="text" class="input_search" style="width:60%" v-model="model.f_end_date"
                     v-validate:f_end_date='{required: true }' v-show="false" >
              <label for="f_end_date" class=" font_normal_body">结束日期</label>
              <datepicker style="width: 60%"
                          placeholder="结束日期"
                          v-model="model.f_end_date"
                          :value.sync="model.f_end_date"
                          class="datepicker"
                          :format="'yyyy-MM-dd'"
                          :disabled='isEdit'>
              </datepicker>

            </div>
          </div>

          <div class="row">
            <div class="col-sm-6 form-group" :class="[$v.f_price_id.required ? 'has-error' : 'has-success']">
              <label class="font_normal_body">气&emsp;&emsp;价</label>
              <input type="text" class="input_search" style="width:60%" v-show="false" v-model="model.f_price_id"
                     v-validate:f_price_id='{required: true }'>
              <v-select id="f_price_name"
                        v-model="model.f_price_id"
                        :value.sync="model.f_price_id"
                        :options='pricename'
                        :value-single="true"
                        close-on-select></v-select>

            </div>
          </div>

          <div style="text-align:right;height:auto;margin-top:6px;">
            <button class="button_search" type="button" @click="confirm('确认')"  v-if="!isEdit" :disabled='!$v.valid'>
              确认
            </button>
            <button class="button_search"  type="button"  style="width:100px;" @click="confirm('取消低保户')"
                    v-show="isBasicLiving1 || model.isBasicLiving1 =='true'" :disabled='!$v.valid'>
              取消低保户
            </button>
            <button class="button_search" type="button"  @click="close()">取消</button>
          </div>
          <!--<upload :blodid="model.f_userinfo_id" cols="col-sm-12" isremark="true" fusetype="低保户"></upload>-->
        </form>

      </div>


    </validator>

  </div>
</template>
<script>
  import {HttpResetClass} from 'vue-client'

  let initGen = async function (self, val) {
  // 获取低保气价
    self.$resetpost('rs/sql/get_priceId',
      {data: {f_orgid: self.f_filiale_id, condition: `f_islow_income = '1'`}},
      {resolveMsg: null, rejectMsg: null}).then((res) => {
        let rs = []
        for (let i = 0; i < res.data.length; i++) {
          let temp = {
            label: res.data[i].f_price_name,
            value: res.data[i].f_price_id
          }
          rs.push(temp)
        }
        self.pricename = [...rs]
      })

    if (val) {
      self.model = Object.assign({}, self.model, val)
    }
    // let http = new HttpResetClass()
    // let res = await http.load('POST', 'rs/sql/get_BasicLiving',
    //   {data: {condition: `id = '${val.id}'`}}, {
    //   resolveMsg: null,
    //   rejectMsg: null
    // })
    // console.log(res.data)
    // self.model = Object.assign({}, self.model, val)
    // self.model.f_userinfo_code = res.data[0].f_userinfo_code
    // self.model.f_userinfo_id = res.data[0].f_userinfo_id
    // self.model.f_user_phone = res.data[0].f_user_phone
    // self.model.f_address = res.data[0].f_address
    // self.model.f_user_phone = res.data[0].f_user_phone
    // self.model.f_start_date = res.data[0].f_start_date
    // self.model.f_end_date = res.data[0].f_end_date
    // self.model.f_period = res.data[0].f_period
    // self.model.f_price_id = res.data[0].f_price_id
    // self.model.f_price_name = res.data[0].f_price_name
    // self.model.f_operator = self.$login.f.name
    // self.model.f_operatorid = self.$login.f.id
    // self.model.f_filialeid = self.model.f_filialeid ? self.model.f_filialeid : self.$login.f.orgid
    // self.model.f_orgid = self.model.f_orgid ? self.model.f_orgid : self.$login.f.orgid
    // self.model.f_orgname = self.model.f_orgname ? self.model.f_orgname : self.$login.f.orgs
    // self.model.f_depid = self.model.f_depid ? self.model.f_depid : self.$login.f.depids
    // self.model.f_depname = self.model.f_depname ? self.model.f_depname : self.$login.f.deps
  }

  export default {
    title: '低保户',
    props: {
      f_islow_income: {
        type: String,
        default: '1'
      },
      data: {
        type: Object
      }
    },
    data () {
      return {
        // 公司树状图信息
        curorgid: [this.$login.f.orgid],
        pricename: [{value: '', label: ''}],
        model: {
          f_userinfo_code: '',
          f_userinfo_id: '',
          f_user_name: '',
          f_address: '',
          f_price_id: '',
          f_price_name: '',
          f_user_phone: '',
          f_start_date: '',
          f_end_date: '',
          f_period: '',
          f_state: '有效'

        },
        isBasicLiving1: false,
        // 编辑控制
        isEdit: false,
        // 所属公司
        f_filiale_id: this.$login.f.orgid,
        // 所属公司名称
        f_filiale_name: this.$login.f.orgs
      }
    },
    ready () {
      if (this.data) {
        this.isBasicLiving1 = true
        initGen(this, this.data)
      } else {
        initGen(this, this.data)
        this.initModel()
      }
    },
    watch: {
      'data' (val) {
        if (val) {
          initGen(this, val)
        } else {
          this.initModel()
        }
      },
      'model.f_start_date' (val) {
        if (this.model.f_end_date) {
          this.compareDate(val, this.model.f_end_date)
        }
      },
      'model.f_end_date' (val) {
        if (this.model.f_start_date) {
          this.compareDate(this.model.f_start_date, val)
        }
      },
      'f_filiale_id' () {
        initGen(this, this.data)
      }
    },
    methods: {

      async getinfo () {
        let res = await this.$resetpost('rs/sql/sale_getUserAndPeriod',
          {data: {condition: "f_userinfo_code = '" + this.model.f_userinfo_code + "' and f_orgid ='" + this.f_filiale_id + "'"}}, {
            resolveMsg: null,
            rejectMsg: null
          })
        if (res.data.length == 0) {
          this.$showAlert('查询无此用户!!请核对编号', 'warning', 3000)
        } else {
          this.model = res.data[0]
          this.$parent.blodid = this.model.f_userinfo_id
          console.log("时间是什么时候",this.$login.toStandardDateString())
          // self.$refs.paged.$refs.cri.model.f_start_date
          this.model.f_start_date = this.$login.toStandardDateString()
          console.log("开始时间有吗？",this.model.f_start_date)
        }
      },
      async confirm (type) {
        // if (type == '取消低保户' && !this.$login.r.find(value => value == '取消低保户')) {
        //   this.$showMessage('你没有取消低保户的权限,请联系管理员!')
        //   return
        // }
        if (type == '取消低保户'){
          let data = {
            f_userinfo_code: this.f_userinfo_code,
            f_userinfo_id: this.model.f_userinfo_id,
            f_user_name: this.model.f_user_name,
            f_address: this.model.f_address,
            f_price_id: this.model.f_price_id,
            f_price_name: this.model.f_price_name
          }
          let res = await this.$resetpost('rs/logic/stopBasicLiving', {data: data},{resolveMsg: null, rejectMsg: null})
          if (res){
            if (res.data.code == 200) {
              this.$showAlert(res.data.msg, 'success', 2000)
              this.$dispatch('success','isBasicLiving')
            } else if (res.data.code == 500) {
              this.$showAlert(res.data.msg, 'warning', 2000)
            }
          }
          this.initModel()
        }else {
          await this.$LogicService.basicLiving(this.model, this.data)
          this.initModel()
          this.$dispatch('success', 'isBasicLiving')
        }
      },
      getorg (id, name) {
        this.f_filialeid = id[0]
        this.f_filialename = name[0]
      },
      initModel () {
        this.model = {
          f_userinfo_code: '',
          f_userinfo_id: '',
          f_user_name: '',
          f_address: '',
          f_price_id: 0,
          f_price_name: '',
          f_user_phone: '',
          f_start_date: '',
          f_end_date: '',
          f_period: '',
          f_state: '正常'
        }
      },
      close () {
        this.$dispatch('close')
      },

      compareDate (date1, date2) {
        let s = new Date(date1)
        let e = new Date(date2)
        if (e < s) {
          this.model.f_end_date = ''
          this.$showAlert('结束日期不能小于开始日期', 'warning', 2000)
        }
      }
    }
  }
</script>
<style lang="less">
  #gas-price-form {
    .form-group {
      margin-left: 0px;
      margin-right: 0px;
    }
  }

</style>
