<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin:0px;padding-left:10px;">
      <div class="searchBtn">
      <Button type="success" class="twoWord" icon="md-add-circle" @click="edit(1)">{{$t('1002')}}</Button>
      <Button type="warning" class="twoWord" icon="ios-create-outline" @click="edit(2)">{{$t('1003')}}</Button>
      <Button type="error" class="twoWord" icon="md-close-circle" @click="dele()">{{$t('1004')}}</Button>
      </div>
    </div>
    <div class="divborder newTableDiv">
      <Table
        class="tableClass"
        @on-current-change="handleRowChange"
        highlight-row
        :data="tableData1"
        :columns="tableColumns1"
      ></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>
      <Modal
        :title="$t('960445')"
        v-model="editmodal"
        :styles="{width:'400px'}"
        class="newModalClass"
      >
        <div class="modal_body">
          <div class="filter">
            <label>
              <i>*</i>{{$t(700049)}}：
            </label>
            <Select v-model="line" label-in-value clearable filterable class="searchSelect">
              <Option
                v-for="(item,key) in lineList"
                :value="item.lineid"
                :key="key"
              >{{ item.title }}</Option>
            </Select>
            <!-- <div class="filter" v-show="type===2">{{editobj.machine_name}}</div> -->
          </div>
          <div class="filter">
            <label>
             <i>*</i> {{$t('900866')}}：
            </label>
            <Input class="searchInput" id="input1" v-model="restType" />
          </div>
          <div class="filter">
            <label>
              <i>*</i>{{$t('1049')}}：
            </label>
            <TimePicker
              type="time"
              format="HH:mm:ss"
              :value="value1"
              class="searchInput"
              @on-change="getStartTime"
            ></TimePicker>
          </div>
          <div class="filter">
            <label>
              <i>*</i>{{$t('1050')}}：
            </label>
            <TimePicker
              type="time"
              :value="value2"
              format="HH:mm:ss"
              class="searchInput"
              @on-change="getEndTime"
            ></TimePicker>
          </div>
        </div>
        <div slot="footer">
          <Button type="text" size="large" @click="cancel" v-text="$t(1033)">取消</Button>
          <Button type="primary" v-if="type===1" size="large" @click="editdata()">{{$t('1002')}}</Button>
          <Button type="primary" v-if="type===2" size="large" @click="editdata()">{{$t('1003')}}</Button>
        </div>
      </Modal>
    </div>
  </div>
</template>
<script>
import moment from 'moment';
import axios from '@/libs/api.request';
export default {
    name: 'bottleneckmanagement', // 组件不刷新
    data () {
        return {
            keyword: '',
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            detailobj: {},
            restType: '', // 休息类型
            value1: '',
            value2: '',
            startTime: '',
            endTime: '',
            editmodal: false,
            editobj: {},
            defaulterrors: [],
            type: 0,
            today: '',
            lineList: [], // 产线列表
            line: '',
            datalevel: [],
            leveldata: [],
            tableColumns1: [
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    },
                    width: '90px'
                },
                {
                    title: this.$t(900879), // 产线
                    key: 'line_name'
                },
                {
                    title: this.$t('900866'), // 休息类型
                    key: 'content'
                },
                {
                    title: this.$t('1049'), // 开始时间
                    key: 'dtstart'
                },
                {
                    title: this.$t('1050'), // 结束时间
                    key: 'dtend'
                },
                {
                    title: this.$t('700186'), // 休息时长(分钟)
                    key: 'resttime'
                },
                {
                    title: this.$t('700187'), // 设置人员
                    key: 'creator_name'
                },
                {
                    title: this.$t('700188'), // 设置时间
                    key: 'createtime'
                }
            ]
        };
    },
    created () {
        this.search(1, 10);
        this.initlevel();
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        initlevel () {
            axios
                .request({
                    url: '/tpm/AndonBaseSetting/GetLineForAndonPlan',
                    method: 'get'
                })
                .then(res => {
                    if (res.data.ret === 1) {
                        const arr = [];
                        this.lineList = res.data.data;
                        arr.push(res.data.data[0].value);
                        this.leveldata = arr;
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                });
        },
        handlelevelchange (value, selectedData) {
            this.leveldata = value;
        },
        cancel () {
            this.editmodal = false;
        },
        search (page, pagesize) {
            var params = {
                page: page,
                pagesize: pagesize
            };
            axios
                .request({
                    url: '/tpm/AndonBaseSetting/GetAndonPlan',
                    params,
                    method: 'get'
                })
                .then(res => {
                    this.total = res.data.total;
                    this.mockTableData1(res.data.data);
                });
        },
        mockTableData1 (tablelist) {
            let data = [];
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        line: item.line,
                        line_name: item.line_name,
                        content: item.content,
                        dtstart: item.dtstart,
                        dtend: item.dtend,
                        resttime: item.resttime,
                        creator_name: item.creator_name,
                        createtime: moment(item.createtime * 1000).format(
                            'YYYY-MM-DD HH:mm:ss'
                        ),
                        change_cycle: item.change_cycle,
                        default_error_id: item.default_error_id,
                        default_error_name: item.default_error_name,
                        id: item.id,
                        station_type: item.station_type
                    });
                }
            });
            this.tableData1 = data;
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.detailobj = currentRow;
        },
        handlePageSize (value) {
            this.pagesize = value;
            this.tableData1 = this.search(this.page, this.pagesize);
        },
        changePage (value) {
            this.page = value;
            this.tableData1 = this.search(this.page, this.pagesize);
        },
        intdefaulterrors () {
            var params = { level: 2 };
            axios
                .request({
                    params,
                    url: '/tpm/Andon/GetErrorsByLevel',
                    method: 'get'
                })
                .then(res => {
                    this.defaulterrors = res.data.data;
                });
        },
        edit (type) {
            this.intdefaulterrors();
            this.getToday();
            this.editmodal = true;
            this.type = type;
            if (type === 2) {
                if (this.detailobj === undefined || this.detailobj.id === undefined) {
                    this.$Message.error(this.$t('1017'));
                    return false;
                }
                this.line = this.detailobj.line;
                this.restType = this.detailobj.content;
                this.value1 = this.detailobj.dtstart;
                this.value2 = this.detailobj.dtend;
                this.startTime = this.today + ' ' + this.value1;
                this.endTime = this.today + ' ' + this.value2;
            } else {
                this.line = 0;
                this.restType = '';
                this.value1 = '';
                this.value2 = '';
            }
        },
        getStartTime (value) {
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            if (month < 10) {
                month = '0' + month;
            }
            if (day < 10) {
                day = '0' + day;
            }
            this.value1 = value;
            this.today = year + '-' + month + '-' + day;
            this.startTime = this.today + ' ' + value;
            // console.log(this.startTime);
        },
        getEndTime (value) {
            this.value2 = value;

            this.endTime = this.today + ' ' + value;
            // console.log(this.endTime);
        },
        getToday () {
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            if (month < 10) {
                month = '0' + month;
            }
            if (day < 10) {
                day = '0' + day;
            }
            this.today = year + '-' + month + '-' + day;
        },
        editdata () {
            if (this.line == 0) {
                this.$Message.error(this.$t(700050));
                return false;
            }
            if (this.restType == '') {
                this.$Message.error(this.$t(700051));
                return false;
            }
            if (this.value1 == '') {
                this.$Message.error(this.$t(600165));
                return false;
            }
            if (this.value2 == '') {
                this.$Message.error(this.$t(600166));
                return false;
            }
            let start = Date.parse(this.startTime);
            let end = Date.parse(this.endTime);
            if (end < start) {
                this.$Message.error(this.$t(9000057));
                return false;
            }

            let data = {
                line: this.line,
                content: this.restType,
                dtstart: this.startTime,
                dtend: this.endTime,
                machine: 0,
                id: 0
            };
            for (let i = 0; i < this.lineList.length; i++) {
                if (this.line == this.lineList[i].lineid) {
                    data.machine = this.lineList[i].machineid;
                }
            }
            if (this.type == 2) {
                data.id = this.detailobj.id;
            }
            // console.log(data);
            // return false;
            axios
                .request({
                    url: '/tpm/AndonBaseSetting/SetAndonPlan',
                    data,
                    method: 'post'
                })
                .then(res => {
                    // console.log(res.data);
                    if (res.data.ret === 1) {
                        this.$Message.success(
                            this.type === 1 ? this.$t('1055') : this.$t('1060')
                        );
                        this.editmodal = false;
                        this.search(this.page, this.pagesize);
                    } else {
                        this.$Message.error(this.$t(res.data.msg));
                    }
                });
        },
        dele () {
            if (this.detailobj === undefined || this.detailobj.id === undefined) {
                this.$Message.error(this.$t('1017'));
                return false;
            } else {
                this.$Modal.confirm({
                    title: this.$t('1018'),
                    content: '',
                    width: '290px',
                    closable: true,
                    okText: this.$t('1004'),
                    onOk: () => {
                        var data = {
                            id: this.detailobj.id
                        };
                        axios
                            .request({
                                url:
                  '/tpm/AndonBaseSetting/DeleteAndonPlan?id=' +
                  this.detailobj.id,
                                method: 'get'
                            })
                            .then(res => {
                                // console.log(res.data);
                                if (res.data.ret === 1) {
                                    this.$Message.success(this.$t('1058'));
                                    this.page = 1;
                                    this.search(this.page, this.pagesize);
                                    this.detailobj = {};
                                } else {
                                    this.$Message.error(this.$t(res.data.msg));
                                }
                            });
                    }
                });
            }
        }
    }
};
</script>
<style lang="less" scoped>

</style>
