<template>
  <div>
    <div class="divborder newSearchDiv" style="margin:0px;">
      <div class="filter">
        <label>{{$t('100089')}}：</label>
        <Select
          v-model="productname"
          label-in-value
          clearable
          filterable
          remote
          :remote-method="remoteMethod1"
          :loading="loading1"
          class="searchSelect"
        >
          <Option
            v-for="(item,index) in products"
            :value="item.name"
            :key="index"
          >{{ item.name }}({{item.code}})</Option>
        </Select>
      </div>
      <div class="filter">
        <label>{{$t('900934')}}：</label>
        <Select
          v-model="ordertype"
          label-in-value
          clearable
          class="searchSelect"
        >
          <Option value="1">{{$t('900944')}}</Option>
          <Option value="2">{{$t('900943')}}</Option>
          <Option value="3">{{$t('900942')}}</Option>
        </Select>
      </div>
      <div class="filter">
        <label>{{$t('900053')}}：</label>
        <Input class="searchInput" type="text" v-model="orderno"/>
      </div>
      <!-- 次级搜索条件--隐藏 -->
      <div v-show="!morecondition">
        <div class="filter">
          <label>{{$t('5500271')}}：</label>
          <Select v-model="printtype" label-in-value clearable class="searchSelect">
            <!-- <Option :value="1">{{$t('5500272)}}</Option> -->
            <Option :value="2">{{$t('5500273')}}</Option>
          </Select>
        </div>
        <div class="filter">
          <label>{{$t('5500274')}}：</label>
          <Select v-model="labeltype" label-in-value clearable class="searchSelect">
            <Option
            v-for="(item,index) in labeltypes"
            :value="item.dkey"
            :key="index"
          >{{item.dval}}</Option>
          </Select>
        </div>
        <div class="filter">
          <label>{{$t('5500285')}}：</label>
          <Select v-model="printmechine" label-in-value clearable class="searchSelect" @on-change="getPrint">
            <Option
            v-for="(item,index) in printmechines"
            :value="item.name"
            :key="index"
          >{{item.name}}</Option>
          </Select>
        </div>
      </div>
      <div class="searchBtn">
        <Button type="primary" class="twoWord" icon="ios-search" @click="tosearch()">{{$t('1001')}}</Button>
        <Button type="success" class="twoWord" icon="md-add-circle" @click="setSN()">{{$t('5500286')}}</Button>
        <Button type="warning" class="twoWord" icon="ios-print-outline" @click="goprint()">{{$t('100077')}}</Button>
      </div>
      <!-- 更多条件 按钮 -->
      <MoreConbtn @getCon="morecondition = !morecondition" :morecondition="morecondition"></MoreConbtn>
    </div>

    <div class="divborder newTableDiv">
      <Table
          class="tableClass"
          @on-current-change="handleRowChange"
          @on-page-size-change="handlePageSize"
          highlight-row
          :data="tableData1"
          :columns="tableColumns1"
      ></Table>
      <div class="pageDiv">
          <div class="pageDirection">
          <Page
              :total="total"
              :current="page"
              :page-size="pagesize"
              @on-change="changePage"
              show-elevator
              show-sizer
              show-total
              :page-size-opts="[10,20,30,40,100]"
          ></Page>
          </div>
        </div>
    </div>

    <div class="divborder newTableDiv" v-show="detailinfo">
      <Table
          class="tableClass"
          highlight-row
          :data="tableData2"
          :columns="tableColumns2"
          @on-selection-change="handleSelectRow"
      ></Table>
    </div>

    <!-- 打印插件 -->
    <Modal
      class="newModalClass"
      :title="$t('5500098')"
      v-model="downloadexe"
      :styles="{width:'700px'}"
    >
      <div class="modal_body" style="width:700px!important;padding:0 20px;">
        <div style="width:660px;padding-left:5px;">
          <div>
            <div class="filter">
              <a
                href="/static/lodop/CLodop_Setup_for_Win32NT.exe"
              >{{$t('5500099')}}:CLodop_Setup_for_Win32NT.exe</a>
              <br />
              <a href="/static/lodop/install_lodop32.exe">{{$t('5500099')}}:install_lodop32.exe</a>
              <br />
              <a href="/static/lodop/install_lodop64.exe">{{$t('5500099')}}:install_lodop64.exe</a>
            </div>
          </div>
        </div>
      </div>
    </Modal>
  </div>
</template>

<script>
import axios from "@/libs/api.request";
import { getLodop } from '../../../static/Clodop/LodopFuncs';
export default {
  data(){
    return {
      morecondition:false,
      productname:'',
      products:[],
      loading1:false,
      ordertype:'',
      ordertypelist:[],
      orderno:'',
      printtype:'',
      labeltype:'',
      labeltypes:[],
      printmechine:'',
      printmechines:[],
      downloadexe:false,
      total:0,
      page:1,
      pagesize:10,
      tableData1:[],
      tableColumns1:[
        {
          type:'index',
          title:this.$t('1008'),
          key:'xu',
          render:(h,params)=>{
            return h('span',
              params.index+(this.page - 1)*this.pagesize + 1
            )
          }
        },
        {
          title:this.$t('100089'),
          key:'product_code'
        },
        {
          title:this.$t('100088'),
          key:'product_name'
        },
        {
          title:this.$t('900934'),
          key:'workordertype',
          render:(h,params)=>{
            let orderType = '';
            if (params.row.workordertype === 1) {
                orderType = this.$t('900944');
            } else if (params.row.workordertype === 2) {
                orderType = this.$t('900943');
            } else if (params.row.workordertype === 3) {
                orderType = this.$t('900942');
            }
            return h('span',orderType)
          }
        },
        {
          title:this.$t('900053'),
          key:'code'
        },
        {
          title:this.$t('1950517'),
          key:'quantity'
        },
        {
          title:this.$t('5500239'),
          key:'create_time_str'
        }
      ],
      tableData2:[],
      page1:1,
      pagesize1:10,
      detailinfo:false,
      tableColumns2:[
        {
          type:'selection',
          width:80
        },
        {
          type:'index',
          title:this.$t('1008'),
          key:'xu',
          render:(h,params)=>{
            return h('span',
              params.index+(this.page - 1)*this.pagesize + 1
            )
          }
        },
        {
          title:this.$t('100089'),
          key:'productCode'
        },
        {
          title:this.$t('5500287'),
          key:'sn'
        },
        {
          title:this.$t('5500240'),
          key:'printTime'
        }
      ],
      selections:[]
    }
  },
  created(){
    this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    this.tableColumns2 = this.$time.initTableTitle(this.tableColumns2)
    this.iniproducts('') //产品
    this.getlabeltypes() //模板类型
    this.tosearch()

    //延后调取打印机
    setTimeout(() => {
      this.getPMachine()
    }, 2000);
  },
  methods:{
    tosearch(){
      this.page = 1
      this.pagesize = 10
      this.search()
    },
    search(){
      var params = {
        product_name:this.productname,
        draw_number:'',
        code:'',
        ordertype:this.ordertype,
        ordernumber:this.orderno,
        status:1,
        startTime:'',
        endTime:'',
        page:this.page,
        pagesize:this.pagesize
      }
      this.detailobj = ''
      axios.request({
          url: '/cnc/Product/GetPlanList',
          params,
          method: 'get'
      }).then(res => {
          this.total = 0
          this.tableData1 = []
          if(res.data.ret===1 && res.data.data!=null && res.data.data.length >0){
            this.total = res.data.total
            this.tableData1 = res.data.data
          }
      })
    },
    getDetailinfos(){
      var params = {
        productCode:this.detailobj.product_code,
        // woType:this.detailobj.workordertype,
        wo:this.detailobj.code,
        page:1,
        pagesize:9999999
      }
      this.$Spin.show();
      axios.request({
          url: '/acc/Snlist/Getlist',
          params,
          method: 'get'
      }).then(res => {
          this.$Spin.hide();
          this.tableData2 = []
          if(res.data.ret===1 && res.data.data!=null && res.data.data.length >0){
            this.tableData2 = res.data.data
          }
      }).catch((e)=>{
        this.$Spin.hide();
      })
    },
    setSN(){
      //生产序号
      if(this.detailobj===''||this.detailobj===undefined||this.detailobj.id===undefined){
        this.$Message.error(this.$t('1017'))
        return false
      }
      var params = {
        planId:this.detailobj.id,
        ruleType:1
      }
      axios.request({
          url: '/acc/Snlist/CreateSnByWo',
          params,
          method: 'get'
      }).then(res => {
          if(res.data.ret===1){
            this.$Message.success(this.$t('5500288'))
            this.getDetailinfos()
          }
          else
          {
            this.$Message.error(this.$t(res.data.msg))
          }
      })
    },
    getPMachine(){
      let LODOP = getLodop();
      if(LODOP === undefined){
        //请先安装打印插件
        this.downloadexe = true;
        return false;
      }
      //接口打印
      this.getPrintMachines()
    },
    goprint(){
      //获取打印机
      let LODOP = getLodop();
      if(LODOP === undefined){
        //请先安装打印插件
        this.downloadexe = true;
      }
      else
      {
        //查询
        if(this.printtype===""||this.printtype===null||this.printtype===undefined){
          this.$Message.error(this.$t('5500280'))
          return false
        }
        if(this.labeltype===""||this.labeltype===null||this.labeltype===undefined){
          this.$Message.error(this.$t('5500281'))
          return false
        }
        if(this.printmechine===""||this.printmechine===null||this.printmechine===undefined){
          this.$Message.error(this.$t('5500289'))
          return false
        }
        if(this.selections===null||this.selections===undefined||this.selections.length===0){
          this.$Message.error(this.$t('5500290'))
          return false
        }
        var data = {
          labletype:this.labeltype,
          printtype:this.printtype,
          productcode:this.detailobj.product_code,
          sngroup:this.selections
        }
        axios.request({
            url: '/acc/PrintRelation/PrintLable',
            data,
            method: 'post'
        }).then(res => {
            let dataArr = res.data.data
            if(res.data.ret===1 && dataArr!=null&&dataArr!=undefined&& dataArr.length >0){
              LODOP.PRINT_INIT('打印');
              for (var i = 0; i < dataArr.length; i++) {
                LODOP.NewPage();
                let strData = dataArr[i];
//                 let strData = `CT~~CD,~CC^~CT~
// ^XA~TA000~JSN^LT0^MNW^MTT^PON^PMN^LH0,0^JMA^PR3,3~SD25^JUS^LRN^CI0^XZ
// ^XA
// ^MMT
// ^PW831
// ^LL0160
// ^LS0
// ^FO32,96^GFA,01792,01792,00028,:Z64:
// eJztkkGK3EAMRWVqUYtA6gZV1whkUp4jBWaRWSS4mj5AbhRkZjHXUOMDpMIs2jCmFX25B5IThEALjEGfx9eXiuhW/3X9oJp0LS2J6kFCeydEAxOtRFG5Ft0Kl5Pq3CNPG1FgihdoAm6UAm4NPFkziPWJkvYMTpzbAiuabH37HdaaiIqM0mkwzqypmPbdfsNWi2l9PJl2iXNPjSamEk0n57J8BHcJDE2F3gfj6Gt2bppN02CjtuHlyjVyrjunx2a5aGHKxqVO7vf5C/z0mfWe6Nm06BmRrz4+IN/PI0/i+SryK1u+tTw+WL7D+eltL8U+0g4uf1jALQsjdICfadPm+/x2BrecOWqjuPtR2PPdLZjzZNOPDX7OXfN9esGcv14Z0NUv051zo3PyihPufr4X5MtlAScTTggUe7lytezcOItB4e99lidwfXQuvO1znzMdnSu8JvhV3C+q58vG2d1NmzxfxZsIvpcNHLTEI5ri2qCC92LavL+zgrfEeTCNavuTCy3gDlzxPm91q39dvwHbuh3w:DAE1
// ^FO32,0^GFA,01792,01792,00028,:Z64:
// eJztkzFu20AQRT/DYjvPBRTwCIY7FQZ5pZQEQokrsGDn3CDIMVykoKqU8QEMe4FcYNJNIWs9MysFSZnGcKEBBALz8TDz/6yAS737uo405WNItXQ59UgfRJufgBaoMtOcc+AgY+ah4iarxtqP+hPjSIJxW6gWAdH+gjofgnHi3AjurMna10/a0rQgCMktoFwbEoJqv/SzjDSrdiAZgHHHQ53QMMJe9aV17v7KuDHyrWqdoI7GNcG5ho1bdNWE4cQlOPe9cCmpL6xVU64+wOc9fvR5v7l7AG5U27lH9/d1Zf4+J24sFwFF01j9HcPTSv2lDZ9zCQsD3cHzvFt7nsJm2jTl0GTP83ljXP/CVU6oyjzg6Ht+WduevW5PyeY5h+Lv52B7Dls26DQvRCqcONfaCcs8z8X9zWJc39gJfd7+D0dz4Yh7hfBvnj/Y55FzOOdZ7jAl5wK3tc2jSe9X5eJvZru7ao37o51FkC2XbFynWs1kTSnaKP5epsTlnQV7SxziqBp9+5tTc3YHpti+8f/kUpf6z3oFNI8wng==:A748
// ^PQ1,0,1,Y^XZ
// `;
                LODOP.SET_PRINT_MODE("SEND_RAW_DATA_ENCODE","UTF-8");//UTF-8 UTF-7 UNICODE ANSI UTF-16 UTF-16BE GBK BIG5 EUC-JP
                LODOP.SEND_PRINT_RAWDATA(strData);
              }
              this.getDetailinfos();
            }
            else
            {
              this.$Message.error(this.$t(res.data.msg))
            }
        })
      }
    },
    getPrintMachines(){
      let LODOP = getLodop();
      //获取本地打印机的个数
      let printnum = LODOP.GET_PRINTER_COUNT();
      if(printnum > 0){
        for(let i=0;i<9;i++){
          let name = LODOP.GET_PRINTER_NAME(i);
          this.printmechines.push({
            index:i,
            name:name
          })
        }
      }
      //获取默认打印机
      let pname = LODOP.GET_PRINTER_NAME(-1);
      this.printmechine = pname
    },
    getPrint(info){
      //设置默认打印机
      let pname = info.value;
      LODOP.SET_PRINT_MODE("WINDOW_DEFPRINTER",pname);
    },
    iniproducts(name) {
      var params = {
        name: name,
        count: 50,
      };
      axios
        .request({
          url: "/config/ProjectProduct/GetProductsByCode",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1 && res.data.data.length > 0) {
            this.products = res.data.data;
          }
        });
    },
    remoteMethod1(name) {
      if (name !== "") {
        this.loading1 = true;
        setTimeout(() => {
          this.loading1 = false;
          this.iniproducts(name);
        }, 200);
      } else {
        this.iniproducts("");
      }
    },
    getlabeltypes(){
      axios
        .request({
          url: "/config/Dict/GetDictByCat?cat=labeltype",
          method: "get",
        })
        .then((res) => {
          this.labeltypes = []
          if (res.data.ret === 1 && res.data.data.length > 0) {
            this.labeltypes = res.data.data;
          }
        });
    },
    handleRowChange(currentRow, oldCurrentRow) {
        this.detailobj = currentRow;
        this.detailinfo = true
        this.getDetailinfos()
    },
    handleSelectRow: function (data) {
        this.selections = [];
        if(data.length > 0){
          for(var i in data){
            this.selections.push({
              sn:data[i].sn
            });
          }
        }
        
    },
    changePage(value) {
        this.page = value;
        this.search();
    },
    handlePageSize(value) {
        this.pagesize = value;
        this.search();
    },
  }
}
</script>
<style lang="less" scoped>

</style>