<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">
        <div class="row wrap">
          <q-input filled stack-label maxlength="30"  v-model="msg.Name" ref="Name"
            class="col-12 q-pb-lg" label="收客渠道" :rules="[val => !!val || '请填写收客渠道']" />
        </div>
        <div class="row wrap">
          <q-select filled stack-label multiple option-value="Id" option-label="Name" clearable v-model="msg.StaticTypeList"
            :options="optionList" label="统计类型" :dense="false" class="col-12" emit-value
            map-options>
          </q-select>
        </div>
      </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" :loading="saveLoading"
          @click="saveStage" />
      </q-card-actions>
    </q-card>
  </q-dialog>
</template>
<script>
  import {
    SetChannel
  } from '../../api/system/index'
  export default {
    props: {
      row: {
        type: Object,
        default: null
      }
    },
    data() {
      return {
        persistent: true,
        saveLoading: false,
        msg : {
          Id: 0,
          Name:"",
          StaticTypeList:[]
        },
        optionTitle: "",
        optionList: [{
          Id: 1,
          Name: "普通员工"
        }, {
          Id: 2,
          Name: "主管"
        }]
      }
    },
    mounted() {
      this.init();
    },
    methods: {
      //初始化表单
      init() {
        if (this.row?.Id??0 > 0) {
          this.msg.Id = this.row.Id;
          this.msg.Name = this.row.Name;
          if(this.row.StaticType){
            let SaArray = this.row.StaticType.split(',');
            SaArray.forEach(item => {  
              this.msg.StaticTypeList.push(+item);  
            });  
          }
          this.optionTitle = "修改收客渠道"
        } else {
          this.optionTitle = "新增收客渠道"
          this.msg.Id = 0;
          this.msg.Name = '';
          this.msg.StaticTypeList = [];
        }
      },
      //关闭弹窗
      closeSaveForm() {
        this.$emit('close')
      },
      //保存菜单
      saveStage() {
        this.$refs.Name.validate();
        if (!this.$refs.Name.hasError) {
          this.saveLoading = true
          SetChannel(this.msg).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>
