<style scoped>
  /deep/.el-input__inner,
  /deep/.el-range-input {
    background-color: transparent;
    border: none;
  }
</style>
<template>
  <q-dialog v-model="persistent" content-class="bg-grey-1" persistent transition-show="scale" transition-hide="scale">
    <q-card style="width: 400px;max-width:400px;">
      <q-card-section>
        <div class="text-h6">{{optionTitle}}</div>
      </q-card-section>
      <q-card-section class="q-pt-none scroll" style="max-height: 70vh">
        <template v-if="isShow==1">
          <div class="row wrap">
            <div class="col-12">
                <q-field filled dense>
                  <template v-slot:control>
                    <el-date-picker v-model="value" style="width:100%" type="month" :clearable="false" @change="dateChange"
                      value-format="yyyy-MM" placeholder="选择月">
                    </el-date-picker>
                  </template>
                </q-field>
                <div style="margin-top:10px;">
                <el-tag v-for="(tag, index) in objOption.TimeList" size="small" style="margin:0 5px 5px 0;" :key="tag"
                  @close="params => tagClose(index)" closable type="success">
                  {{tag}}
                </el-tag>
              </div>
            </div>
          </div>
          <div class="row wrap" style="margin-top:20px;">
            <div class="col-12">
              <q-input filled stack-label maxlength="20" :dense="false" @keyup.native="checkPrice(objOption,'GoalMoney')" v-model="objOption.GoalMoney" ref="GoalMoney2"
                label="目标金额" :rules="[val => !!val || '请填写目标金额']" />
            </div>
          </div>
          <div class="row wrap" style="margin-top:10px;">
            <div class="col-12">
              <q-select filled stack-label v-model="objOption.Dept_Id" :options="DeptList" use-input @filter="filterDepart" emit-value map-options
                label="所属部门" option-label="DeptName" option-value="DeptId" />
            </div>
          </div>
        </template>
        <template v-if="isShow==2">
          <div class="row wrap">
            <div class="col-12">
            <q-field filled dense>
              <template v-slot:control>
                <el-date-picker v-model="value2" style="width:100%" type="month" value-format="yyyy-MM" placeholder="选择月">
                </el-date-picker>
              </template>    
            </q-field>
            </div>
          </div>
          <div class="row wrap" style="margin-top:30px;">
            <div class="col-12">
              <q-input filled stack-label maxlength="20" :dense="false" v-model="updateMsg.GoalMoney" ref="GoalMoney2"
                label="目标金额" :rules="[val => !!val || '请填写目标金额']" />
            </div>
          </div>
          <div class="row wrap" style="margin-top:10px;">
            <div class="col-12">
              <q-select filled stack-label v-model="updateMsg.Dept_Id" :options="DeptList" use-input @filter="filterDepart" emit-value map-options
                label="所属部门" option-label="DeptName" option-value="DeptId" />
            </div>
          </div>
        </template>
      </q-card-section>
      <q-separator />
      <q-card-actions align="right" class="bg-white">
        <q-btn label="取消" flat color="grey-10" style="font-weight:400 !important" @click="closeSaveForm" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" v-if="isShow==1"
          :loading="saveLoading" @click="saveNeed" />
        <q-btn label="立即提交" color="accent q-px-md" style="font-weight:400 !important" v-if="isShow==2"
          :loading="saveLoading" @click="saveTarget" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>
<script>
  import {
    AddConsultantGoal,
    UpdateConsultantGoal,
    GetConsultantGoal
  } from '../../api/system/index'
  //部门
  import {
    getDeptList
  } from '../../api/system/dept'
  import selectTree from '../../components/common/select-tree'
  export default {
    props: {
      saveObj: {
        type: Object,
        default: null
      }
    },
    components: {
      selectTree
    },
    data() {
      return {
        persistent: true,
        saveLoading: false,
        value: '',
        objOption: {
          GoalMoney: "",
          Dept_Id: 13,
          TimeList: [],
        },
        updateMsg: {
          Id: 0,
          YearStr: '',
          MonthStr: '',
          GoalMoney: '',
          Dept_Id: ''
        },
        optionTitle: "",
        DeptList: [], // 部门下拉

        isShow: 1,
        value2: '',
        allDeptList:[]
      }
    },
    created() {
      this.queryDeptList();
    },
    mounted() {
      this.initObj();
    },
    methods: {
      //筛选部门
      filterDepart(val, update, abort) {
        update(() => {
          this.DeptList = this.allDeptList.filter(
            v => v.DeptName.indexOf(val) > -1
          );
        });
      },
      queryDeptList() {
        var qMsg = {};
        getDeptList(qMsg).then(res => {
          this.DeptList = res.Data;
          this.allDeptList = res.Data;
        });
      },
      //选择月份
      dateChange(val) {
        this.objOption.TimeList.push(val) // 这里记得格式化
      },
      tagClose(index) {
        this.objOption.TimeList.splice(index, 1);
      },
      //初始化表单
      initObj() {
        if (this.saveObj && this.saveObj.Id > 0) {
          GetConsultantGoal({
            Id: this.saveObj.Id
          }).then(res => {
            this.updateMsg.Id = res.Data.Id;
            this.updateMsg.YearStr = res.Data.YearStr;
            this.updateMsg.MonthStr = res.Data.MonthStr;
            this.updateMsg.GoalMoney = res.Data.GoalMoney;
            this.updateMsg.Dept_Id = res.Data.Dept_Id;
            this.isShow = 2;
            this.value2 = this.updateMsg.YearStr + '-' + this.updateMsg.MonthStr;
          })
          this.optionTitle = "修改目标"
        } else {
          this.optionTitle = "新增目标"
          this.objOption.GoalMoney = '';
          this.objOption.Dept_Id = '';
          this.objOption.TimeList = [];
          this.isShow = 1
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
      },
      //新增保存菜单
      saveNeed() {
        this.saveLoading = true
        AddConsultantGoal(this.objOption).then(res => {
          this.saveLoading = false;
          if (res.Code == 1) {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据保存成功！',
              position: 'top'
            })
            this.$emit("success")
            this.closeSaveForm()
          }
        })
      },
      //修改保存
      saveTarget() {
        if (this.value2) {
          let myDate = this.value2.split('-');
          this.updateMsg.YearStr = myDate[0];
          this.updateMsg.MonthStr = myDate[1];
        } else {
          this.updateMsg.YearStr = '';
          this.updateMsg.MonthStr = '';
        }
        this.saveLoading = true;
        UpdateConsultantGoal(this.updateMsg).then(res => {
          this.saveLoading = false;
          if (res.Code == 1) {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据保存成功！',
              position: 'top'
            })
            this.$emit("success")
            this.closeSaveForm()
          }
        })
      }
    }
  }
</script>