<template>
	<div class="app-container">
    <div class="title">打印机设置</div>
    <!-- v-if="$store.getters.user.userType==='01'" -->
    <div class="list_printer" v-if="userInfoVO.userType == '02' || userInfoVO.userType == '00'">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>小票打印机</span>
            </div>
            <el-select v-model="printer.receipt" placeholder="请选择打印机" clearable>
              <el-option
                v-for="item in printerList"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </el-card>
        </el-col>
        <el-col :span="8" v-if="userInfoVO.userType == '02'">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>条码打印机</span>
            </div>
            <el-select v-model="printer.barCode" placeholder="请选择打印机" clearable>
              <el-option
                v-for="item in printerList"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </el-card>
        </el-col>
        <el-col :span="8" v-if="userInfoVO.userType == '02'">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>标签打印机</span>
            </div>
            <el-select v-model="printer.label" placeholder="请选择打印机" clearable>
              <el-option
                v-for="item in printerList"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <div class="list_printer" v-if="userInfoVO.userType == '01'">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>查询记录打印机</span>
            </div>
            <el-select v-model="printer.query" placeholder="请选择打印机" clearable>
              <el-option
                v-for="item in printerList"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>出厂单打印机</span>
            </div>
            <el-select v-model="printer.exfactory" placeholder="请选择打印机" clearable>
              <el-option
                v-for="item in printerList"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>小票打印机</span>
            </div>
            <el-select v-model="printer.receipt" placeholder="请选择打印机" clearable>
              <el-option
                v-for="item in printerList"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </el-card>
        </el-col>
      </el-row>
      <el-row :gutter="20" style="margin-top: 20px">
        <el-col :span="8">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>条码打印机</span>
            </div>
            <el-select v-model="printer.barCode" placeholder="请选择打印机" clearable>
              <el-option
                v-for="item in printerList"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </el-card>
        </el-col>
        <el-col :span="8">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>标签打印机</span>
            </div>
            <el-select v-model="printer.label" placeholder="请选择打印机" clearable>
              <el-option
                v-for="item in printerList"
                :key="item"
                :label="item"
                :value="item">
              </el-option>
            </el-select>
          </el-card>
        </el-col>
      </el-row>
    </div>
    <div class="btn_box">
      <el-button type="primary" :loading="printLoading" @click="btn_submit_print">保 存</el-button>
    </div>
	</div>
</template>

<script>
  import { getLodop } from "@/utils/lodopUtils";
	import { setPrint, getPrintInfo } from "@/api/system/print";
  import {getInfoByOrderNo} from "@/api/order/cloth";

	export default {
		name: "printerSettings",
		dicts: ['sys_normal_disable'],
		data() {
			return {
        printer: {
          query: '', //查询记录打印机
          exfactory: '', //出厂单打印机
          receipt: '', //小票
          barCode: '', //条码
          label: '', //标签
        },
        printerList: [],
        printLoading: false,
        LODOP: null,
        userInfoVO: null,
        number: 1,
			}
		},
		created() {
      this.userInfoVO = this.getUserInfo();
		},
    mounted() {
      this.getPrinter();
      this.$modal.loading("正在加载打印机配置，请稍候...");
      this.number = 1;
      const printerTime = setInterval(() =>{
        this.LODOP = getLodop();
        this.number++
        if( this.number == 25 ){
          clearInterval(printerTime);
          this.$modal.closeLoading();
        }
        if( this.LODOP ){
          for(var i=0; i<this.LODOP.GET_PRINTER_COUNT(); i++){
            // 打印机名称
            this.printerList.push(this.LODOP.GET_PRINTER_NAME(i))
          }
          this.$modal.closeLoading();
          clearInterval(printerTime);
        }
      }, 200)
    },
		methods: {
      getPrinter(){
        getPrintInfo(this.form).then(response => {
          response.data.forEach(element => {
            if( element.printType == 0 ){
              this.printer.receipt = element.printName
            }else if( element.printType == 1 ){
              this.printer.barCode = element.printName
            }else if( element.printType == 2 ){
              this.printer.label = element.printName
            }else if(  element.printType == 3  ){
              this.printer.query = element.printName
            }else{
              this.printer.exfactory = element.printName
            }
          });
        });
      },
			btn_submit_print(){
        this.printLoading = true;
        let state = false
        if( this.userInfoVO.userType == '01' ){
          if( !this.printer.query ) {
            this.$message.error(`请选择查询记录打印机`);
            state = true
          }
          if( !this.printer.exfactory ) {
            this.$message.error(`请选择出厂单打印机`);
            state = true
          }
        }
        
        if( !this.printer.receipt ) {
          this.$message.error(`请选择小票打印机`);
          state = true
        }
        if( !this.printer.barCode && this.userInfoVO.userType == '02') {
          this.$message.error(`请选择条码打印机`);
          state = true
        }
        if( !this.printer.label && this.userInfoVO.userType == '02' ) {
          this.$message.error(`请选择标签打印机`);
          state = true
        }

        if( state ) return false;
        const primary = [{
          "printName": this.printer.receipt,
          "printType": 0
        },{
          "printName": this.printer.barCode,
          "printType": 1
        },{
          "printName": this.printer.label,
          "printType": 2
        },{
          "printName": this.printer.query,
          "printType": 3
        },{
          "printName": this.printer.exfactory,
          "printType": 4
        }]
        this.$cache.local.remove("printerSeting");
        setPrint(primary).then(response => {
          this.printLoading = false;
          this.$modal.msgSuccess("保存成功");

          this.$cache.local.set("printerSeting", JSON.stringify(primary));
        });
      },
		}
	};
</script>
<style lang="scss" scoped>
.list_printer {
  margin: 30px 0;
  .el-select {
    margin: 20px 0;
  }
}

.btn_box {
  text-align: right;
  .el-button {
    padding: 10px 50px;
  }
}
</style>
