<template>
    <div class="auto select-overspread" >
        <validator name='v'>
            <form class="form-horizontal" novalidate>
                <ul class="nav nav-tabs">
                    <li class="active"><a href="#">派单</a></li>
                </ul>
                <div class="row">
                    <div :class="[$v.f_userinfo_code.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <label class="font_normal_body" >用户编号</label>
                        <input class="input_search" style="width:60%" type="text" v-model="data.f_userinfo_code" :readonly="editValue" v-show="true">
                    </div>
                    <div :class="[$v.f_dispathch_name.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <label class="font_normal_body">项目名称</label>
                        <input class="input_search" style="width:60%" type="text" v-model="data.f_dispathch_name" :readonly="editValue" v-show="true">
                    </div>
                </div>
                <div class="row">
                    <div :class="[$v.f_userinfo_code.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <label class="font_normal_body" >联系电话</label>
                        <input class="input_search" style="width:60%" type="text" v-model="data.f_phone" :readonly="editValue" v-show="true">
                    </div>
                    <div :class="[$v.f_userinfo_code.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <label class="font_normal_body" >安装地址</label>
                        <input class="input_search" style="width:60%" type="text" v-model="data.f_dispathch_address" :readonly="editValue" v-show="true">
                    </div>
                </div>
                <div class="row">
                    <div :class="[$v.f_userinfo_code.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <label class="font_normal_body" >派工类型</label>
                        <v-select :disabled='isEdit'
                                  :options='f_dispathch_type'
                                  :value-single="true" :readonly="editValue"
                                  :value.sync="data.f_dispathch_type"
                                  close-on-select
                                  placeholder='请选择'
                                  v-model="data.f_dispathch_type">
                        </v-select>
                    </div>
                    <div  class="col-sm-6"  :class="[$v.f_yuyue_date.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <!--            <input class="input_search" style="width:60%" type="text" v-model="data.f_perform_date" v-show="false"-->
                        <!--                   v-validate:f_perform_date='{required: true }'>-->
                        <label class="font_normal_body" for="f_finish_date" title="预约时间">预约时间</label>
                        <datepicker :disabled='isEdit' :aria-required="true"
                                    :format="'yyyy-MM-dd'" :readonly="editValue"
                                    :value.sync="data.f_yuyue_date"
                                    id="f_yuyue_date"
                                    placeholder="预约时间"
                                    v-model="data.f_yuyue_date" style="width: 60%">
                        </datepicker>
                    </div>
                </div>



                <div   class="row">
                    <div   :class="[$v.f_finish_date.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <!--            <input class="input_search" style="width:60%" type="text" v-model="data.f_perform_date" v-show="false"-->
                        <!--                   v-validate:f_perform_date='{required: true }'>-->
                        <label class="font_normal_body" for="f_finish_date">票号&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
                        <input class="input_search" style="width:60%" type="text" :readonly="editValue"  v-model="data.f_zjph" v-show="true">
                    </div>
                  <div :class="[$v.f_userinfo_code.required ? 'has-error' : 'has-success']" class="col-sm-6">
                    <label class="font_normal_body" >燃气表号</label>
                    <input class="input_search" style="width:60%" type="text" v-model="data.f_meternumber" :readonly="editValue" v-show="true">
                  </div>
                </div>



                <div class="row">
                    <div :class="[$v.f_userinfo_code.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <label class="font_normal_body" >进气方向</label>
                        <v-select :disabled='isEdit'
                                  :options='f_aroundmeter'
                                  :value-single="true" :readonly="editValue"
                                  :value.sync="data.f_aroundmeter"
                                  close-on-select
                                  placeholder='请选择'
                                  v-model="data.f_aroundmeter">
                        </v-select>
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-12">
                        <label  class="font_normal_body ">备注内容</label>
                        <textarea class="input_textarea" rows="1" style="width:80%;margin-top:5px" v-model="data.f_remarks" :readonly="editValue"></textarea>
                    </div>
                </div>
                <div class="row">
                    <div  class="col-sm-6" :class="[$v.f_perform_date.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <!--            <input class="input_search" style="width:60%" type="text" v-model="data.f_perform_date" v-show="false"-->
                        <!--                   v-validate:f_perform_date='{required: true }'>-->
                        <label class="font_normal_body" for="f_dispathch_date">派工日期</label>
                        <datepicker :disabled='isEdit'
                                    :format="'yyyy-MM-dd'" :readonly="editValue"
                                    :value.sync="data.f_dispathch_date"
                                    id="f_dispathch_date"
                                    placeholder="派工日期"
                                    v-model="data.f_dispathch_date" style="width: 60%">
                        </datepicker>
                    </div>
                    <div :class="[$v.f_dispathch_name.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <label class="font_normal_body">派&nbsp;工&nbsp;人&nbsp;</label>
                        <input class="input_search" style="width:60%" type="text" :readonly="editValue" :value.sync="f_dispathch_people" v-model="data.f_dispathch_people" v-show="true">
                    </div>
                </div>
                <div class="row">
                    <div  class="col-sm-6" :class="[$v.f_end_date.required ? 'has-error' : 'has-success']" class="col-sm-12">
                        <!--            <input class="input_search" style="width:60%" type="text" v-model="data.f_perform_date" v-show="false"-->
                        <!--                   v-validate:f_perform_date='{required: true }'>-->
                        <label class="font_normal_body" for="f_dispathch_date" title="预估截止时间">截至日期</label>
                        <datepicker :disabled='isEdit'
                                    :format="'yyyy-MM-dd'"
                                    :value.sync="data.f_end_date"
                                    id="f_dispathch_date" :readonly="editValue"
                                    placeholder="截至日期"
                                    v-model="data.f_end_date" style="width: 60%">
                        </datepicker>
                    </div>
                    <div :class="[$v.f_dispathch_num.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <input class="input_search" style="width:60%" type="text" v-model="data.f_dispathch_num" v-show="false"
                               v-validate:f_gasproperties='{required: true }'>
                        <label class="font_normal_body" style="color: red" >紧急程度</label>
                        <v-select :disabled='isEdit'
                                  :options='urgentNum'
                                  :value-single="true" :readonly="editValue"
                                  :value.sync="data.f_dispathch_num"
                                  close-on-select
                                  placeholder='请选择'
                                  v-model="data.f_dispathch_num">
                        </v-select>
                    </div>
                </div>
                <div class="row">
                    <div :class="[$v.f_build_people.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <label class="font_normal_body">施&nbsp;工&nbsp;人&nbsp;</label>
                        <v-select :disabled='isEdit'
                                  :options='sgr'
                                  :value-single="true" :readonly="editValue"
                                  :value.sync="data.f_build_people"
                                  close-on-select
                                  placeholder='请选择'
                                  v-model="data.f_build_people">
                        </v-select>
                    </div>

                    <div :class="[$v.f_build_unit.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <label class="font_normal_body">施工单位</label>
                        <v-select :disabled='isEdit'
                                  :options='sgdw'
                                  :value-single="true" :readonly="editValue"
                                  :value.sync="data.f_build_unit"
                                  close-on-select
                                  placeholder='请选择'
                                  v-model="data.f_build_unit">
                        </v-select>
                    </div>

                </div>

                <div class="row">
                    <div class="col-sm-12">
                        <label  class="font_normal_body " >施工内容</label>
                        <textarea class="input_textarea" rows="1" style="width:80%;margin-top:5px" v-model="data.f_contents" :readonly="editValue"></textarea>
                    </div>
                </div>
                <div  v-show="data.f_dispathch_state=='施工中'||data.f_dispathch_state=='已完成'" class="row">
                    <div  class="col-sm-6"  :class="[$v.f_finish_date.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <!--            <input class="input_search" style="width:60%" type="text" v-model="data.f_perform_date" v-show="false"-->
                        <!--                   v-validate:f_perform_date='{required: true }'>-->
                        <label class="font_normal_body" for="f_finish_date" title="施工完成日期">完成日期</label>
                        <datepicker :disabled='isEdit' :aria-required="true"
                                    :format="'yyyy-MM-dd'" :readonly="editValue"
                                    :value.sync="data.f_finish_date"
                                    id="f_finish_date"
                                    placeholder="完成日期"
                                    v-model="data.f_finish_date" style="width: 60%">
                        </datepicker>
                    </div>
                    <div :class="[$v.f_pingjia.required ? 'has-error' : 'has-success']" class="col-sm-6">
                        <label class="font_normal_body" >完成评价</label>
                        <v-select :disabled='isEdit'
                                  :options='pj'
                                  :value-single="true" :readonly="editValue"
                                  :value.sync="data.f_pingjia"
                                  close-on-select
                                  placeholder='请选择'
                                  v-model="data.f_pingjia">
                        </v-select>
                    </div>
                </div>





                <div  v-show="data.f_dispathch_state=='施工中'||data.f_dispathch_state=='已完成'" class="row">
                    <div class="col-sm-12">
                        <label  class="font_normal_body ">完工备注</label>
                        <textarea class="input_textarea" rows="1" style="width:80%;margin-top:5px" v-model="data.f_wgremarks" :readonly="editValue"></textarea>
                    </div>
                </div>
                <show-list :queryshow="true"  :data="data"></show-list>
            </form>



            <div style="text-align:right;height:auto;margin-top:6px;">
                <button :disabled='!$v.valid' @click="confirm()" class="button_search" v-show="data.f_dispathch_state=='待分配'">派单</button>
                <button :disabled='!$v.valid' @click="confirm()" class="button_search" v-show="!data.f_dispathch_state">新增派单</button>
                <button :disabled='!$v.valid' @click="confirm()" class="button_search" v-show="data.f_dispathch_state=='施工中'">完成</button>
                <!--        <button @click="deletePrice()" class="button_delete" v-if="!isEdit && data.usercount === 0">删除</button>-->
                <button @click="close()" class="button_clear">取消</button>
            </div>

            <!--</div>-->
        </validator>
        <!--    <limit-gas :f_price_id="data.id" :show.sync="limitShow" @limit="getLimitGas"></limit-gas>-->
    </div>
</template>
<script>
    import {HttpResetClass} from 'vue-client'

    export default {
      data () {
        return {
          unitshow: false,
          mixshow: false,
          stairshow: false,
          model: {},
          editValue: false,
                // 编辑控制
          isEdit: false,

                // 扣费方式的改变 如果涉及日运算，则阶梯开始时间和生效时间不做显示
          stairStartShow: true,

                // 公司树状图信息
          curorgid: [],

          f_filialeid: '',  // 所属公司
          f_filialename: '',  // 所属公司名称

                // 限购配置
          limitShow: false,
          limit_gas: null,
          f_dispathch_people: this.$login.f.name,

                // 下拉框数据
          usertype: this.$appdata.getParam('用户类型'),
          sgr: this.$appdata.getParam('施工人'),
          sgdw: this.$appdata.getParam('施工单位'),
          pricetype: this.$appdata.getParam('气价类型'),
          hearways: [{label: '按月', value: '按月'}, {label: '按季', value: '按季'}],
          isLowIncomes: [{label: '是', value: '1'}, {label: '否', value: '0'}],
          deductionWays: [{label: '周期扣费', value: 'cycle'}, {label: '日运算扣费', value: 'day'}, {label: '采暖季独立扣费', value: 'singleHS'}, {label: '采暖季日扣费', value: 'dayHS'}],
          urgentNum: [],
          pj: [{label: '好', value: '好'}, {label: '一般', value: '一般'}, {label: '不好', value: '不好'}],
          f_aroundmeter: [{label: '左进', value: '左进'}, {label: '右进', value: '右进'}],
          f_dispathch_type: this.$appdata.getParam('派工类型')
        }
      },
      props: ['data', 'config', 'f_filialeid'],
      watch: {
        'data' (val) {
          if (val) {
                    //  initGen(this, val)
          } else {
            this.initModel()
          }
        }

      },
      ready () {
            // initGen(this, this.data)
        for (let i = 1; i < 6; i++) {
          let num = {label: i, value: i}
          this.urgentNum.push(num)
        }
            /* if(this.data.f_dispathch_state!='待分配'){
                this.editValue=false;
            }else{
                this.editValue=true;
            } */
      },
      methods: {
        async confirm () {
          if (!this.data.f_dispathch_state) {
            console.log('111')
            this.data.f_dispathch_state = '待分配'
          }
          if (!this.data.f_money) {
            console.log('111')
            this.data.f_money = 0
          }
          let getUser = await this.$resetpost('rs/logic/saveDispatchInfo', {'data': this.data}, {resolveMsg: '派单成功', rejectMsg: '派单失败'})
          this.close()
        },

        async savePrice () {
                // 去除重复或者空的明细
          if (this.model.f_price_type[0] === '阶梯气价') {
            this.removeDuplication(this.model)
          }
                // 审核配置
          this.model.audit = this.config.audit

          this.model.f_filialeid = this.f_filialeid ? this.f_filialeid : this.$login.f.orgid

          if (this.limit_gas) {
            this.model.limit_gas = this.limit_gas
            // this.model.limit_gas.f_start_date = this.model.f_perform_date
          }
                // 现调用气价修改的逻辑   返回：version 和之前的对比 增加的话， 进行提示操作 反之 结束
          await this.$LogicService.gasPrice(this.model, this.data)
          this.$dispatch('success', '气价管理', this.data)
          this.initModel()
        },

        async deletePrice () {
          let res = await this.$showMessage(`您确定要删除此气价吗？`, ['confirm', 'cancel'])
          if (res === 'confirm') {
            await this.$resetpost('rs/logic/delprice', {data: {id: this.model.id}}, {
              resolveMsg: '删除气价成功',
              rejectMsg: '删除气价失败'
            })
          }
          this.$dispatch('success')
        },

        getLimitGas (val) {
          console.log('获取到限购值。', val)
          this.limit_gas = val
        },

        getorg (id, name) {
          this.f_filialeid = id[0]
          this.f_filialename = name[0]
        },
        initModel () {
          this.model = {
            f_user_type: '',
            f_gasproperties: '',
            f_price_type: '',
            f_stairmonths: 1,
            f_perform_date: '',
            detailprice: [],
            f_stair_start_date: '',
            f_population_increment: 0,
            f_population_base: 4,
            f_comments: '',
            f_deduction_way: this.config.deductionWay
          }
        },
        close () {
          this.$dispatch('close')
        },
        removeDuplication (val) {
          if (val.detailprice.length > 0) {
            let hash = []
            let arr = []
            for (var i = 0; i < val.detailprice.length; i++) {
                        // hash[val.detailprice[i].f_gas] != null
              if (!val.detailprice[i].f_gas) {
                continue
              }
              if (!hash[val.detailprice[i].f_gas]) {
                arr.push(val.detailprice[i])
                hash[val.detailprice[i].f_gas] = true
              }
            }
            val.detailprice = arr
          }
        },
        usertypeChange (val) {
          if (val) {
            this.gastype = this.$appdata.getParam(this.model.f_user_type)
          }
        },
        deductionChange (val) {
                // 扣费方式的改变 如果涉及日运算，则阶梯开始时间和生效时间不做显示
          this.stairStartShow = !val.includes('day')
        },
        limitClick () {
          this.limitShow = true
        },
        showChangeView () {
          this.$parent.changeView = true
        }
      }
    }
</script>
<style lang="less">
    #gas-price-form {
        .form-group {
            margin-left: 0px;
            margin-right: 0px;
        }
    }

</style>
