<template>
  <div class="customer-css">
    <el-dialog :title="titleCon" :close-on-click-modal="false" :visible.sync="visible" width="350px" style="height: 420px;" class="customer-dialog">

        <el-form :inline="true" label-position="top" label-width="80px">
            <!-- 标签自定义信息 -->
                <el-row>
                  <el-col :span="12">
                    <el-form-item class="customer-item" :label=labels.customerId>
                      <el-input v-model="pageData.customerId" :readonly="labelNoReadOnly" style="width: 120px;" ></el-input>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item class="customer-item" :label=labels.addressNo>
                      <el-input v-model="pageData.addressNo" :readonly="labelNoReadOnly" style="width: 120px;" ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="12">
                    <el-form-item class="customer-item" :label=labels.labelType>
                      <el-select  v-model="pageData.labelType" disabled style="width: 150px;" >
                        <el-option label="外箱标签" value="外箱标签"></el-option>
                        <el-option label="卷标签" value="卷标签"></el-option>
                      </el-select >
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item class="customer-item">
                      <span slot="label" style="" @click="getBaseList(1099)"><a herf="#">{{labels.labelNo}}</a></span>
                      <el-input v-model="pageData.labelNo" :readonly="labelNoReadOnly" @blur="checkLabelNoMethod()" style="width: 120px;" ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
<!--                <el-row>
                  <el-col :span="12">
                    <el-form-item class="customer-item" :label=labels.labelClass>
                      <el-select v-model="pageData.labelClass" disabled style="width: 150px;" >
                        <el-option label="打印软件" value="打印软件"></el-option>
                      </el-select>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item class="customer-item" :label=labels.labelName>
                      <el-input v-model="pageData.labelName" disabled style="width: 120px;" ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>-->
                <el-row>
                  <el-col :span="12">
                    <el-form-item class="customer-item" style="margin-top: -8px;">
                      <span>{{labels.subLabelFlag}}</span>
                      <el-checkbox v-model="pageData.subLabelFlag"  @change="changeParentLabelNo()" true-label="Y" false-label="N"
                                   style="margin-top: 28px; margin-left: 10px;"></el-checkbox>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item class="customer-item" :label=labels.parentLabelNo>
                      <el-input v-model="pageData.parentLabelNo" :disabled="pageData.subLabelFlag !== 'Y'" style="width: 120px;" ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="24">
                    <el-form-item class="customer-item" :label=labels.remark>
                      <el-input v-model="pageData.remark" style="width: 310px;" ></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="saveLabelSetting()">{{ buttons.saveButton }}</el-button>
            <el-button type="primary" @click="closeDialog">{{buttons.closeButton}}</el-button>
          </span>
    </el-dialog>
    <!--列表的组件-->
    <Chooselist ref="baseList" @getBaseData="getBaseData"></Chooselist>
  </div>
</template>

<script>
import {
  insertLabelSetting,
  updateLabelSetting,
  checkLabelNo,
} from '@/api/labelSetting/com_add_update_customer_label.js';

import {
  searchSysLanguagePackList,
  searchSysLanguageParam,
  searchFunctionButtonList,
  saveButtonList,
  searchSysLanguage,
  searchLanguageListByLanguageCode,
  saveSysLanguageOne,
  searchPageLanguageData,
  removerLanguage,
  saveSysLanguageList
} from "@/api/sysLanguage.js";


/* 引入组件 */
import Chooselist from '@/views/modules/common/Chooselist'/* 列表组件 */
var functionId = 'C10000053';

export default {
  data() {
    return {
      titleCon: '客户标签定义维护',
      showDefault: false,
      visible: false,
      userId: this.$store.state.user.name,
      pageData: {
        site: this.$store.state.user.site,
        username: this.$store.state.user.name,
        customerId: '',
        addressNo: '',
        labelNo: '',
        labelType: '',
        labelName: '',
        labelClass: '',
        subLabelFlag: 'N',
        parentLabelNo: '',
        remark: '',
        addFlag: false,
      },
      labelNoReadOnly: false,
      dataListLoading: false,
      buttons: {
        saveButton: '保存',
        closeButton: '关闭',
      },
      queryButton: {
        functionId: functionId,
        table_id: '*',
        languageCode: this.$i18n.locale,
        objectType: 'button'
      },
      labels: {
        site: '工厂编号:',
        customerId: '客户编码:',
        addressNo: '地址编码:',
        titleCon: '客户标签定义维护',
        labelNo: '标签编号:',
        labelType: '标签类型:',
        labelName: '报表文件名:',
        labelClass: '标签种类:',
        subLabelFlag: '子报表:',
        parentLabelNo: '上级标签编号:',
        remark: '备注:',
        confirmLabel: '确认',
        cancelLabel: '取消',
        tipsLabel: '提示',
        labelNoCantBeNull: '标签编号不能为空!',
        labelTypeCantBeNull: '标签类型不能为空!',
        labelNameCantBeNull: '报表文件名不能为空!',
      },
      queryLabel: {
        functionId: functionId,
        table_id: '*',
        languageCode: this.$i18n.locale,
        objectType: 'label'
      },
    }
  },
  methods: {
    //初始化组件的参数
    init(currentRow) {
      //初始化参数
      this.pageData = JSON.parse(JSON.stringify(currentRow));
      //统一子报表标识和值，避免页面展示为*
      this.pageData.subLabelFlag = this.pageData.subLabelFlag === 'Y' ? 'Y' : 'N';
      this.pageData.parentLabelNo = this.getDisplayParentLabelNo(this.pageData.parentLabelNo, this.pageData.subLabelFlag);
      //打开页面
      this.visible = true;
      //判断是否是新增
      if(this.pageData.addFlag == 'Y'){
        this.labelNoReadOnly = false;
      }else{
        this.labelNoReadOnly = true;
      }
      //重置人员信息
      this.pageData.username = this.userId;
      //判断是否启用多语言
      //this.getMultiLanguageList();
      //重置标题
      this.titleCon = this.labels.titleCon;
    },

    /*检验标签的编码*/
    checkLabelNoMethod(){
      checkLabelNo(this.pageData).then(({data}) => {
        //判断是否查询到了
        if(data && data.code == 200){
          //设置参数
          let labelSetting = data.resultRow;
          this.pageData.labelType = labelSetting.labelType;
          this.pageData.labelClass = labelSetting.labelClass;
          this.pageData.labelName = labelSetting.labelName;
        }else{
          this.pageData.labelName = '';
          this.pageData.labelNo = '';
          this.pageData.labelType = '';
          this.pageData.labelClass = '';
          this.$message.error(data.msg);
        }
      })
    },

    /*子报表事件触发*/
    changeParentLabelNo(){
      //首选判断是否是选中状态
      if(this.pageData.subLabelFlag === 'Y'){
        this.pageData.parentLabelNo = '';
      }else{
        this.pageData.parentLabelNo = '';
      }
    },

    /*根据子报表标识处理上级标签显示值*/
    getDisplayParentLabelNo(parentLabelNo, subLabelFlag){
      if(subLabelFlag !== 'Y'){
        return '';
      }
      if(parentLabelNo == null || parentLabelNo === '*'){
        return '';
      }
      return parentLabelNo;
    },

    /*构建提交参数*/
    buildSubmitData(){
      let submitData = JSON.parse(JSON.stringify(this.pageData));
      submitData.customerId = (submitData.customerId || '').trim().toUpperCase();
      submitData.addressNo = (submitData.addressNo || '').trim().toUpperCase();
      submitData.labelNo = (submitData.labelNo || '').trim().toUpperCase();
      submitData.labelType = (submitData.labelType || '').trim();
      submitData.labelName = (submitData.labelName || '').trim();
      submitData.remark = (submitData.remark || '').trim();
      submitData.subLabelFlag = submitData.subLabelFlag === 'Y' ? 'Y' : 'N';
      let parentLabelNo = (submitData.parentLabelNo || '').trim().toUpperCase();
      submitData.parentLabelNo = submitData.subLabelFlag === 'Y' ? parentLabelNo : '*';
      return submitData;
    },

    /*保存前校验*/
    validateSubmitData(submitData){
      if(!submitData.customerId){
        this.$message.error('客户编码不能为空!');
        return false;
      }
      if(!submitData.addressNo){
        this.$message.error('地址编码不能为空!');
        return false;
      }
      if(!submitData.labelNo){
        this.$message.error(this.labels.labelNoCantBeNull);
        return false;
      }
      if(!submitData.labelType){
        this.$message.error(this.labels.labelTypeCantBeNull);
        return false;
      }
      if(submitData.subLabelFlag === 'Y' && (!submitData.parentLabelNo || submitData.parentLabelNo === '*')){
        this.$message.error('子报表必须维护上级标签编号!');
        return false;
      }
      return true;
    },

    /*保存标签自定义的信息*/
    saveLabelSetting(){
      let submitData = this.buildSubmitData();
      if(!this.validateSubmitData(submitData)){
        return false;
      }
      //判断是否是新增标签自定义
      if(this.pageData.addFlag === 'Y'){
        this.insertLabelSettingData(submitData);
      }else{
        this.updateLabelSettingData(submitData);
      }
    },

    /*数量失去焦点的事件*/
    insertLabelSettingData(submitData){
      insertLabelSetting(submitData).then(({data}) => {
        if(data.code == 200){
          this.$message.success(data.msg);
          setTimeout(() => {
            this.closeDialog();
          }, 200);
        }else{
          this.$message.error(data.msg);
        }
      })
    },

    /*验证时长的参数*/
    updateLabelSettingData(submitData){
      updateLabelSetting(submitData).then(({data}) => {
        if(data.code == 200){
          this.$message.success(data.msg);
          setTimeout(() => {
            this.closeDialog();
          }, 200);
        }else{
          this.$message.error(data.msg);
        }
      })
    },

    /*关闭modal*/
    closeDialog(){
      //刷新报工的页面
      this.$emit('refreshPageTables');
      //关闭当前的页面
      this.visible = false;
    },

    /* 列表方法的回调 */
    getBaseData(val) {
      if (this.tagNo === 1099) {
        //设置参数
        this.pageData.labelNo = val.ReportID;
        this.pageData.labelType = val.ReportFamily;
        this.pageData.labelName = val.Reportfile;
        this.pageData.labelClass = val.ReportType;
      }
    },

    // 获取基础数据列表
    getBaseList(val) {
      this.tagNo = val
      this.$nextTick(() => {
        let strVal = ''
        if (val === 1099) {
          strVal = this.pageData.labelNo;
        }
        this.$refs.baseList.init(val, strVal)
      })
    },

    /*getMultiLanguageList() {
      //首先查询当前按钮的多语言
      searchFunctionButtonList(this.queryButton).then(({data}) => {
        if (data && data.code == 0) {
          this.buttons = data.data
        } else {
          // saveButtonList(this.buttonList).then(({data}) => {
          // })
        }
      });
      //其次查询当前标签的多语言
      searchFunctionButtonList(this.queryLabel).then(({data}) => {
        if (data && data.code == 0 ) {
          this.labels = data.data
        } else {
          // saveButtonList(this.buttonList).then(({data}) => {
          // })
        }
      });
    },*/

  },
  components: {
    Chooselist, /* 选择的组件 */
  },
  watch: {
    pageData: {
      deep: true,
      handler: function (newV, oldV) {
        let labelNo = (this.pageData.labelNo || '').toUpperCase();
        let parentLabelNo = (this.pageData.parentLabelNo || '').toUpperCase();
        if(this.pageData.labelNo !== labelNo){
          this.pageData.labelNo = labelNo;
        }
        if(this.pageData.parentLabelNo !== parentLabelNo){
          this.pageData.parentLabelNo = parentLabelNo;
        }
      }
    },
  },
  created() {
    // this.factoryList()
    // this.getLanguageList()
  }
}

</script>

<style scoped lang="scss">


</style>
