<template>
  <div>
    <div class="divborder">
      <div class="filter">
        <label>产线层级:</label>
        <Cascader :data="datalevel" :value="leveldata" trigger="hover" style="display:inline-block;width:160px;"></Cascader>
      </div>
      <div class="filter">
        <label>设备类型:</label>
        <Cascader :data="datamt" :value="mtdata" @on-change="machineTypeChange" trigger="hover" style="display:inline-block;width:160px;"></Cascader>
      </div>
      <!-- <div class="filter">
        <label>父级设备类型:</label>
        <Select v-model="filterObj.machineType" @on-change="machineTypeChange" label-in-value clearable style="width:160px;">
          <Option v-for="item in initPageData.machineTypeList" :value="item.id" :key="item.id">{{ item.title }}</Option>
        </Select>
      </div>
      <div class="filter">
        <label>子级设备类型:</label>
        <Select v-model="filterObj.machineTypeMixed" @on-change="machineTypeMixedChange" label-in-value clearable style="width:160px;">
          <Option v-for="item in initPageData.machineTypeMixedList" :value="item.id" :key="item.id">{{ item.title }}</Option>
        </Select>
      </div> -->
      <div class="filter">
        <label>故障代码:</label>
        <Select v-model="filterObj.faultType" @on-change="faultTypeChange" label-in-value clearable style="width:160px;">
          <Option v-for="item in initPageData.faultTypeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
      </div>
      <div class="filter">
        <label>工单状态:</label>
        <Select v-model="filterObj.status" label-in-value clearable style="width:182px;">
          <Option :value="1" :key="1">待接单</Option>
          <Option :value="2" :key="2">待签到</Option>
          <Option :value="3" :key="3">维修中</Option>
          <Option :value="4" :key="4">待复线</Option>
          <Option :value="5" :key="5">待反馈</Option>
          <Option :value="6" :key="6">已完成</Option>
        </Select>
      </div>

      <div v-show="!morecondition">
        <div>
          <!-- <div class="filter">
            <label>失效模式:</label>
            <Select v-model="filterObj.faultContent" label-in-value clearable style="width:160px;">
              <Option v-for="item in initPageData.faultContentList" :value="item.title" :key="item.id">{{ item.title }}</Option>
            </Select>
          </div> -->
          <div class="filter">
            <label>维修人员:</label>
            <input type="text" class="publicinput ivu-input" placeholder="请输入维修人员" style="width: 160px;" v-model="filterObj.repairname" />
          </div>
          <div class="filter">
            <label>工单号码:</label>
            <input type="text" class="publicinput ivu-input" style="width: 160px;" v-model="filterObj.repairid"
              placeholder="请输入数字" />
          </div>
          <div class="filter">
            <label>维修时长:</label>
            <Select v-model="filterObj.compare" label-in-value clearable style="width:75px;">
              <Option value="2" :key="2">大于</Option>
              <Option value="1" :key="1">小于</Option>
            </Select>
            <input type="text" class="publicinput ml5 ivu-input" v-model="filterObj.compare_time" placeholder="分钟"
              style="width:80px;" />
          </div>

          <div class="filter">
            <label>工单时间:</label>
            <DatePicker type="datetimerange" @on-change="timechange" :value="filterObj.startTime" format="yyyy-MM-dd"
              placement="bottom-end" split-panels placeholder="Select date" style="width: 182px"></DatePicker>
          </div>
        </div>
      </div>

      <div class="buttonall">
        <Button type="primary" icon="ios-search" @click="search(1,10)">{{$t('1001')}}</Button>
        <Button type="success" icon="md-add-circle" @click="add()">添加</Button>
        <Button type="warning" icon="ios-create-outline" @click="edit()">修改</Button>
        <Button type="error" icon="md-close-circle" @click="del()">删除</Button>
        <Button type="primary" icon="md-download" @click="toExcel()">{{$t(1007)}}</Button>
      </div>

      <div class="conditionblock">
        <div class="conditionborder" @click="changecondition">
          <div class="box">
            <div class="skew">
              <div class="skew-main">
                <div v-show="morecondition">
                  <span>{{this.$t('950400')}}
                    <Icon type="ios-arrow-down" /></span>
                </div>
                <div v-show="!morecondition">
                  <span>{{this.$t('950401')}}
                    <Icon type="ios-arrow-up" /></span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

    </div>

    <div class="divborder">
      <Table style="margin-top:10px" width='100%' @on-current-change="handleRowChange" highlight-row :data="tableData1"
        :columns="tableColumns1"></Table>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <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 :title="modalTitle" v-model="addModal" @on-ok="handleSubmit('addObj')" :loading="loading" :styles="{top: '40px',width:'900px'}">
      <div class="modal_body" style="width:100%!important;padding:0 20px;">
        <Form ref="addObj" :model="addObj" label-position="left" :label-width="80" :rules="addModalRule">
          <Row type="flex" justify="space-between" class="code-row-bg">
            <Col span="14">
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="12">
              <FormItem label="报修时间" v-show="optType==='add'" prop="createtime">
                <DatePicker type="datetime" v-model="addObj.createtime" @on-change="addObj.createtime=$event" format="yyyy-MM-dd hh:mm:ss"
                  placement="bottom-end" placeholder="Select date" style="width: 160px"></DatePicker>
              </FormItem>
              </Col>
              <Col span="12">
              <FormItem label="设备名称" v-show="optType==='add'" prop="machineid">
                <Select v-model="addObj.machineid" @on-change="machineChangeAdd($event)" label-in-value clearable style="width:160px;">
                  <Option v-for="item in initPageData.machineList" :tag="item.typeid" :value="item.id" :key="item.id">{{ item.machinename }}</Option>
                </Select>
              </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="12">
              <FormItem label="响应时间" prop="taketime">
                <InputNumber v-model="addObj.taketime" :disabled="optType==='edit'" placeholder="分钟"></InputNumber>
                <!-- :max="10" :min="1" -->
              </FormItem>
              </Col>
              <Col span="12">
              <FormItem label="报修人" prop="bxr">
                <Select v-model="addObj.bxr" label-in-value clearable style="width:160px;">
                  <Option v-for="item in initPageData.bxrList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
              </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="12">
              <FormItem label="维修用时" prop="repairtime">
                <InputNumber v-model="addObj.repairtime" placeholder="分钟" :disabled="optType==='edit'"></InputNumber>
                <!-- :max="10" :min="1" -->
              </FormItem>
              </Col>
              <Col span="12">
              <FormItem label="签到人" prop="qdr">
                <Select v-model="addObj.qdr" label-in-value clearable style="width:160px;">
                  <Option v-for="item in initPageData.qdrList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
              </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="12">
              <FormItem label="维修人" prop="wxr">
                <Select v-model="addObj.wxr" multiple style="width:158px">
                  <Option v-for="item in initPageData.wxrList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
              </FormItem>
              </Col>
              <Col span="12">
              <FormItem label="复线人" prop="fxr">
                <Select v-model="addObj.fxr" label-in-value clearable style="width:160px;">
                  <Option v-for="item in initPageData.fxrList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
              </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="12">
              <FormItem label="故障类型" prop="faultType">
                <Select v-model="addObj.faultType" @on-change="faultTypeChangeAdd" label-in-value clearable style="width:160px;">
                  <Option v-for="item in initPageData.faultTypeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
              </FormItem>
              </Col>
              <Col span="12">
              <FormItem label="失效模式" prop="ssType">
                <Select v-model="addObj.ssType" label-in-value clearable style="width:160px;">
                  <Option v-for="item in initPageData.ssTypeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
                </Select>
              </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="12">
              <FormItem label="停机" prop="offlinestatus">
                <Select v-model="addObj.offlinestatus" label-in-value clearable style="width:160px;">
                  <Option :value="0" :key="0">否</Option>
                  <Option :value="1" :key="1">是</Option>
                </Select>
              </FormItem>
              </Col>
              <Col span="12">
              <FormItem label="备注">
                <input class="publicinput" type="text" v-model="addObj.remark">
              </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="24">
              <FormItem label="报修时上传照片">
                <ImgsUpload :uList="addObj.bxImgList" :key="'bxImgs'" @activeName="outImgsUploadChild($event,'bx')"
                  v-model="addObj.bxImgList"></ImgsUpload>
              </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="24">
              <FormItem label="故障描述" prop="fault_description">
                <input class="publicinput" style="width: 96%;" type="text" v-model="addObj.fault_description">
              </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="24">
              <FormItem label="故障图片">
                <ImgsUpload :uList="addObj.gzImgList" :key="'gzImgs'" @activeName="outImgsUploadChild($event,'gz')"
                  v-model="addObj.gzImgList"></ImgsUpload>
              </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="24">
              <FormItem label="维修排查过程" prop="repair_process">
                <input class="publicinput" style="width: 96%;" type="text" v-model="addObj.repair_process">
              </FormItem>
              </Col>
            </Row>
            <Row type="flex" justify="space-between" class="code-row-bg">
              <Col span="24">
              <FormItem label="故障原因" prop="real_faultid_desc">
                <input class="publicinput" style="width: 96%;" type="text" v-model="addObj.real_faultid_desc">
              </FormItem>
              </Col>
            </Row>
            </Col>
            <Col span="2">
            </Col>
            <Col span="8">
            <SparePart :inputSpareParts="addObj.items" @activeName="outSparePartChild($event)"></SparePart>
            </Col>
          </Row>

        </Form>
      </div>
    </Modal>

    <Modal title="查看图片/视频" v-model="carousel" :loading="loading" :styles="{top: '40px',width:'900px',height:'400px'}">
      <div class="modal_body" style="width:100% !important;height: 400px !important; padding:0 10px;overflow: hidden;">
        <Carousel v-if="carousel" v-model="carouselValue">
          <CarouselItem v-for="(item,index) in carouselData" :key="index">
            <span style="font-size: 0;width: 848px;height: 400px;display: table-cell;text-align: center;vertical-align: middle;">
              <img alt="" :src="item" style="max-width: 100%;max-height: 100%;" />
            </span>
            <span v-show="false">{{index}}</span>
          </CarouselItem>
        </Carousel>
      </div>
    </Modal>

  </div>
</template>
<script>
import axios from '@/libs/api.request'
import ImgsUpload from '_c/image-upload/imgs-upload.vue'
import SparePart from '_c/spare-part/spare-part.vue'
import moment from 'moment'
export default {
    name: 'tpm-list',
    components: {
        ImgsUpload,
        SparePart
    },
    // data ************************************************************************************
    data () {
        const validateCreateTime = (rule, value, callback) => { // 正整数
            if (value != '') {
                callback()
            } else {
                return callback(new Error('请选择报修时间'))
            }
        }
        const validatePositiveInteger = (rule, value, callback) => { // 正整数
            if (/^[1-9]\d*$/.test(value)) {
                callback()
            } else {
                return callback(new Error('请填写正整数'))
            }
        }
        const validateMachine = (rule, value, callback) => {
            if (/^[1-9]\d*$/.test(value)) {
                callback()
            } else {
                return callback(new Error(this.$t(100150)))
            }
        }
        const validateBxr = (rule, value, callback) => {
            if (/^[1-9]\d*$/.test(value)) {
                callback()
            } else {
                return callback(new Error('请选择报修人'))
            }
        }
        const validateQdr = (rule, value, callback) => {
            if (/^[1-9]\d*$/.test(value)) {
                callback()
            } else {
                return callback(new Error('请选择签到人'))
            }
        }
        const validateWxr = (rule, value, callback) => {
            if (value.length > 0) {
                callback()
            } else {
                return callback(new Error('请选择维修人'))
            }
        }
        const validateFxr = (rule, value, callback) => {
            if (/^[1-9]\d*$/.test(value)) {
                callback()
            } else {
                return callback(new Error('请选择复线人'))
            }
        }
        const validateFaultType = (rule, value, callback) => {
            if (/^[1-9]\d*$/.test(value)) {
                callback()
            } else {
                return callback(new Error('请选择故障类型'))
            }
        }
        const validateSSType = (rule, value, callback) => {
            if (/^[1-9]\d*$/.test(value)) {
                callback()
            } else {
                return callback(new Error('请选择失效模式'))
            }
        }
        return {

            datalevel: [], // 产线层级插件所需数据
            leveldata: [], // 产线层级选中数据
            levelid: 0, // 产线层级选中的section_id

            datamt: [], // 插件所需数据
            mtdata: [], // 选中数据
            mtid: 0, //

            morecondition: false,
            filterObj: {
                machineType: '', /// config/MachineType/GetEditDcategory
                machineTypeMixed: '',
                faultType: '',
                faultContent: '',
                repairname: '',
                repairid: '',
                compare: 2,
                compare_time: '',
                status: '',
                startTime: ['2020-01-01', '2020-01-31'], // 2019-11-01 12:00:00 - 2020-02-19 12:00:00
                morecondition: false // 隐藏条件
            },

            initPageData: {
                machineTypeList: [], // filter设备类型  下拉菜单
                machineTypeMixedList: [], // filter子设备类型 下拉菜单
                faultTypeList: [], // filter故障类型  下拉菜单
                faultContentList: [], // filter失效模式 下拉菜单
                statusEnum: { // filter工单状态
                    '1': '待接单',
                    '2': this.$t(200033),
                    '3': '待维修',
                    '4': '待复线',
                    '5': '待提交维修反馈',
                    '6': '已完成'
                },
                allUser: [],
                machineList: [], // addModal 设备名称 下拉菜单
                bxrList: [], // addModal报修人下拉菜单
                wxrList: [], // addModal维修人下拉菜单
                fxrList: [],
                ssTypeList: [] // addModal失效模式 下拉菜单
            },

            addModal: false, // 添加Modal的开关
            loading: true,
            modalTitle: '',
            optType: 'add', // add,edit
            addObj: {
                id: 0,
                machineid: '', // 【设备id】!!!
                machineTypeId: 0, // 设备类型id
                createtime: '', // 报修时间
                bxr: '', // 报修人
                taketime: 0, // 响应用时
                repairtime: 0, // 维修用时
                qdr: 0, // 签到人
                wxr: [], // 维修人
                fxr: '', // 复线人
                faultType: '', // 故障类型，根据设备变动
                ssType: 0, // 失效模式，根据故障类型变动
                offlinestatus: 0, // 停机是，否
                remark: '', // 备注
                bxImgList: [], // 报修时上传照片
                images: '',
                fault_description: '', // 故障描述
                gzImgList: [], // 故障相关图片
                fault_desc_img: '',
                repair_process: '', // 维修排查过程
                real_faultid_desc: '', // 故障原因
                items: [],
                SparepartOut: ''
            },
            selectedObj: {}, // 选中对象容器

            total: 0, // 刀位列表total
            page: 1, // 刀位列表当前页数
            pagesize: 10, // 刀位列表pagesize

            carouselValue: 0, // 预览图片
            carousel: false,
            carouselData: [],

            tableData1: [], // 刀位列表数据容器
            tableColumns1: [{ // 列表字段属性集合
                type: 'index2',
                title: this.$t(1008),
                key: 'xuhao',
                render: (h, params) => {
                    return h('span', params.index + (this.page - 1) * this.pagesize + 1)
                },
                width: 60
            },
            {
                title: '工单号码',
                key: 'id',
                width: 90
            },
            {
                title: '工位名称',
                key: 'station',
                width: 120
            },
            {
                title: '所在位置',
                key: 'location',
                width: 160
            },
            {
                title: '停机',
                key: 'offlinestatus', // 1,0
                width: 60
            },
            {
                title: '故障类型',
                key: 'faulttype',
                width: 120
            },
            {
                title: '失效模式',
                key: 'faultcontent',
                width: 120
            },
                // {
                //   title: '图片/视频',
                //   key: 'imagesurl' //ele.images
                // },
            {
                title: '图片/视频',
                key: 'imagesurl',
                width: 100,
                align: 'center',
                render: (h, params) => {
                    if (params.row.imagesurls.length > 0) {
                        return h('div', [
                            h('A', {
                                props: {
                                    // type: 'primary',
                                    // size: 'small'
                                },
                                style: {
                                    // marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.showImages(params.index, params.row.imagesurls)
                                    }
                                }
                            }, params.row.imagesurl)

                        ])
                    } else {
                        return h('span', {

                        }, params.row.imagesurl)
                    }
                }
            },
            {
                title: '报修备注',
                key: 'remark',
                width: 160
            },
            {
                title: '工单状态',
                key: 'repairstatus', // status:1,2,3,4,5,6
                width: 100
            },
            {
                title: '故障描述',
                key: 'fault_description',
                width: 140
            },
            {
                title: '图片/视频',
                key: 'fault_desc_imgurl',
                width: 100,
                align: 'center',
                render: (h, params) => {
                    if (params.row.fault_desc_imgurls.length > 0) {
                        return h('div', [
                            h('A', {
                                props: {
                                    // type: 'primary',
                                    // size: 'small'
                                },
                                style: {
                                    // marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.showImages(params.index, params.row.fault_desc_imgurls)
                                    }
                                }
                            }, params.row.fault_desc_imgurl)

                        ])
                    } else {
                        return h('span', {

                        }, params.row.fault_desc_imgurl)
                    }
                }
            },
            {
                title: '维修排查过程',
                key: 'repair_process',
                width: 120
            },
            {
                title: '故障原因',
                key: 'real_faultid_desc',
                width: 120
            },
            {
                title: '所用备件',
                key: 'sparepart',
                width: 120
            },
            {
                title: '维修备注',
                key: 'repair_remark',
                width: 120
            },
            {
                title: '报修时间',
                key: 'createtimeformat',
                width: 140
            },
            {
                title: '维修用时',
                key: 'repairtime',
                width: 120
            },
            {
                title: '总用时',
                key: 'totaltime', // ele.repairtime+分钟
                width: 120
            },
            {
                title: '维修人',
                key: 'add_repairmid_name', // ele.totaltime+分钟
                width: 100
            }
            ],
            addModalRule: {
                createtime: [{
                    required: true,
                    validator: validateCreateTime,
                    trigger: 'blur,change'
                }],
                machineid: [{
                    required: true,
                    message: '请选择设备类型',
                    validator: validateMachine,
                    trigger: 'blur,change'
                }],
                taketime: [{
                    required: true,
                    validator: validatePositiveInteger,
                    trigger: 'blur,change'
                }],
                bxr: [{
                    required: true,
                    validator: validateBxr,
                    trigger: 'blur,change'
                }],
                repairtime: [{
                    required: true,
                    validator: validatePositiveInteger,
                    trigger: 'blur,change'
                }],
                qdr: [{
                    required: true,
                    validator: validateQdr,
                    trigger: 'blur,change'
                }],
                wxr: [{
                    required: true,
                    validator: validateWxr,
                    trigger: 'blur,change'
                }],
                fxr: [{
                    required: true,
                    validator: validateFxr,
                    trigger: 'blur,change'
                }],
                faultType: [{
                    required: true,
                    validator: validateFaultType,
                    trigger: 'blur,change'
                }],
                ssType: [{
                    required: true,
                    validator: validateSSType,
                    trigger: 'blur,change'
                }],
                fault_description: [{
                    required: true,
                    message: '请输入故障描述',
                    trigger: 'blur,change'
                }],
                repair_process: [{
                    required: true,
                    message: '请输入维修排查过程',
                    trigger: 'blur,change'
                }],
                real_faultid_desc: [{
                    required: true,
                    message: '请输入故障原因',
                    trigger: 'blur,change'
                }]
            }

        }
    },
    // created ************************************************************************************
    created () {
        this.search(1, 10) // 初始化列表
        this.initlevel() // 初始化产线层级
        // this.initMachineType() //初始化设备类型下拉菜单
        this.initMT() // 初始化设备类型下拉菜单
        this.initAddModal()
    },
    // methods ************************************************************************************
    methods: {
        initMT () {
            axios.request({
                url: '/config/MachineType/GetMachineTypeTree',
                method: 'get'
            }).then(res => {
                this.datamt = res.data.data
                this.mtdata = []
            })
        },
        initMachineType () { // 初始化产线层级
            axios.request({
                url: '/config/MachineType/GetEditDcategory',
                method: 'get'
            }).then(res => {
                if (res.data.data != undefined && res.data.data.length > 0) {
                    this.initPageData.machineTypeList = res.data.data
                } else {
                    this.initPageData.machineTypeList = []
                }
            })
        },
        machineTypeChange (data) {
            this.mtdata = data
            let params = {
                machinetypeid: data.length > 0 ? data[1] : 0
            }
            axios.request({
                url: '/tpm/FaultType/GetFaultTypeList',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.data != undefined && res.data.data.length > 0) {
                    this.initPageData.faultTypeList = [] // res.data.data
                    res.data.data.forEach(item => {
                        this.initPageData.faultTypeList.push({
                            label: item.faulttype,
                            value: item.faulttype
                        })
                    })
                } else {
                    this.initPageData.faultTypeList = []
                }
                // this.filterObj.faultType = 0
                // this.filterObj.faultContent = 0
                // this.initPageData.faultContentList = []
            })
        },
        faultTypeChange (data) {
            // 请求数据
            let params = {
                faultTypeId: data === undefined ? 0 : data.value
            }
            axios.request({
                url: '/tpm/FaultType/GetInvalidModesByTypeId',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.data != undefined && res.data.data.length > 0) {
                    this.initPageData.faultContentList = res.data.data
                } else {
                    this.initPageData.faultContentList = []
                }
                this.filterObj.faultContent = 0
            })
        },
        machineChangeAdd (event) { // obj=>{label:'',value:''}
            this.addObj.machineTypeId = event != undefined ? event.tag : this.addObj.machineTypeId
            let params = {
                machinetypeid: this.addObj.machineTypeId
            }
            let temp = []
            axios.request({
                url: '/tpm/FaultType/GetFaultTypeList',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1 && res.data.data != undefined) {
                    res.data.data.forEach(item => {
                        temp.push({
                            label: item.faulttype,
                            value: item.listid
                        })
                    })
                }
            })
            this.initPageData.faultTypeList = temp
            // console.log('change createtime:' + this.changeDate(this.addObj.createtime))
            // console.log('change machine name:' + this.addObj.machinename)
            // console.log('change taketime:' + this.addObj.taketime)
            // console.log('change bxr name:' + this.addObj.bxr)
        },
        faultTypeChangeAdd () { // obj=>{label:'',value:''}
            let params = {
                faultTypeId: this.addObj.faultType
            }
            let temp = []
            axios.request({
                url: '/tpm/FaultType/GetInvalidModesByTypeId',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1 && res.data.data != undefined) {
                    res.data.data.forEach(item => {
                        temp.push({
                            label: item.title,
                            value: item.id
                        })
                    })
                }
            })
            this.initPageData.ssTypeList = temp
        },
        showImages (index, imageurls) {
            this.carouselData = imageurls
            this.carousel = true
        },
        timechange (value) {
            this.filterObj.startTime = value
        },
        changeDate (dateA) {
            var dateee = new Date(dateA).toJSON()
            var date = new Date(+new Date(dateee) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/,
                '')
            return date
        },

        initlevel () { // 初始化产线层级
            axios.request({
                url: 'config/ProjectLevelSection/GetSectionTree',
                method: 'get'
            }).then(res => {
                const arr = []
                this.datalevel = res.data.data
                arr.push(res.data.data[0].value)
                this.leveldata = arr
            })
        },
        changelevel (data) { // 切换产线层级，触发事件
            // 请求数据
            let params = {
                sectionid: this.levelid,
                type: 1
            }
        },
        changePage (value) { // 换页事件
            this.page = value
            this.tableData1 = this.search(this.page, this.pagesize)
        },
        handleRowChange (currentRow, oldCurrentRow) { // 选中一条数据，触发事件
            this.selectedObj = currentRow
        },
        handlePageSize (value) { // 切换分页页数，触发事件
            this.pagesize = value
            this.tableData1 = this.search(this.page, this.pagesize)
        },
        search (page, pagesize) { // 点击查询，触发事件
            var data = {
                compare: this.filterObj.compare === undefined ? '' : this.filterObj.compare,
                compare_time: this.filterObj.compare_time,

                endtime: this.filterObj.startTime[1],
                fault_id: this.filterObj.faultContent,
                faulttype_id: this.filterObj.faultType,
                page: page,
                pagesize: pagesize,
                repairid: this.filterObj.repairid,
                repairname: this.filterObj.repairname,
                section_id: this.levelid,
                startTime: this.filterObj.startTime[0],
                status: this.filterObj.status,
                type_id: this.mtdata.length > 0 ? this.mtdata[0] : 0,
                concrete_id: this.mtdata.length > 0 ? this.mtdata[1] : 0
            }
            // console.log(this.mtdata)
            // console.log(data)
            axios.request({
                url: '/tpm/Repair/GetRepairListNew',
                data,
                method: 'post'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.total = res.data.total
                    this.mockTableData1(res.data.data)
                }
            })
        },
        mockTableData1 (tablelist) { // 转换数据，并赋值给列表数据容器tableData1
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    let imagesurls = item.images != '' ? item.images.split(',') : []
                    let imagesurl = imagesurls.length + '个'
                    let fault_desc_imgurls = item.fault_desc_img != '' ? item.fault_desc_img.split(',') : []
                    let fault_desc_imgurl = fault_desc_imgurls.length + '个'
                    data.push({
                        id: item.id,
                        station: item.station,
                        location: item.location,
                        offlinestatus: item.offlinestatus == 1 ? '是' : '否', // 1，0
                        faulttype: item.faulttype,
                        faultcontent: item.faultcontent,
                        imagesurl: imagesurl, // item.images.split(",")
                        imagesurls: imagesurls,
                        remark: item.remark,
                        repairstatus: this.initPageData.statusEnum[item.status], // 1，2，3，4，5，6
                        fault_description: item.fault_description,
                        fault_desc_imgurl: fault_desc_imgurl, /// /item.fault_desc_img.split(",")
                        fault_desc_imgurls: fault_desc_imgurls, /// /item.fault_desc_img.split(",")
                        repair_process: item.repair_process,
                        real_faultid_desc: item.real_faultid_desc,
                        sparepart: item.sparepart,
                        repair_remark: item.repair_remark,
                        createtimeformat: item.createtimeformat,
                        repairtime: item.repairtime + '分钟',
                        totaltime: item.totaltime + '分钟',
                        add_repairmid_name: item.add_repairmid_name
                    })
                }
            })
            this.tableData1 = data
        },

        initAddModal () {
            this.initAllMachines() // 初始化添加维修记录弹层中的全部设备下拉菜单
            this.initbxrList() // 初始化  报修人下拉列表
            this.initqdrList(2) // 初始化 type=2签到人，维修人下拉列表
            this.initqdrList(1) // 初始化 type=1复线人下拉列表
            this.addObj = {
                id: 0,
                machineid: '', // 【设备id】!!!
                machineTypeId: 0, // 设备类型id
                createtime: '', // 报修时间
                bxr: '', // 报修人
                taketime: 0, // 响应用时
                repairtime: 0, // 维修用时
                qdr: 0, // 签到人
                wxr: [], // 维修人
                fxr: '', // 复线人
                faultType: '', // 故障类型，根据设备变动
                ssType: 0, // 失效模式，根据故障类型变动
                offlinestatus: 0, // 停机是，否
                remark: '', // 备注
                bxImgList: [], // 报修时上传照片
                images: '',
                fault_description: '', // 故障描述
                gzImgList: [], // 故障相关图片
                fault_desc_img: '',
                repair_process: '', // 维修排查过程
                real_faultid_desc: '', // 故障原因
                items: [],
                SparepartOut: ''
            }
        },
        initAllMachines () {
            var data = {
                sectionId: 0
            }
            axios.request({
                url: '/config/Machine/GetAllMachines',
                data,
                method: 'get'
            }).then(res => {
                let temp = []
                if (res.data.ret === 1 && res.data.data != undefined && res.data.data.length > 0) {
                    res.data.data.forEach(item => {
                        temp.push({
                            machinename: item.title, // 设备名称
                            typeid: item.typeid, // 设备类型id
                            id: item.id // 设备id
                        })
                    })
                }
                this.initPageData.machineList = temp
            })
        },
        initbxrList () {
            axios.request({
                url: '/config/ProjectUser/GetAllUserByCurrentProjectId',
                method: 'get'
            }).then(res => {
                let temp = []
                if (res.data.ret === 1 && res.data.data != undefined && res.data.data.length > 0) {
                    res.data.data.forEach(item => {
                        temp.push({
                            label: item.name,
                            value: item.mid,
                            id: item.id
                        })
                    })
                }
                this.initPageData.bxrList = temp
            })
        },
        initqdrList (type) {
            axios.request({
                url: '/config/ProjectUser/GetProductionUsers?typeId=' + type,
                method: 'get'
            }).then(res => {
                let temp = []
                if (res.data.ret === 1 && res.data.data != undefined && res.data.data.length > 0) {
                    res.data.data.forEach(item => {
                        temp.push({
                            label: item.name,
                            value: item.mid
                        })
                    })
                }
                if (type === 2) { // 维修人，签到人列表
                    this.initPageData.qdrList = temp
                    this.initPageData.wxrList = temp
                } else {
                    this.initPageData.fxrList = temp
                }
            })
        },

        add () { // 添加
            this.addModal = true
            this.modalTitle = '添加'
            this.optType = 'add'
            this.initAddModal() // 初始化
            this.$refs.addObj.resetFields()
        },
        edit () {
            if (this.selectedObj.id === '' || this.selectedObj.id === undefined) {
                alert(this.$t(1043))
                return false
            }
            this.addModal = true
            this.modalTitle = '修改'
            this.optType = 'edit'
            this.initEditModal()
            this.$refs.addObj.resetFields()
        },
        initEditModal () {
            let params = {
                id: this.selectedObj.id
            }
            axios.request({
                url: '/tpm/Repair/getRepairInfoDetail',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.addObj.bxImgList = []
                    let item = res.data.data
                    this.addObj.id = item.id
                    // 根据machinetype_id找到machineid,因为接口给的machineId=0!!!
                    if (this.initPageData.machineList.length > 0) {
                        this.initPageData.machineList.forEach(dropdown_item => {
                            if (dropdown_item.typeid === item.machinetype_id) {
                                this.addObj.machineid = dropdown_item.id
                            }
                        })
                    }
                    this.addObj.machineTypeId = item.machinetype_id
                    this.addObj.createtime = moment(item.createtime * 1000).format('YYYY-MM-DD HH:mm:ss')
                    this.addObj.bxr = item.mid
                    this.addObj.taketime = item.taketime
                    this.addObj.repairtime = item.repairtime
                    this.addObj.qdr = item.signmid
                    this.addObj.wxr = item.add_repairmid.split(',').map((value) => {
                        return parseInt(value)
                    })
                    this.addObj.fxr = item.checkmid
                    this.addObj.faultType = item.faultlist_id
                    this.addObj.ssType = item.faultcontent_id
                    this.addObj.offlinestatus = item.offlinestatus
                    this.addObj.remark = item.remark
                    let tempBximgs = item.images.split(',')
                    let bximgs = []
                    if (tempBximgs.length > 0) {
                        tempBximgs.forEach(item => {
                            if (item != '') {
                                bximgs.push({
                                    name: 'abc.png',
                                    url: item,
                                    status: 'finished'
                                })
                            }
                        })
                    }
                    this.addObj.bxImgList = bximgs

                    this.addObj.images = item.images
                    this.addObj.fault_description = item.fault_description
                    let tempGzimgs = item.fault_desc_img.split(',')
                    let gzimgs = []
                    if (tempGzimgs.length > 0) {
                        tempGzimgs.forEach(item => {
                            if (item != '') {
                                gzimgs.push({
                                    name: 'abc.png',
                                    url: item,
                                    status: 'finished'
                                })
                            }
                        })
                    }
                    this.addObj.gzImgList = gzimgs

                    this.addObj.repair_process = item.repair_process
                    this.addObj.real_faultid_desc = item.real_faultid_desc
                    this.addObj.items = []
                    let spareList = item.sparepartid.split(',')
                    let tempSpareList = []
                    if (spareList.length > 0) {
                        let spareIndex = 1
                        spareList.forEach(function (spareid_num, index) {
                            let tempArr = spareid_num.split('*')
                            if (spareid_num.length > 0) {
                                tempSpareList.push({
                                    spareid: tempArr[0],
                                    spare: 'spare name',
                                    num: parseInt(tempArr[1]),
                                    index: spareIndex
                                })
                                spareIndex++
                            }
                        })
                        this.addObj.items = tempSpareList
                    }
                    this.addObj.SparepartOut = item.sparepartid
                    this.faultTypeChangeAdd()
                }
            })
        },
        del () { // 删除
            if (this.selectedObj.id === '' || this.selectedObj.id === undefined) {
                alert(this.$t(1043))
                return false
            } else {
                if (confirm('确认删除该条数据吗')) {
                    axios.request({
                        url: '/tpm/Repair/DeleteRepairOne?id=' + this.selectedObj.id,
                        method: 'post'
                    }).then(res => {
                        if (res.data.ret === 1) {
                            this.$Message.success(this.$t(1058))
                            this.search(1, 10)
                        } else {
                            this.$Message.error(this.$t(res.data.msg))
                        }
                    })
                }
            }
        },
        toExcel () { // 导出excel
            let data = {
                compare: this.filterObj.compare,
                compare_time: this.filterObj.compare_time,
                concrete_id: this.filterObj.machineTypeMixed,
                endtime: this.filterObj.startTime[1],
                fault_id: this.filterObj.faultContent,
                faulttype_id: this.filterObj.faultType,
                repairid: this.filterObj.repairid,
                repairname: this.filterObj.repairname,
                section_id: this.levelid,
                startTime: this.filterObj.startTime[0],
                status: this.filterObj.status,
                type_id: this.filterObj.machineType
            }
            axios.request({
                url: '/tpm/Repair/GetRepairListExcel',
                data,
                method: 'post'
            }).then(res => {
                if (res.data.ret === 1) {
                    window.location.href = axios.publicPath + '' + res.data.data
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        },
        handleSubmit (name) { // 添加
            this.$refs[name].validate((valid) => {
                if (valid) { // valid
                    let tempBxList = []
                    this.addObj.bxImgList.forEach(item => { // 报修相关
                        tempBxList.push(item.url)
                    })
                    this.addObj.images = tempBxList.join(',')
                    let tempGzList = []
                    this.addObj.gzImgList.forEach(item => { // 故障相关
                        tempGzList.push(item.url)
                    })
                    this.addObj.fault_desc_img = tempGzList.join(',')
                    let data = {}
                    if (this.optType === 'add') { // 添加操作
                        data = {
                            SparepartOut: this.addObj.SparepartOut,
                            checkmid: this.addObj.fxr,
                            createtime: this.changeDate(this.addObj.createtime), // "2020-01-11 00:00:00"
                            fault_desc_img: this.addObj.fault_desc_img,
                            fault_description: this.addObj.fault_description,
                            faultcontent_id: this.addObj.ssType,
                            faultid: this.addObj.faultType,
                            images: this.addObj.images,
                            machineid: this.addObj.machineid,
                            mid: this.addObj.bxr,
                            offlinestatus: this.addObj.offlinestatus,
                            real_faultid_desc: this.addObj.real_faultid_desc,
                            remark: this.addObj.remark,
                            repair_process: this.addObj.repair_process,
                            repairmid: this.addObj.wxr.join(','),
                            repairtime: this.addObj.repairtime,
                            signmid: this.addObj.qdr,
                            taketime: this.addObj.taketime
                        }
                        axios.request({
                            url: '/tpm/Repair/AddRepairRecord',
                            data,
                            method: 'post'
                        }).then(res => {
                            if (res.data.ret === 1) { // res.data.ret === 1
                                this.$Message.success(this.$t(1055))
                                this.addModal = false
                                this.search(1, 10)
                            } else {
                                this.$Message.error(this.$t(1056))
                                // 解决ok按钮loading的问题
                                this.loading = false
                                this.$nextTick(() => {
                                    this.loading = true
                                })
                            }
                        })
                    } else {
                        data = {
                            SparepartOut: this.addObj.SparepartOut,
                            checkmid: this.addObj.fxr,
                            fault_desc_img: this.addObj.fault_desc_img,
                            fault_description: this.addObj.fault_description,
                            faultcontent_id: this.addObj.ssType,
                            faultid: this.addObj.faultType,
                            id: this.addObj.id,
                            images: this.addObj.images,
                            machineid: this.addObj.machineid,
                            mid: this.addObj.bxr,
                            offlinestatus: this.addObj.offlinestatus,
                            real_faultid_desc: this.addObj.real_faultid_desc,
                            remark: this.addObj.remark,
                            repair_process: this.addObj.repair_process,
                            repairmid: this.addObj.wxr.join(','),
                            repairtime: this.addObj.repairtime,
                            signmid: this.addObj.qdr,
                            taketime: this.addObj.taketime
                        }
                        axios.request({
                            url: '/tpm/Repair/EditRepairInfo',
                            data,
                            method: 'post'
                        }).then(res => {
                            if (res.data.ret === 1) { // res.data.ret === 1
                                this.$Message.success(this.$t(1060))
                                this.addModal = false
                                this.search(1, 10)
                            } else {
                                this.$Message.error(this.$t(1061))
                                // 解决ok按钮loading的问题
                                this.loading = false
                                this.$nextTick(() => {
                                    this.loading = true
                                })
                            }
                        })
                    }
                } else {
                    // 解决ok按钮loading的问题
                    this.loading = false
                    this.$nextTick(() => {
                        this.loading = true
                    })
                }
            })
        },
        outImgsUploadChild (uploadList, type) {
            // type: gz=>故障 bx=>报修
            if (type === 'gz') {
                this.addObj.gzImgList = uploadList
            } else {
                this.addObj.bxImgList = uploadList
            }
        },
        outSparePartChild (items) {
            this.addObj.items = items
        },
        changecondition () {
            this.morecondition = !this.morecondition
        }

    },
    watch: {
        'addObj.items' (val) {
            let temp = []
            val.forEach(item => { // 备件相关
                temp.push(item.spareid + '*' + item.num)
            })
            this.addObj.SparepartOut = temp.join(',')
        }
    }
}
</script>
<style lang="less" scoped>

</style>
