<template>
  <div class="testdiv">
    <!-- 查询条件 -->
    <div class="divborder newSearchDiv" style="margin:0px;">
      <div class="filter">
        <label>{{$t('100088')}}：</label>
        <Input class="searchInput" type="text" v-model="productName" />
      </div>
      <div class="filter">
        <label>{{$t('100090')}}：</label>
        <Input class="searchInput" type="text" v-model="drawingNo" />
      </div>
      <div class="filter">
        <label>{{$t('900337')}}：</label>
        <Input class="searchInput" type="text" v-model="productionOrderNo" />
      </div>
      <div class="filter">
        <label>{{$t('99200041')}}：</label>
        <Input class="searchInput" type="text" v-model="workOrderNo" />
      </div>
      <!-- 次级搜索条件--隐藏 -->
      <div v-show="!morecondition">
        <div class="filter">
          <label>{{$t('200002')}}：</label>
          <Select v-model="status" clearable class="searchSelect">
            <Option value>{{$t('1014')}}</Option>
            <Option value="0">{{$t('900938')}}</Option>
            <Option value="1">{{$t('900937')}}</Option>
            <Option value="2">{{$t('500096')}}</Option>
            <Option value="3">{{$t('900936')}}</Option>
            <Option value="4">{{$t('900935')}}</Option>
          </Select>
        </div>
        <div class="filter">
          <label>{{$t('1134')}}：</label>
          <Select v-model="status1" clearable class="searchSelect">
            <Option value="1">{{$t('1000020')}}</Option>
            <Option value="0">{{$t('1000021')}}</Option>
          </Select>
        </div>
        <div class="filter">
          <label>{{$t('500030')}}：</label>
          <DatePicker
            type="daterange"
            :value="dateTime"
            placeholder
            class="searchInput middleWidth"
            @on-change="selectdate"
          ></DatePicker>
        </div>
      </div>
      <div class="searchBtn">
        <Button
          type="primary"
          class="twoWord"
          icon="ios-search"
          @click="search(1,10)"
        >{{$t('1001')}}</Button>
        <Button type="success" class="twoWord" icon="md-add-circle" @click="add()">{{$t('900947')}}</Button>
        <Button
          type="warning"
          class="twoWord intervalRight"
          icon="md-close-circle"
          @click="end()"
        >{{$t('900948')}}</Button>
        <Button type="primary" class="fourWord" icon="ios-print" @click="print()">{{$t('900949')}}</Button>
        <!-- <Button type="primary" class="fourWord" @click="lookStock()">查看库存</Button> -->
      </div>
      <!-- 更多条件 按钮组件 -->
      <MoreConbtn @getCon="morecondition = !morecondition" :morecondition="morecondition"></MoreConbtn>
    </div>

    <div class="divborder newTableDiv">
      <Table
        class="tableClass"
        @on-current-change="handleRowChange"
        highlight-row
        :data="tableData"
        :columns="tableColumns"
        @on-selection-change="handleSelect"
      ></Table>
      <div class="pageDiv">
        <div class="pageDirection">
          <Page
            :total="total"
            :current="page"
            @on-change="changePage"
            show-elevator
            show-sizer
            show-total
            @on-page-size-change="handlePageSize"
            :page-size-opts="[10,20,30,40,100]"
          ></Page>
        </div>
      </div>
    </div>
    <Modal
      class="newModalClass"
      :title="$t('900947')"
      v-model="addmodal"
      :styles="{width:'1100px'}"
    >
      <div class="modal_body" style="padding-right:40px">
        <div class="modaltop">
          <div>订单数量：{{detailobj.quantity}}</div>
          <div style="margin-left:10px">已排产数量：{{detailobj.has_schedule}}</div>
          <div class="filter">
            <label>本次排产数量：</label>
            <Input class="searchInput" type="number" min="0" v-model="currentNum" />
          </div>
          <div class="filter">
            <label>批次数：</label>
            <InputNumber class="searchInput" type="number" :min="0" v-model="batchNum" />
          </div>
          <Button type="primary" @click="addBatch()">{{$t('1011')}}</Button>
          <Checkbox v-model="rest" style="width:100px!important;margin-left:10px">排除休息时间</Checkbox>
          <Button type="primary" @click="exclude()">一键排程</Button>
        </div>
        <div class="modaltop">
          <div style="width:25%">产品编号(料号)：{{detailobj.product_code}}</div>
          <div style="width:25%">产品名称：{{detailobj.product_name}}</div>
          <div style="width:25%">图纸号：{{detailobj.draw_number}}</div>
          <div style="width:25%">交付时间：{{detailobj.delivery_time_str}}</div>
        </div>
        <div>
          <Table style="margin-top:10px" :data="addTable1" :columns="addColumn1" height="150"></Table>
          <Table
            style="margin-top:10px;overflow:visible"
            :data="addTable2"
            :columns="addColumn2"
            height="200"
          ></Table>
        </div>
      </div>
      <div slot="footer">
        <Button type="text" size="large" @click="addmodal=false" v-text="$t('1033')">取消</Button>
        <Button type="primary" size="large" :loading="addloading" @click="adddata">
          <span v-if="!addloading">{{$t('1011')}}</span>
          <span v-else>Loading...</span>
        </Button>
      </div>
    </Modal>
    <!-- 打印 -->
    <Modal class="newModalClass" title="打印工单" v-model="printmodal" :styles="{width:'750px'}">
      <div class="modal_body" style="padding-right:40px">
        <div style="width:680px;" id="print">
          <div style="width:680px;" v-for="(item,index) in printList" :key="index">
            <div style="display:flex;align-items:center;border:1px solid #e0e0e0;padding:10px">
              <div style="width:33%;display: inline-block;">
                <span>{{$t('100089')}}：</span>
                <span>{{item.product_code }}</span>
                <br />
                <span>{{$t('100088')}}：</span>
                <span>{{item.product_name}}</span>
              </div>
              <div style="width:33%;display: inline-block;">
                <span>{{$t('900965')}}：</span>
                <span>{{item.count}}</span>
                <br />
                <span>{{$t('500100')}}：</span>
                <span>{{item.delivery_time}}</span>
                <br />
              </div>
              <div style="width:33%;display: flex;align-items:center">
                <svg :id="'barcode'+index" />
                <Icon type="ios-trash" :size="30" @click="printList.splice(index, 1)" />
              </div>
            </div>
            <!-- <div v-if="printList.routes"> -->
            <div>
              <Table :data="item.routes" :columns="printColumn" height="150"></Table>
            </div>
          </div>
        </div>
      </div>
      <div slot="footer">
        <Button type="text" size="large" @click="printmodal=false" v-text="$t('1028')">关闭</Button>
        <Button type="primary" size="large" v-print="printObj" v-text="$t('100077')">打印</Button>
      </div>
    </Modal>
    <Modal class="newModalClass" title="查看库存" v-model="lookModal" :styles="{width:'1100px'}">
      <div class="modal_body" style="padding-right:40px">
        <div>
          <div>产品库存量</div>
          <Table style="margin-top:10px" :data="prolist" :columns="column1" height="150"></Table>
        </div>
        <div style="margin-top:10px;">
          <div>物料齐套性</div>
          <Table
            style="margin-top:10px;overflow:visible"
            :data="matlist"
            :columns="column2"
            height="200"
          ></Table>
        </div>
      </div>
      <div slot="footer">
        <Button type="text" size="large" @click="lookModal=false" v-text="$t('1033')">取消</Button>
        <Button type="primary" size="large" @click="lookModal=false" v-text="$t('1011')">确定</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import axios from '@/libs/api.request';
import moment from 'moment';
import JsBarcode from 'jsbarcode';
export default {
    data () {
        return {
            printObj: {
                id: '#print',
                popTitle: '',
                extraCss: '',
                extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>'
            },
            addloading: false,
            dateTime: '',
            morecondition: false, // 条件
            productName: '',
            drawingNo: '',
            productionOrderNo: '', // 生产令号
            workOrderNo: '', // 工单号
            status: '', // 排期
            status1: '', // 是否逾期
            startTime: '', // 开始时间
            endTime: '', // 结束时间
            dataTime: '', // 时间范围
            total: 0, // 总信息数
            page: 1,
            pagesize: 10,
            tableData: [], // 表格数据
            tableColumns: [
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    },
                    width: '50px',
                    align: 'center'
                },
                {
                    type: 'selection',
                    width: 50,
                    align: 'center'
                },
                {
                    title: this.$t('100088'),
                    key: 'product_name',
                    width: '120px',
                    align: 'center'
                },
                {
                    title: this.$t('100089'),
                    key: 'product_code',
                    width: '120px'
                },
                {
                    title: this.$t('100090'),
                    key: 'draw_number',
                    width: '120px',
                    align: 'center'
                },
                {
                    title: this.$t('100091'),
                    key: 'product_remark',
                    width: '120px',
                    align: 'center'
                },
                {
                    title: this.$t('900337'),
                    key: 'code',
                    align: 'center'
                },
                {
                    title: this.$t('500099'),
                    key: 'quantity',
                    align: 'center'
                },
                {
                    title: this.$t('950190'),
                    key: 'has_schedule',
                    align: 'center'
                },
                {
                    title: this.$t('500100'),
                    key: 'delivery_time_str',
                    align: 'center'
                },
                {
                    title: this.$t('900925'),
                    key: 'leaveDays',
                    align: 'center'
                },
                {
                    title: this.$t('200002'),
                    key: 'status',
                    align: 'center',
                    render: (h, params) => {
                        var status = '',
                            bg = '';
                        if (params.row.status === 0) {
                            status = '未排产';
                            bg = '#f0ad4e';
                        } else if (params.row.status === 1) {
                            status = '已排产';
                            bg = '#f0ad4e';
                        } else if (params.row.status === 2) {
                            status = '生产中';
                            bg = 'green';
                        } else if (params.row.status === 3) {
                            status = '已完工';
                            bg = 'blue';
                        } else if (params.row.status === 4) {
                            status = '已终止';
                            bg = 'blue';
                        } else {
                            status = '逾期';
                            bg = 'blue';
                        }
                        return h(
                            'div',
                            {
                                class: '',
                                style: {
                                    backgroundColor: bg,
                                    color: '#fff',
                                    width: '100%',
                                    height: '36px',
                                    lineHeight: '36px',
                                    textAlign: 'center'
                                }
                            },
                            status
                        );
                    }
                },
                {
                    title: this.$t('900950'),
                    key: 'plan_start',
                    align: 'center',
                    width: 150,
                    render: (h, params) => {
                        let str = '';
                        str =
              params.row.plan_start == 0
                  ? ''
                  : moment(params.row.plan_start * 1000).format('YYYY-MM-DD');
                        return h('div', str);
                    }
                },

                {
                    title: this.$t('900951'),
                    key: 'plan_end',
                    align: 'center',
                    width: 150,
                    render: (h, params) => {
                        let str = '';
                        str =
              params.row.plan_end == 0
                  ? ''
                  : moment(params.row.plan_end * 1000).format('YYYY-MM-DD');
                        return h('div', str);
                    }
                },
                {
                    title: this.$t('900952'),
                    key: 'finish_time',
                    align: 'center',
                    width: 150,
                    render: (h, params) => {
                        let str = '';
                        str =
              params.row.finish_time == 0
                  ? ''
                  : moment(params.row.finish_time * 1000).format('YYYY-MM-DD');
                        return h('div', str);
                    }
                },
                {
                    title: 'OK产量',
                    key: 'ok_number',
                    align: 'center'
                },
                {
                    title: 'NOK产量',
                    key: 'nok_number',
                    align: 'center'
                },
                {
                    title: this.$t('1950514'),
                    align: 'center',
                    key: 'batch'
                },
                {
                    title: this.$t('500103'),
                    align: 'center',
                    key: 'number'
                }
            ],
            detailobj: {},
            currentNum: 0,
            batchNum: 0,
            rest: false,
            addmodal: false,
            addTable1: [],
            addTable2: [],
            addColumn1: [
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h('span', params.index + 1);
                    }
                },
                {
                    title: this.$t('900958'),
                    key: 'orderNumber',
                    align: 'center',
                    width: 200
                },

                {
                    title: this.$t('900959'),
                    key: 'count',
                    align: 'center',
                    width: 150,
                    render: (h, params) => {
                        return h('InputNumber', {
                            props: {
                                value: params.row.count,
                                size: 'small',
                                min: 0
                            },
                            on: {
                                input: (val) => {
                                    this.addTable1[params.index].count = val;
                                }
                            }
                        });
                    }
                },
                {
                    title: this.$t('900960'),
                    key: 'plan_start',
                    align: 'center'
                },
                {
                    title: this.$t('900961'),
                    key: 'plan_end',
                    align: 'center'
                }
            ],
            addColumn2: [
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    },
                    width: '60px'
                },

                {
                    title: this.$t('900967'),
                    key: 'route_name',
                    align: 'center'
                },
                {
                    title: '工序类型',
                    key: 'route_type',
                    render: (h, params) => {
                        let arr = [
                            { value: 1, label: '自制' },
                            { value: 2, label: '外协' }
                        ];
                        return h(
                            'Select',
                            {
                                props: {
                                    transfer: true,
                                    value: params.row.route_type
                                },
                                on: {
                                    'on-change': (event) => {
                                        this.addTable2[params.index].route_type = event;
                                    }
                                }
                            },
                            arr.map((item) => {
                                // console.log(item.key)
                                return h('Option', {
                                    props: {
                                        value: item.value,
                                        label: item.label
                                    }
                                });
                            })
                        );
                    }
                },
                {
                    title: this.$t('100308'),
                    key: 'machine_id',
                    align: 'center',
                    render: (h, params) => {
                        return h(
                            'Select',
                            {
                                props: {
                                    transfer: true,
                                    value: params.row.machine_id
                                    // labelInValue:true
                                },
                                on: {
                                    'on-change': (val) => {
                                        this.addTable2[params.index].machine_id = val;
                                    }
                                }
                            },
                            params.row.machines.map((item) => {
                                // console.log(item.key)
                                return h('Option', {
                                    props: {
                                        value: item.machine_id,
                                        label: item.machine_name
                                    }
                                });
                            })
                        );
                    }
                },
                {
                    title: this.$t('1030'),
                    key: 'remark',
                    align: 'center',
                    render: (h, params) => {
                        let str = '';
                        return h(
                            'Input',
                            {
                                props: {
                                    value: params.row.remark
                                },
                                on: {
                                    'on-change': (event) => {
                                        this.addTable2[params.index].remark = event.target.value;
                                        str = event.target.value;
                                    }
                                }
                            },
                            str
                        );
                    }
                },
                {
                    title: this.$t('500098'),
                    key: 'cycle_time',
                    align: 'center'
                    // render: (h, params) => {
                    //     return h("div", params.row.machines[0].cycle_time);
                    // },
                },
                {
                    title: this.$t('900962'),
                    key: 'sectionStartTime',
                    align: 'center',
                    width: 200
                },
                {
                    title: this.$t('900960'),
                    key: 'startTime',
                    align: 'center',
                    width: 200,
                    render: (h, params) => {
                        // let str = ''
                        return h('DatePicker', {
                            props: {
                                type: 'datetime',
                                value: params.row[params.column.key]
                                // format: "yyyy-MM-DD HH:mm:ss",
                                // size: "small",
                            },
                            on: {
                                'on-change': (event) => {
                                    this.addTable2[params.index].startTime = event;
                                    // str = event;
                                }
                            }
                        });
                    }
                },
                {
                    title: this.$t('900961'),
                    key: 'endTime',
                    align: 'center',
                    width: 200,
                    render: (h, params) => {
                        // let str = ''
                        return h('DatePicker', {
                            props: {
                                type: 'datetime',
                                value: params.row[params.column.key]
                                // format: "yyyy-MM-DD HH:mm:ss",
                            },
                            on: {
                                'on-change': (event) => {
                                    this.addTable2[params.index].endTime = event;
                                }
                            }
                        });
                    }
                }
            ],
            printmodal: false,
            selection: [],
            printList: [],
            printColumn: [
                {
                    title: this.$t('900967'),
                    key: 'route_name',
                    align: 'left'
                },

                {
                    title: this.$t('100308'),
                    key: 'sectionname',
                    align: 'left'
                },
                {
                    title: this.$t('500098'),
                    key: 'cycletime',
                    align: 'left'
                },
                {
                    title: this.$t('900951'),
                    key: 'plan_end',
                    align: 'left',
                    render: (h, params) => {
                        let str = '';
                        str = moment(params.row.plan_end * 1000).format('YYYY-MM-DD');
                        return h('div', str);
                    }
                },
                {
                    title: this.$t('1030'),
                    key: 'remark',
                    align: 'left'
                }
            ],
            lookModal: false,
            column1: [
                {
                    title: this.$t('900921'),
                    key: 'product_name',
                    align: 'center'
                },
                {
                    title: this.$t('600567'),
                    key: 'draw_number',
                    align: 'center'
                },
                {
                    title: this.$t('500099'),
                    key: 'quantity',
                    align: 'center'
                },
                {
                    title: this.$t('950190'),
                    key: 'has_schedule',
                    align: 'center'
                },
                {
                    title: this.$t('950201'),
                    key: 'ok_number',
                    align: 'center'
                },
                {
                    title: this.$t('950202'),
                    key: 'stock_quantity',
                    align: 'center',
                    width: 140
                },
                {
                    title: this.$t('901010'),
                    key: 'difference',
                    align: 'center',
                    width: 140
                }
            ],
            column2: [
                {
                    title: this.$t('950203'),
                    key: 'invCode',
                    align: 'center'
                },
                {
                    title: this.$t('950204'),
                    key: 'invName',
                    align: 'center'
                },
                {
                    title: this.$t('950205'),
                    key: 'invDefine4',
                    align: 'center'
                },
                {
                    title: this.$t('950206'),
                    key: 'baseQtyD',
                    align: 'center'
                },
                {
                    title: this.$t('950207'),
                    key: 'baseQtyN',
                    align: 'center'
                },
                {
                    title: this.$t('950208'),
                    key: 'quantity',
                    align: 'center'
                },
                {
                    title: this.$t('950209'),
                    key: 'fullQuantity',
                    align: 'center'
                }
            ],
            prolist: [],
            matlist: []
        };
    },
    created () {
        this.inittime();
        this.search(1, 10);
        // this.tableColumns = this.$time.initTableTitle(this.tableColumns);
        this.tableColumns = this.$time.initTableTitle(this.tableColumns);
        this.addColumn1 = this.$time.initTableTitle(this.addColumn1);
        this.addColumn2 = this.$time.initTableTitle(this.addColumn2);
    },
    methods: {
        timechange () {
            var time = this.$time.changetime(this.date1).split(' - ');
            this.dateTime = [time[0], time[1]];
        },
        inittime () {
            // 时间赋值
            var date = new Date();
            var start = this.$time.timestampToTime(
                date.getTime() - 3600 * 1000 * 24 * 30
            );
            var end = this.$time.timestampToTime(date.getTime() - 3600 * 1000 * 24);
            // this.dateTime = [start, end];
        },
        selectdate (e) {
            this.dateTime = [e[0], e[1]];
        },
        search () {
            var params = {
                page: this.page,
                pagesize: this.pagesize,
                product_name: this.productName,
                draw_number: this.drawingNo,
                code: this.productionOrderNo,
                ordernumber: this.workOrderNo,
                status: this.status,
                isdelay: this.status1,
                startTime: this.dateTime[0],
                endTime: this.dateTime[1]
            };
            axios
                .request({
                    url: '/cnc/Product/GetPlanListForSchedule',
                    params,
                    methods: 'get'
                })
                .then((res) => {
                    this.total = res.data.total;
                    let arr = [];
                    arr = res.data.data;

                    arr.forEach((item) => {
                        if (item) {
                            if (parseInt(item.quantity) <= 0) {
                                item.number = 0 + '%';
                            } else {
                                item.number =
                  Math.round((item.ok_number / item.quantity) * 100) + '%';
                            }
                        }
                    });
                    // console.log(arr);
                    this.tableData = arr;
                    // this.initTableData(res.data.data);
                });
        },
        add () {
            if (!this.detailobj.id) {
                this.$Message.error(this.$t(9000396));
                return false;
            }
            if (this.detailobj.status >= 3) {
                this.$Message.error('该计划已排产');
                return false;
            }
            if (this.detailobj.quantity <= this.detailobj.has_schedule) {
                this.$Message.error('该计划已排产');
                return false;
            }
            this.addTable1 = [];
            this.addTable2 = [];
            this.currentNum = this.detailobj.quantity - this.detailobj.has_schedule;
            this.batchNum = 0;
            this.addmodal = true;
        },
        addBatch () {
            if (
                this.currentNum == '' ||
        this.currentNum == '0' ||
        this.currentNum < 0
            ) {
                this.$Message.error(this.$t('950192'));
                return false;
            }
            if (this.batchNum == '' || this.batchNum == '0' || this.batchNum < 0) {
                this.$Message.error(this.$t('950193'));
                return false;
            }
            if (
                parseInt(this.currentNum) >
        parseInt(this.detailobj.quantity) -
          parseInt(this.detailobj.has_schedule)
            ) {
                this.$Message.error(this.$t('950194'));
                return false;
            }

            let data = {
                planId: this.detailobj.id, // 计划id
                num: this.batchNum,
                total: parseInt(this.currentNum)
            };
            this.request('/cnc/ProductSchedule/GetPlanDetails', data, 'get').then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.addTable1 = res.data.data;
                        this.addTable1.forEach((ele) => {
                            ele.plan_start = '';
                            ele.plan_end = '';
                        });
                    }
                }
            );
            this.request('/cnc/ProductSchedule/GetRoutes', data, 'get').then(
                (ret) => {
                    if (ret.data.ret == 1) {
                        this.addTable2 = ret.data.data;

                        this.addTable2.forEach((ele) => {
                            ele.route_type = 1;
                            ele.machine_id = ele.machines[0].machine_id;
                            ele.cycle_time = ele.machines[0].cycle_time;
                            ele.remark = '';
                        });
                    }
                }
            );
        },
        exclude () {
            if (this.addTable1.length > 0) {
                let totalnum = 0;
                this.addTable1.forEach((item, index) => {
                    totalnum += parseInt(item.count);
                });
                if (totalnum != this.currentNum) {
                    this.$Message.error('批量数之和与本次排产数量不相符');
                    return false;
                }
            } else {
                this.$Message.error('批量数之和与本次排产数量不相符');
                return false;
            }

            let arr = [];
            this.addTable2.forEach((ele) => {
                let i = ele.batch;
                ele.orderNumber = this.addTable1[i].orderNumber;
                ele.count = this.addTable1[i].count;
                ele.machines.forEach((item) => {
                    if (ele.machine_id == item.machine_id) {
                        ele.machine_name = item.machine_name;
                    }
                });
                let obj = {
                    batch: ele.batch,
                    orderNumber: ele.orderNumber,
                    count: ele.count,
                    route_type: ele.route_type,
                    route_number: ele.route_number,
                    route_name: ele.route_name,
                    machine_id: ele.machine_id,
                    machine_name: ele.machine_name,
                    cycleTime: ele.cycle_time,
                    remark: ele.remark
                };
                arr.push(obj);
            });
            // debugger
            var data = {
                planId: this.detailobj.id, // 计划id
                excludeRestTime: this.rest == true ? 1 : 0, // 是否排除节假日
                orders: arr // 工令号及产线层级的集合
            };

            this.request('/cnc/ProductSchedule/ExecuteSchedule', data, 'post').then(
                (res) => {
                    if (res.data.ret == 1) {
                        let list = res.data.data,
                            obj = {},
                            arr1 = [],
                            array = [];

                        for (let i = 0; i < list.length; i++) {
                            const ele = list[i];
                            ele.startTime = moment
                                .unix(ele.startTime)
                                .format('YYYY-MM-DD HH:mm:ss');
                            ele.endTime = moment
                                .unix(ele.endTime)
                                .format('YYYY-MM-DD HH:mm:ss');
                            ele.sectionStartTime = moment
                                .unix(ele.sectionStartTime)
                                .format('YYYY-MM-DD HH:mm:ss');
                            console.table({
                                startTime: ele.startTime,
                                endTime: ele.endTime,
                                sectionStartTime: ele.sectionStartTime
                            });

                            ele.cycle_time = ele.cycleTime;
                            let a = ele.batch;
                            obj = {
                                count: ele.count,
                                orderNumber: ele.orderNumber,
                                plan_start: ele.startTime,
                                plan_end: ele.endTime
                            };
                            this.addTable1.splice(a, 1, obj);
                        }

                        this.addTable2 = list;
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                }
            );
        },
        adddata () {
            let bool = false;
            this.addTable2.forEach((ele) => {
                if (!ele.startTime || !ele.endTime) {
                    bool = true;
                }
            });
            if (bool) {
                this.$Message.error(this.$t(920076));
                return false;
            }
            // console.log(this.addTable2);
            let arr = [];
            this.addTable2.forEach((ele) => {
                let i = ele.batch;
                ele.orderNumber = this.addTable1[i].orderNumber;
                ele.count = this.addTable1[i].count;
                ele.machines.forEach((item) => {
                    if (ele.machine_id == item.machine_id) {
                        ele.machine_name = item.machine_name;
                    }
                });
                let obj = {
                    batch: ele.batch,
                    orderNumber: ele.orderNumber,
                    count: ele.count,
                    route_type: ele.route_type,
                    route_number: ele.route_number,
                    route_name: ele.route_name,
                    machine_id: ele.machine_id,
                    machine_name: ele.machine_name,
                    startTime: ele.startTime,
                    endTime: ele.endTime,
                    cycleTime: ele.cycle_time,
                    remark: ele.remark
                };
                arr.push(obj);
            });
            let data = {
                planId: this.detailobj.id, // 计划id
                orders: arr // 工令号及产线层级的集合
            };
            this.addloading = true;
            this.request('/cnc/ProductSchedule/AddSchedule', data, 'post').then(
                (res) => {
                    // console.log(res.data);
                    if (res.data.ret == 1) {
                        this.$Message.success(this.$t(res.data.msg));
                        this.search(1, 10);
                        this.addmodal = false;
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                    this.addloading = false;
                }
            );
        },
        end () {
            if (!this.detailobj.id) {
                this.$Message.error(this.$t('9000396'));
                return false;
            }
            // if (this.detailobj.status != 1) {
            //     this.$Message.error(this.$t(920070));
            //     return false;
            // }
            this.$Modal.confirm({
                title: this.$t(920071),
                content: '',
                width: '290px',
                closable: true,
                okText: this.$t('1011'),
                onOk: () => {
                    this.request(
                        '/cnc/Scheduling/StopSchedule?planId=' + this.detailobj.id,
                        '',
                        'get'
                    ).then((res) => {
                        if (res.data.ret === 1) {
                            this.$Message.success(this.$t('1064'));
                            this.search(1, 10);
                        } else {
                            this.$Message.error(this.$t(res.data.msg));
                        }
                    });
                }
            });
        },
        print () {
            if (this.selection.length == 0) {
                this.$Message.error(this.$t(9000396));
                return false;
            }
            this.getPrintDetail();
            this.printList.forEach((ele, i) => {
                let name = '#barcode' + i;
                // setTimeout(() => {
                //     JsBarcode(name, ele.orderNumber, {
                //         // width: 1,
                //         fontSize: 12,
                //         height: 60,
                //         displayValue: true, //是否在条形码下方显示文字
                //         textAlign: "center", //设置文本的水平对齐方式
                //         textPosition: "bottom", //设置文本的垂直位置
                //     });
                // }, 400);
            });
            this.printmodal = true;
        },
        getPrintDetail () {
            let ids = [];
            this.selection.forEach((ele) => {
                ids.push(ele.id);
            });
            var data = {
                planId: ids
            };
            this.request(
                '/cnc/ProductSchedule/GetPlanDatailsForPrint',
                data,
                'post'
            ).then((res) => {
                // console.log(res.data.data);
                if (res.data.ret == 1) {
                    this.printList = res.data.data;
                    this.printList.forEach((ele, i) => {
                        ele.delivery_time = moment(ele.delivery_time * 1000).format(
                            'YYYY-MM-DD'
                        );
                        let name = '#barcode' + i;
                        setTimeout(() => {
                            JsBarcode(name, ele.orderNumber, {
                                // width: 1,
                                fontSize: 12,
                                height: 60,
                                displayValue: true, // 是否在条形码下方显示文字
                                textAlign: 'center', // 设置文本的水平对齐方式
                                textPosition: 'bottom' // 设置文本的垂直位置
                            });
                        }, 400);
                    });
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        lookStock () {
            if (!this.detailobj.id) {
                this.$Message.error(this.$t('920072'));
                return false;
            }
            this.getProductsHtml();
            this.getMateriels();
            this.lookModal = true;
        },
        getProductsHtml () {
            var data = {
                id: this.detailobj.id
            };
            this.request('/changling/Scheduling/GetStockData', data, 'get').then(
                (res) => {
                    this.prolist = res.data.data;
                }
            );
        },
        getMateriels () {
            var data = {
                id: this.detailobj.partnumber
            };
            this.request('/changling/Scheduling/GetStockDetails', data, 'get').then(
                (res) => {
                    this.matlist = res.data.data;
                }
            );
        },
        handdlesearch () {
            this.page = 1;
            this.search();
        },
        handleSelect (val) {
            this.selection = val;
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.detailobj = currentRow;

            // let data={
            //     id:currentRow.id
            // }
            // this.request('/cnc/Product/GetProducePlanForSchedule',data,'get').then(res=>{
            //     console.log(res.data)
            // })
            this.currentNum = this.detailobj.quantity - this.detailobj.has_schedule;
        },
        handlePageSize (value) {
            this.pagesize = value;

            this.tableData = this.search();
        },
        changePage (value) {
            this.page = value;
            this.tableData = this.search();
        },
        request (url, data, type) {
            if (type == 'get') {
                return this.axios
                    .request({
                        url: url,
                        params: data,
                        method: type
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            } else {
                return this.axios
                    .request({
                        url: url,
                        data,
                        method: type
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            }
        }
    }
};
</script>

<style lang="less" scoped>
.modaltop {
  display: flex;
  justify-content: space-between;
  height: 40px;
  line-height: 40px;
}
</style>
