<template>
  <div>
    <Toptitle title="查看">
      <Button
        @click="handleMatchedSelectAll()"
        type="primary"
        :ghost="!isMatchedSelectAll"
        style="margin-right: 10px"
        >{{ isMatchedSelectAll ? "取消选中" : "全部选中" }}</Button
      >
       <Button type="primary" style="margin-right: 10px" @click="print_chip"
        >打印芯片</Button
      >
       <!-- <Button type="primary" style="margin-right: 10px" @click="test"
        >测试查看</Button
      > -->
      <Button @click="back" type="primary" ghost style="margin-right: 10px"
        >返回</Button
      >
    </Toptitle>
    <Row style="padding: 10px;font-size: 18px;">
      <Col span="6">
        <span>项目编号：</span><span>{{ project_number }}</span>
      </Col>
      <Col span="6">
        <span>项目名称：</span><span>{{ project_name }}</span>
      </Col>
    </Row>
    <div class="context-tabs">
      <Row type="flex" align="middle" style="padding:10px 0">
        <Col span="4">
          <span>图号：</span>
          <span>
            <Select
              filterable
              multiple
              filter-by-label
              clearable
              transfer
              v-model="matchedInfo.url_number"
              :max-tag-count="2"
              size="small"
              style="width: 150px"
            >
              <Option
                v-for="(item, index) in urlMatchedList"
                :key="index"
                :label="item"
                :value="item"
              ></Option>
            </Select>
          </span>
        </Col>
        <Col span="5">
        <span>图号批量查询：</span>
        <span>
          <Input clearable v-model="matchedInfo.url_numbers" placeholder="请输入" size="small"
            style="width: 150px"/>
        </span>
      </Col>
        <Col span="5">
          <span>产品名称：</span>
          <span>
            <Select
              filterable
              multiple
              filter-by-label
              clearable
              transfer
              v-model="matchedInfo.product_id"
              :max-tag-count="2"
              size="small"
              style="width: 150px"
            >
              <Option
                v-for="(item,index) in productMatchedList"
                :key="index"
                :label="item.title"
                :value="item.id"
              ></Option>
            </Select>
          </span>
        </Col>
          <Col span="5">
          <span>区域名称：</span>
          <span>
            <Select
              filterable
              multiple
              filter-by-label
              clearable
              transfer
              v-model="matchedInfo.layer"
              :max-tag-count="2"
              size="small"
              style="width: 150px"
            >
              <Option
                v-for="(item, index) in layerList"
                :key="index"
                :label="item"
                :value="item"
              ></Option>
            </Select>
          </span>
        </Col>
        <Col span="2">
          <Button @click="initData(matchedInfo)" size="small" type="primary">
            搜索
          </Button>
        </Col>
      </Row>
      <div
        v-for="(matched_info,index) in matchedList"
        :key="index"
        class="matched-block"
      >
        <Row type="flex" justify="space-between" align="top">
          <Col style="display:flex;justify-content: space-around;">
              <Checkbox
                v-model="matched_info.isSelect"
                @on-change="(e) => handleMatchedSelect(matched_info, e)"
                >选择</Checkbox
              >
            <span>图号：{{ matched_info.url_number }}</span>
          </Col>
          <Col span="3">
            <span>产品名称：{{ matched_info.product_title }}</span>
          </Col>
          <Col span="3">
            <span>区域：{{ matched_info.layer }}</span>
          </Col>
          <Col style="display:flex;justify-content: space-between;" span="8">
            <div>
              总计
              <span style="color:red">{{ matched_info.total }}</span>
              条数据
            </div>
            <div>
              <Button
                @click="handleShowCurrencyMatched(matched_info,index)"
                size="small"
                type="text"
                >{{ matched_info.isCurrenct ? "收缩" : "展开" }}</Button
              >
              <Icon
                size="20"
                :type="
                  matched_info.isCurrenct
                    ? 'md-arrow-dropdown'
                    : 'md-arrow-dropright'
                "
                style="vertical-align: middle;"
              />
            </div>
          </Col>
        </Row>
        <Row style="margin-top:20px" v-if="matched_info.isCurrenct">
          <Col span="4">
            <span>房号：</span>
            <span>
              <Select
                filterable
                multiple
                transfer
                clearable
                filter-by-label
                v-model="matchedInfoDetail.number"
                :max-tag-count="2"
                size="small"
                style="width: 150px"
              >
                <Option
                  v-for="(item, index) in numberMatchedList"
                  :key="index"
                  :label="item.number"
                  :value="item.number"
                ></Option>
              </Select>
            </span>
          </Col>
           <Col span="4">
            <span>行号：</span>
            <span>
              <Select
                filterable
                multiple
                transfer
                clearable
                filter-by-label
                v-model="matchedInfoDetail.sort"
                :max-tag-count="2"
                size="small"
                style="width: 150px"
              >
                <Option
                  v-for="(item, index) in sortList"
                  :key="index"
                  :label="item"
                  :value="item"
                ></Option>
              </Select>
            </span>
          </Col>
           <Col span="4">
            <span>组合名称：</span>
            <span>
              <Select
                filterable
                multiple
                transfer
                clearable
                filter-by-label
                v-model="matchedInfoDetail.compose_name"
                :max-tag-count="2"
                size="small"
                style="width: 150px"
              >
                <Option
                  v-for="(item, index) in compose_name_list"
                  :key="index"
                  :label="item"
                  :value="item"
                ></Option>
              </Select>
            </span>
          </Col>
          <Col span="4">
            <span>部件名称：</span>
            <span>
              <Select
                filterable
                multiple
                transfer
                clearable
                filter-by-label
                v-model="matchedInfoDetail.parts_id"
                :max-tag-count="2"
                size="small"
                style="width: 150px"
              >
                <Option
                  v-for="(item, index) in partNameMatchedList"
                  :key="index"
                  :label="item.title"
                  :value="item.id"
                ></Option>
              </Select>
            </span>
          </Col>
          <Col span="2">
            <Button
              @click="getMatchedDetailList(matched_info,matchedInfoDetail)"
              size="small"
              type="primary"
            >
              搜索
            </Button>
          </Col>
        </Row>
        <div v-if="matched_info.isCurrenct">
            <Table
              :columns="matchedSheetTableColumns"
              border
              :max-height="500"
              :data="matchedDetailList"
              @on-select='select'
              @on-select-cancel='selectCancel'
              ref='table'
              @on-select-all='selectAll($event,null,1)'
              @on-select-all-cancel='selectAllCancel($event,null,1)'
            >
            </Table>
            <span>总计{{matchedDetailList.length?matchedDetailList.length:0}}条记录</span>
        </div>
      </div>
    </div>
    <Modal v-model="processModal" title="下生产">
      <div>
        <div class="process_modal">
          <span>生产人员:</span>
          <Select
            v-model="process_man"
            filterable
            filter-by-label
            clearable
            style="width: 150px"
          >
            <Option
              v-for="(item,index) in processManList"
              :key="index"
              :label="item.nickname"
              :value="item.id"
            ></Option>
          </Select>
        </div>
        <div class="process_modal">
          <span>选择时间:</span>
          <DatePicker
            type="date"
            v-model="process_start_time"
            placeholder="年/月/日"
            style="width: 150px"
          ></DatePicker>
          至
          <DatePicker
            type="date"
            v-model="process_end_time"
            placeholder="年/月/日"
            style="width: 150px"
          ></DatePicker>
        </div>
      </div>
      <div slot="footer">
        <Button
          @click="processModal = false"
          type="primary"
          ghost
          style="margin-right: 10px"
          >取消</Button
        >
        <Button
          @click="handleProcess"
          type="primary"
          style="margin-right: 10px"
          :disabled="process_control"
          >确定</Button
        >
      </div>
    </Modal>
    <Modal v-model="showDel" title="" @on-ok='important_del' :closable='false' style="text-align:center" :width='20'>
      <div style="margin-top:30px"><span style="margin-right:10px">用户:</span><Input v-model="delData.user_name" style="width:70%"></Input></div>
      <div style="margin-top:30px"><span style="margin-right:10px">密码:</span><Input v-model="delData.password" style="width:70%"></Input></div>
    </Modal>
  </div>
</template>

<script>
// 这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
// 例如：import 《组件名称》 from '《组件路径》';
import $ from "jquery";
import { ContainerMixin } from 'vue-slicksort';
export default {
  name: "",
  components: {},
  props: {},
  // import引入的组件需要注入到对象中才能使用
  data() {
    // 这里存放数据
    return {
      compose_name_list:[],
      sortList:[],
      delData:{
        user_name:'',
        password:'',
      },
      layerList:[],
      project_number: this.$route.query.project_number,
      project_name: this.$route.query.project_name,
      isMatchedSelectAll: false,
      matchedInfoDetail:{
        compose_name:[],
         sort:[],
         parts_id:[],
         number:[]
      },
      matchedInfo: {
        url_number: [],
        product_id: [],
        layer:[],
        url_numbers:'',
      },
      urlMatchedList: [],
      productMatchedList: [],
      regionMatchedList: [],
      numberMatchedList: [],
      partNameMatchedList: [],
      matchedDetailList: [],
      matchedList: [],
      matchedSheetTableColumns: [
        {type:'selection',align:'center',width:60},
        { title: "序号", align: "center", type: "index", resizable: true,
                        minWidth: 80 }, 
                        { title: "行号", align: "center", key: "sort",  resizable: true,
                        minWidth: 120 },
        { title: "区域名称", align: "center", key: "area",  resizable: true,
                        minWidth: 160 },
        { title: "房号", align: "center", key: "number",  resizable: true,
                        minWidth: 160 },
        { title: "组合名称", align: "center", key:'compose_name', resizable: true,
                        minWidth: 100 },
        { title: "部件名称", align: "center", key: "part_title",  resizable: true,
                        minWidth: 180 },
                { title: "精裁面积", align: "center", key: "ext_1",  resizable: true,
                        minWidth: 150 },
                         { title: "贴皮面积", align: "center", key: "skin_area",  resizable: true,
                        minWidth: 150 },         
        {
          title: "精裁尺寸",
          align: "center",
          resizable: true,
                        minWidth: 150,
          render: (h, params) => {
            return h("span", {}, params.row.measure);
          },
         }, 
          {
          title:"毛料尺寸",
          align:'center',
          resizable:true,
          width:150,
          key:"wool_size"
        },
         { title: "打印芯片", align: "center", key: "chip_state",  resizable: true,
                        minWidth: 80 ,  render: (h, params) => {
            return h("span", {}, params.row.chip_state==0?'否':'是');
          }},
           { title: "工艺要求", align: "center", key: "remark",  resizable: true,
                        minWidth: 180 },
          { title: "操作", align: "center", key: "set",  resizable: true,
                        minWidth: 180 ,render:(h,params)=>{
                          const {row} = params;
                          return h('a',{on:{'click':()=>{
                                this.print_chip(row,1)
                          }}},'打印芯片')
                        }},
      ], //匹配表头
      processModal: false,
      process_man: "",
      processManList: [],
      process_start_time: "",
      process_end_time: "",
      process_control: false,
      cut_order_product_ids: [],
      matchedSelectedList: [],
      delList:[],
      showDel:false,
      selectIndex:'',
      selectArr:[],
    };
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.axios("/api/user").then(
      (res) => (this.processManList = res.data.data)
    );
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.initData();
  },
  methods: {
    test(){
  console.log(this.selectArr)
    },
    selectAll(selection,arr,type){
     let ids =[];
     if(arr){
   arr.forEach(m=>{
     ids.push(m.id)
   })
     }else{
        this.matchedDetailList.forEach(m=>{
          ids.push(m.id)
      })}
      for(let i =0;i<this.matchedSelectedList.length;i++){
       if(ids.indexOf(this.matchedSelectedList[i].id)>=0){
         this.matchedSelectedList.splice(i,1);
         i--;
       }
     }
     if(arr){
        this.matchedSelectedList.push(...arr);
     }else{
     this.matchedSelectedList.push(...this.matchedDetailList);}
     
     if(typeof this.selectIndex === 'number'){
      let ids = [];
      if(type){
     this.matchedList[this.selectIndex].isSelect = true;
     this.selectArr.forEach(v=>{
        ids.push(v.id)
     })
     if(ids.indexOf(this.matchedList[this.selectIndex].id)<0){
     this.selectArr.push(this.matchedList[this.selectIndex]);}
     }}
     this.$forceUpdate();
    },
    selectAllCancel(selection,arr,type){
      let ids =[];
      if(arr){
        arr.forEach(v=>{
           ids.push(v.id)
        })
      }else{
        this.matchedDetailList.forEach(m=>{
          ids.push(m.id)
      })}
       console.log(ids);
     for(let i =0;i<this.matchedSelectedList.length;i++){
       if(ids.indexOf(this.matchedSelectedList[i].id)>=0){
         this.matchedSelectedList.splice(i,1);
         i--;
       }
     }

      // this.matchedSelectedList.forEach((v,index)=>{
      //   if(ids.indexOf(v.id)>=0){
      //     this.matchedSelectedList.splice(index,1);
      //     index--;
      //     console.log(this.matchedSelectedList)
      //   }
      // })
    
     if(typeof this.selectIndex === 'number'){
      if(type){
       this.matchedList[this.selectIndex].isSelect = false;
       this.selectArr.forEach((m,index)=>{
         if(m.id == this.matchedList[this.selectIndex].id){
           this.selectArr.splice(index,1);
         }
       })}
       this.$forceUpdate();
     }
    },
    select(selection,row){
      let ids = [];
      this.matchedSelectedList.forEach(v=>{
       ids.push(v.id)
      })
      if(ids.indexOf(row.id)<0){
        this.matchedSelectedList.push(row);
      }
       console.log(selection)
       if(selection.length == this.matchedDetailList.length){
         this.matchedList[this.selectIndex].isSelect = true;
         this.selectArr.push(this.matchedList[this.selectIndex]);
         this.$forceUpdate();
       }else{
         this.matchedList[this.selectIndex].isSelect = false;
          this.selectArr.forEach((m,index)=>{
         if(m.id == this.matchedList[this.selectIndex].id){
           this.selectArr.splice(index,1);
         }
       })
         this.$forceUpdate();
       }
       console.log(this.matchedSelectedList);
    },
    selectCancel(selection,cancelRow){
    //  this.matchedList[this.selectIndex].isSelect = false;
      this.matchedSelectedList.forEach((v,index)=>{
        if(v.id == cancelRow.id){
          this.matchedSelectedList.splice(index,1)
        }
      })
        if(selection.length == this.matchedDetailList.length){
         this.matchedList[this.selectIndex].isSelect = true;
         this.$forceUpdate();
       }else{
         this.matchedList[this.selectIndex].isSelect = false;
           this.selectArr.forEach((m,index)=>{
         if(m.id == this.matchedList[this.selectIndex].id){
           this.selectArr.splice(index,1);
         }
       })
         this.$forceUpdate();
       }
      console.log(this.matchedSelectedList)
    },
     hexCharCodeToStr(hexCharCodeStr) {
     let trimedStr = hexCharCodeStr.trim();
     let rawStr = trimedStr.substr(0,2).toLowerCase() === "0x"?trimedStr.substr(2):trimedStr;
     let len = rawStr.length;
      if(len % 2 !== 0) {
        alert("Illegal Format ASCII Code!");
        return "";}
       let curCharCode;
       let resultStr = [];
       for(var i = 0; i < len;i = i + 2) {
        curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
          resultStr.push(String.fromCharCode(curCharCode));
       }
       return resultStr.join("");
},
      handlePrintChipsUsb(contents, type) {
        console.log(contents)
      this.axios.get("/api/get_print_url").then((res) => {
        let data = {};
        // let url = "http://192.168.0.145:888/postek/print"; //request_url
        let url = `http://${res.data.request_url}:888/postek/print`; //request_url
        data.reqParam = "1"; //这个一般不用改，如需改，查api
        let printparamsJsonArray = [];
        // let IP = "192.168.1.58";//print_url
        // let IP = "192.168.0.199"; //print_url
        printparamsJsonArray.push({ PTK_OpenUSBPort: 255 });
        printparamsJsonArray.push({ PTK_ClearBuffer: "" });
        printparamsJsonArray.push({ PTK_SetDirection: "B" });
        printparamsJsonArray.push({ PTK_SetPrintSpeed: "4" });
        printparamsJsonArray.push({ PTK_SetDarkness: "10" });
        printparamsJsonArray.push({
          PTK_SetLabelHeight: 624 + "," + 24 + ",0,false",
        });
        printparamsJsonArray.push({ PTK_SetLabelWidth: 1080 });
        // 打印类型 1江山 2贝斯特
        if (type == 1) {
          contents.map((content) => {
            //江山打印格式
            //上
            const printContent_l1 = `单号：${content.order_no}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "100,40,60,0,微软雅黑,1,700,0,0,0," + printContent_l1,
            });
            const printContent_l1_2 = `型号：${content.product_title}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "600,40,60,0,微软雅黑,1,700,0,0,0," + printContent_l1_2,
            });
            const printContent_l2 = `尺寸：${content.measure}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "100,110,60,0,微软雅黑,1,700,0,0,0," + printContent_l2,
            });
            const printContent_l2_2 = `工艺：${content.process_title}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "600,110,60,0,微软雅黑,1,700,0,0,0," + printContent_l2_2,
            });
            const printContent_l3 = `颜色：${content.color_title}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "100,250,60,0,微软雅黑,1,700,0,0,0," + printContent_l3,
            });
            const printContent_l3_2 = `部件：${content.part_title}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "100,180,60,0,微软雅黑,1,700,0,0,0," + printContent_l3_2,
            });
            //下
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "100,435,45,0,微软雅黑,1,700,0,0,0," + printContent_l1,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "600,435,45,0,微软雅黑,1,700,0,0,0," + printContent_l1_2,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "100,480,45,0,微软雅黑,1,700,0,0,0," + printContent_l2,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "600,480,45,0,微软雅黑,1,700,0,0,0," + printContent_l2_2,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "100,525,45,0,微软雅黑,1,700,0,0,0," + printContent_l3,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "600,525,45,0,微软雅黑,1,700,0,0,0," + printContent_l3_2,
            });
            printparamsJsonArray.push({
              PTK_RWRFIDLabel:
                "1,0,0," + content.chip.length / 2 + ",1," + content.chip,
            });
            printparamsJsonArray.push({ PTK_PrintLabel: "1,1" });
          });
        } else {
          contents.map((content) => {
            //贝斯特打印格式
            //上
            const printContent_l1 = `项目：${content.client_name}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "150,40,60,0,微软雅黑,1,700,0,0,0," + printContent_l1,
            });
            const printContent_l1_2 = `区域：${
              content.house ? content.house + "-" : ""
            }${content.layer ? content.layer : ""}${
              content.position ? content.position : ""
            }`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "650,40,60,0,微软雅黑,1,700,0,0,0," + printContent_l1_2,
            });
            const printContent_l2 = `房号：${content.number_detail}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "150,110,60,0,微软雅黑,1,700,0,0,0," + printContent_l2,
            });
            const printContent_l2_2 = `产品：${content.product_title}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "650,110,60,0,微软雅黑,1,700,0,0,0," + printContent_l2_2,
            });
            const printContent_l3 = `图号：${content.url_number}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "150,180,60,0,微软雅黑,1,700,0,0,0," + printContent_l3,
            });
            const printContent_l3_2 = `部件：${content.part_title}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "650,180,60,0,微软雅黑,1,700,0,0,0," + printContent_l3_2,
            });
            const printContent_l4 = `木皮：${content.color_title}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "150,320,60,0,微软雅黑,1,700,0,0,0," + printContent_l4,
            });
            const printContent_l4_2 = `尺寸：${content.measure}`;
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "150,250,60,0,微软雅黑,1,700,0,0,0," + printContent_l4_2,
            });
            let space_str = '\u0000'
             let printContent_l5_data=this.hexCharCodeToStr(content.chip);
             printContent_l5_data = printContent_l5_data.replace(space_str,'')
             const printContent_l5 = `芯片编码：${printContent_l5_data}`;
            //  console.log(printContent_l5_data)
            printparamsJsonArray.push({
              PTK_DrawText_TrueTypeEx:
                "970,330,25,0,微软雅黑,1,700,0,0,0,10,0,0,0," + printContent_l5,
            });
            //下
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "150,435,45,0,微软雅黑,1,700,0,0,0," + printContent_l1,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "650,435,45,0,微软雅黑,1,700,0,0,0," + printContent_l1_2,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "150,480,45,0,微软雅黑,1,700,0,0,0," + printContent_l2,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "650,480,45,0,微软雅黑,1,700,0,0,0," + printContent_l2_2,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "150,525,45,0,微软雅黑,1,700,0,0,0," + printContent_l3,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "650,525,45,0,微软雅黑,1,700,0,0,0," + printContent_l3_2,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "150,570,45,0,微软雅黑,1,700,0,0,0," + printContent_l4,
            });
            printparamsJsonArray.push({
              PTK_DrawText_TrueType:
                "650,570,45,0,微软雅黑,1,700,0,0,0," + printContent_l4_2,
            });
              printparamsJsonArray.push({
              PTK_DrawText_TrueTypeEx:
                "970,530,25,0,微软雅黑,1,700,0,0,0,10,0,0,0," + printContent_l5,
            });
            // 17，35 420
            printparamsJsonArray.push({
              PTK_RWRFIDLabel:
                "1,0,0," + content.chip.length / 2 + ",1," + content.chip,
            });
            printparamsJsonArray.push({ PTK_PrintLabel: "1,1" });
          });
        }
        printparamsJsonArray.push({ PTK_CloseUSBPort: "" });
        data.printparams = printparamsJsonArray;
        let sub_data = JSON.parse(JSON.stringify(data));
        sub_data.printparams = JSON.stringify(sub_data.printparams);
        let _this = this;
        $.ajax({
          type: "post",
          url,
          data: sub_data,
          dataType: "json",
          timeout: 5000,
          success: function(result) {
            // _this.getChipDetail();
            if (result.retval == "0") {
              _this.$Message.success("发送成功");
            } else {
              _this.$Message.error("发送失败，返回结果：" + result.msg);
            }
          },
          error: function() {
            // _this.getChipDetail();
          },
        });
      });
    },
    print_chip(row,type){

        if(type){
               this.axios.post('/api/station_print',{matching_type:1,id:row.id,num:1,order_no:this.$route.query.project_number,product_id:row.product_id,rows:[],type:1}).then(resp=>{
             if(resp.code == 200){
                 this.axios.get("/api/get_print_local_url").then((res) => {
            if (res.code == 200) {
              if (res.data.state) {
                this.handlePrintChipsUsb(resp.data, res.data.type);
              } 
            }
          });
          } })
        }else{
         this.matchedSelectedList.forEach(v=>{
           this.axios.post('/api/station_print',{matching_type:1,id:v.id,num:1,order_no:this.$route.query.project_number,product_id:v.product_id,rows:[],type:1}).then(resp=>{
             if(resp.code == 200){
                 this.axios.get("/api/get_print_local_url").then((res) => {
            if (res.code == 200) {
              if (res.data.state) {
                this.handlePrintChipsUsb(resp.data, res.data.type);
              } 
            }
          });
          } })
         })
        }
    },
    important_del(){
       this.axios.post('/api/delete_cut_order_product',{id:[...this.delList],...this.delData}).then(res=>{
         if(res.code == 200){
           this.$Message.success(res.msg);
           this.delList = [];
           this.isMatchedSelectAll=false;
           this.initData();
         }
       })
    },
    handleMatchedSelectAll(){
 this.isMatchedSelectAll = !this.isMatchedSelectAll;
 this.matchedSelectedList = [];
      this.matchedList.map((v) => {
          v.isSelect = JSON.parse(JSON.stringify(this.isMatchedSelectAll));
         this.handleMatchedSelect(v,this.isMatchedSelectAll);

      });
      //  this.$nextTick(()=>{this.matchedDetailList.forEach(v=>{
      //           v._checked = true;
      //         })})
      if(!this.isMatchedSelectAll){
          let ids = [];
        this.matchedList.forEach(element => {
          ids.push(element.id);
        });
         for(let i =0;i<this.selectArr.length;i++){
       if(ids.indexOf(this.selectArr[i].id)>=0){
         this.selectArr.splice(i,1);
         i--;
       }
     }
      }else{
        let ids = [];
        this.selectArr.forEach(element => {
          ids.push(element.id);
        });
       this.matchedList.forEach(v=>{
         if(ids.indexOf(v.id)<0){
            this.selectArr.push(v)
         }
       })
      }
    },
    handleMatchedSelect(row, e) {
      row.isSelect = e;
       let flag = true;
      if(e){
       this.selectArr.push(row);
       this.axios.post('/api/change_match_detail',{layer:row.layer,order_no:this.$route.query.project_number,url_number:row.url_number}).then(res=>{
              
             if(row.isCurrenct){
                this.matchedDetailList = res.data.list.data;
                this.matchedDetailList.forEach(v=>{
                  v._checked = true;
                  
                })}
              this.selectAll(null,res.data.list.data);
            }) 
      
      }else{
          flag = false;
            this.selectArr.forEach((m,index)=>{
         if(m.id == row.id){
           this.selectArr.splice(index,1);
         }
       })
          this.axios.post('/api/change_match_detail',{layer:row.layer,order_no:this.$route.query.project_number,url_number:row.url_number}).then((res)=>{
        
           if(row.isCurrenct){
            this.matchedDetailList = res.data.list.data;
           } 
           this.selectAllCancel(null,res.data.list.data);
            })
      }
      this.isMatchedSelectAll = flag;
      this.cut_order_product_ids = JSON.parse(JSON.stringify(this.matchedSelectedList));
    },
MatchedSelect(row,e){
  row.del_isSelect = e;
      console.log(row);
      console.log(this.matchedList)
      this.delList = [];
  this.matchedList.forEach(v=>{
    if(v.del_isSelect){
          this.delList.push(v.id);
    }
  })

},
    back() {
      this.$router.go(-1);
    },
    handleProcess() {
      this.process_control = true;
      console.log(this.cut_order_product_ids);
      this.axios({
        method: "post",
        url: "/api/bst_pull",
        data: {
          cut_order_product_ids: this.cut_order_product_ids,
          process_man: this.process_man,
          process_start_time: this.func.replaceDateNoHMS(
            this.process_start_time
          ),
          process_end_time: this.func.replaceDateNoHMS(this.process_end_time),
        },
      }).then((res) => {
        if (res.code == 200) {
          this.$Message.success(res.msg);
          this.processModal = false;
          this.matchedSelectedList = [];
          this.initData();
        }
        setTimeout(() => {
          this.process_control = false;
        }, 500);
      });
    },
    initData(row) {
      console.log(this.matchedList)
      this.axios
        .post("/api/change_match_list", {
            order_no:this.$route.query.project_number,
           ...row
        })
        .then((res) => {
          // this.matchedList = [];
          res.data.list.map((v) => {
            v.isCurrenct = false;

          });
      
          this.urlMatchedList = res.data.url_number;
          this.productMatchedList = res.data.product_list;
          this.layerList = res.data.layer;
          this.matchedList = res.data.list;

         this.matchedList.forEach((v,index)=>{
            this.selectArr.forEach(m=>{
              if(v.url_number===m.url_number){
                 if(v.layer===m.layer){
                   this.matchedList[index].isSelect = true;
                   console.log(5555555)
                 }
              }
            })
         })
             });
    },
    handleHiddenMatchedDetail(row) {
      if (row.isHidden) {
        row.isHidden = !row.isHidden;
      } else {
        row.isHidden = true;
      }
      this.$forceUpdate();
    },
    handleShowCurrencyMatched(row,index) {
      
      if (row.isCurrenct) {
        this.selectIndex = '';
        row.isCurrenct = !row.isCurrenct;
      } else {
          this.matchedInfoDetail={
        compose_name:[],
         sort:[],
         parts_id:[],
         number:[]
      };
        this.selectIndex = index;
        this.matchedList.map((v) => (v.isCurrenct = false));
        row.isCurrenct = true;
      }
      this.getMatchedDetailList(row);
    },
    getMatchedDetailList(row,arr) {
      row.isCurrenct &&
        this.axios
          .post("/api/change_match_detail", {
           layer: row.layer,
           url_number:row.url_number,
            order_no:this.$route.query.project_number,
            ...arr
          })
          .then((res) => {
            this.regionMatchedList = res.data.region;
           this.sortList = res.data.sort_list;
          this.compose_name_list = res.data.compose_name_list;
            this.numberMatchedList = res.data.house_list;
            this.partNameMatchedList = res.data.part_list;
            this.matchedDetailList = res.data.list.data;
            if(row.isSelect){
              this.matchedDetailList.forEach(v=>{
                v._checked = true;
              })
            }else{
              this.matchedDetailList.forEach(v=>{
                this.matchedSelectedList.forEach(m=>{
                  if(v.id==m.id){
                    v._checked=true;
                  }
                })
              })
            }

          });
    },
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {}, // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>

<style lang="scss" scoped>
.context-tabs {
  position: relative;
  max-height: 650px;
  overflow: hidden;
  overflow-y: auto;
  padding: 15px;
  margin: 10px;
  margin-bottom: 30px;
  border-radius: 5px;
  box-shadow: 1px 1px 5px 1px #999;
}
.matched-block {
  padding: 15px;
  margin-bottom: 30px;
  background-color: #e9ecef;
  border-radius: 5px;
}
.process_modal {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 10px;
}
/deep/.del_check .ivu-checkbox .ivu-checkbox-inner{
  border-radius: 50%;
}
</style>
