<template>
  <!-- 采集参数项目维护 -->
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin:0px;">
        <div class="filter">
          <label >{{$t('1950573')}}：</label>
          <Input class="searchInput" type="text" v-model="key" />
        </div>
        <div class="filter">
          <label >{{$t('1950574')}}：</label>
          <Cascader :data="datalevel" :value="leveldata" v-model="sectionid" trigger="click" change-on-select class="searchSelect"></Cascader>
        </div>
        <div class="filter">
          <label >{{$t('950083')}}：</label>
          <Select v-model="machineid" label-in-value clearable class="searchSelect" >
              <Option v-for="item in machines" :value="item.id" :key="item.id">{{ item.title }}</Option>
          </Select>
        </div>
        <div class="filter">
              <label >{{$t('100307')}}：</label>
              <Select v-model="productid" label-in-value clearable class="searchSelect" >
                  <Option v-for="item in products" :value="item.id" :key="item.id">{{ item.name + "【" + item.code + "】" }}</Option>
              </Select>
            </div>
        <!-- 次级搜索条件--隐藏 -->
      <div v-show="!morecondition">
        <div>

            <div class="filter">
              <label >{{$t('200042')}}：</label>
              <Select v-model="status" label-in-value clearable class="searchSelect" >
                  <Option :value="0" :key="0">{{$t("400014")}}</Option>
                  <Option :value="1" :key="1">{{$t("800212")}}</Option>
              </Select>
            </div>
            <div class="filter">
            <label>{{$t('1046')}}：</label>
            <DatePicker
              id="日期"
              type="datetimerange"
              v-model="daterange"
              confirm
              placeholder=""
              class="searchInput MaxWidth"
              format="yyyy-MM-dd HH:mm:ss"
              @on-change="selectdate"
            ></DatePicker>
        </div>
      </div>
      </div>
      <div class="searchBtn">
          <Button type="primary" class="twoWord" icon="ios-search" @click="search(1,pagesize)">{{$t('1001')}}</Button>
          <Button type="success" class="twoWord" icon="ios-create-outline"  @click="add()">{{$t('1002')}}</Button>
          <Button type="warning" class="twoWord" icon="ios-create-outline"  @click="edit()">{{$t('1003')}}</Button>
          <Button type="primary" class="twoWord" icon="md-bulb"  @click="changeenable(1)">{{$t('800212')}}</Button>
          <Button type="error" class="twoWord" icon="ios-bulb-outline" @click="changeenable(0)">{{$t('400014')}}</Button>
      </div>

     <!-- 更多条件 按钮 -->
      <MoreConbtn @getCon="morecondition = !morecondition" :morecondition="morecondition"></MoreConbtn>

    </div>
    <div class="divborder newTableDiv">
      <Table
        class="tableClass"
        width='100%'
        @on-current-change="handleRowChange"
        @on-selection-change="handleSelectRow"
        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>
    </div>

    <Modal class="newModalClass" :title='title1' v-model="editmodal" :styles="{width:'400px'}">
      <div class="modal_body">
            <div class="filter">
              <label ><i>*</i>{{$t('1950574')}}：</label>
              <Cascader :data="datalevel1" :value="leveldata1" v-show="showlevel" v-model="editobj.editsectionid" trigger="click" change-on-select class="searchSelect"></Cascader>
            </div>
            <div class="filter">
                <label ><i>*</i>{{$t('100307')}}：</label>
                <Select v-model="editobj.productID" label-in-value clearable @on-change="changeitem" class="searchSelect" >
                    <Option v-for="item in editproducts" :value="item.id" :key="item.id">{{ item.name + "【" + item.code + "】" }}</Option>
                </Select>
            </div>
            <div class="filter">
              <label ><i>*</i>{{$t('901048')}}：</label>
              <Select v-model="editobj.itemID" label-in-value clearable class="searchSelect" >
                  <Option v-for="item in edititems" :value="item.id" :key="item.id">{{ item.item }}</Option>
              </Select>
            </div>
            <div class="filter">
              <label ><i>*</i>Value{{$t('920140')}}：</label>
              <Select v-model="editobj.value" label-in-value clearable filterable class="searchSelect" >
                  <Option v-for="item in parameters" :value="item.key" :key="item.key">{{ item.value }}</Option>
              </Select>
            </div>
            <div class="filter">
              <label ><i >*</i>Result{{$t('920140')}}：</label>
              <Select v-model="editobj.result" label-in-value clearable filterable class="searchSelect" >
                  <Option v-for="item in parameters" :value="item.key" :key="item.key">{{ item.value }}</Option>
              </Select>
            </div>
      </div>
      <div slot="footer">
        <Button type="text" size="large" @click="cancle">{{$t('1033')}}</Button>
        <Button type="primary" v-if="type === 1" size="large" @click="adddata">{{$t('1002')}}</Button>
        <Button type="primary" v-if="type === 2" size="large" @click="editdata">{{$t('1003')}}</Button>
      </div>
    </Modal>
  </div>
</template>

<script>
import axios from '@/libs/api.request'
import moment from 'moment'
export default{
    data () {
        return {
            headers: {
                token: localStorage.getItem('token'),
                timestamp: localStorage.getItem('timestamp'),
                nonce: localStorage.getItem('nonce'),
                sign: localStorage.getItem('sign')
            },
            morecondition: false,
            dtStart: '',
            dtEnd: '',
            title1: this.$t('1002'),
            tableData1: [],
            editmodal: false,
            page: 1,
            pagesize: 10,
            total: 0,
            tableColumns1: [
                {
                    type: 'selection',
                    width: 50,
                    align: 'center'
                },
                {
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        )
                    },
                    width: 70
                },
                {
                    title: this.$t('1950574'),
                    key: 'SectionName'
                },
                {
                    title: this.$t('950083'),
                    key: 'machineName'
                },
                {
                    title: this.$t('100307'),
                    key: 'productName'
                },
                {
                    title: this.$t('901048'),
                    key: 'itemName'
                },
                {
                    title: 'Value' + this.$t('920140'),
                    key: 'value'
                },
                {
                    title: 'Result' + this.$t('920140'),
                    key: 'result'
                },
                {
                    title: this.$t('200042'),
                    key: 'status_value'
                },
                {
                    title: this.$t('920139'),
                    key: 'userName'
                },
                {
                    title: this.$t('920138'),
                    key: 'changeTime'
                }
            ],
            editobj: {},
            leveldata: [],
            datalevel: [],
            sectionid: [],
            leveldata1: [],
            datalevel1: [],
            editsectionid: [],
            type: '',
            daterange: [],
            key: '',
            types: [],
            edittypes: [],
            configtype: '',
            edititems: [],
            field: '',
            selectids: [],
            showlevel: true,
            parameters: [],
            products: [],
            editproducts: [],
            productid: 0,
            machineid: 0,
            status: '',
            machines: [],
            type: 0
        }
    },
    created () {
        this.daterange = []
        this.search(1, 10)
        this.initlevel()
        this.initparameters()
        this.initproducts()
        this.initmachines()
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        datetrans (date) {
            if (date === '' || date === undefined || date === 0 || date === null) {
                return ''
            }
            date = new Date(date * 1000)
            var Y = date.getFullYear() + '-'
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
            var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' '
            return Y + M + D
        },
        search (page, pagesize) {
            var sectionid = this.sectionid[this.sectionid.length - 1]
            var params = {
                page: page,
                pagesize: pagesize,
                machineid: this.machineid,
                sectionid: sectionid,
                status: this.status,
                productid: this.productid,
                starttime: this.dtStart,
                endtime: this.dtEnd,
                key: this.key
            }
            axios
                .request({
                    url: '/qms/ParameterConfig/GetParamterPageList',
                    params,
                    method: 'get'
                })
                .then(res => {
                    this.page = page
                    this.pagesize = pagesize
                    this.total = res.data.total
                    this.mockTableData1(res.data.data)
                })

            this.obj = {}
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        id: item.id,
                        machineID: item.machineID,
                        machineName: item.machineName,
                        value: item.value,
                        result: item.result,
                        SectionID: item.sectionID,
                        SectionName: item.sectionName,
                        itemName: item.itemName,
                        itemID: item.itemID,
                        productName: item.productName,
                        productID: item.productID,
                        status: item.status,
                        status_value: item.status === 1 ? this.$t('800212') : this.$t('400014'),
                        userID: item.userID,
                        userName: item.userName,
                        changeTime: item.changeTime
                    })
                }
            })
            this.tableData1 = data
        },
        cancle () {
            this.editobj = {}
            this.editmodal = false
            this.search(this.page, this.pagesize)
        },
        selectdate (e) {
            this.dtStart = e[0]
            this.dtEnd = e[1]
            let last_str = this.dtEnd.split(' ')[1]
            if (last_str === '00:00:00') {
                this.dtEnd = moment(this.dtEnd).format('YYYY-MM-DD') + ' 23:59:59'
                this.daterange = [this.dtStart, this.dtEnd]
            }
        },
        add () {
            this.editobj = {}
            this.title1 = this.$t('1002')
            this.editmodal = true
            this.editobj.editsectionid = []
            this.type = 1
            this.initeditlevel()
            this.initeditproducts()
        },
        adddata () {
            let data = this.editobj
            data.sectionID = this.editobj.editsectionid[this.editobj.editsectionid.length - 1]
            axios.request({
                url: '/qms/ParameterConfig/AddParameter',
                data,
                method: 'post'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.obj = {}
                    this.$Message.success(this.$t('1060'))
                    this.search(this.page, this.pagesize)
                    this.editmodal = false
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        },
        edit () {
            if (JSON.stringify(this.obj) === '{}' || this.obj === null || this.obj === undefined) {
                this.$Message.error(this.$t('1043'))
                return false
            }
            this.editobj = {}
            this.title1 = this.$t('1003')
            this.editobj = this.obj
            this.editmodal = true
            this.editobj.editsectionid = []
            this.type = 2
            this.getdata()
            this.initeditlevel()
            this.initeditproducts()
            this.initparameters()
        },
        getdata () {
            this.initeditsectionid()
        },
        changesection () {
            axios.request({
                url: '/qms/SendTestCollection/GetParentSectionByItem?itemid=' + this.editobj.itemID,
                method: 'get'
            })
                .then(res => {
                    this.editobj.editsectionid = res.data.data
                    this.showlevel = false
                    this.showlevel = true
                })
        },
        changeitem () {
            if (this.editobj.productID > 0 && this.editobj.editsectionid.length > 0) {
                var sectionid = this.editobj.editsectionid[this.editobj.editsectionid.length - 1]
                axios.request({
                    url: '/qms/InspectStandard/GetItems?productid=' + this.editobj.productID + '&sectionid=' + sectionid,
                    method: 'get'
                })
                    .then(res => {
                        this.edititems = res.data.data
                    })
            }
        },
        initparameters () {
            axios.request({
                url: '/qms/ParameterConfig/GetParameterKeys',
                method: 'get'
            })
                .then(res => {
                    this.parameters = res.data.data
                })
        },
        initeditsectionid () {
            axios.request({
                url: '/qms/SendTestCollection/GetParentSection?sectionid=' + this.editobj.SectionID,
                method: 'get'
            })
                .then(res => {
                    this.editobj.editsectionid = res.data.data
                    this.showlevel = false
                    this.showlevel = true
                    this.changeitem()
                })
        },
        editdata () {
            let data = this.editobj
            data.sectionID = this.editobj.editsectionid[this.editobj.editsectionid.length - 1]
            axios.request({
                url: '/qms/ParameterConfig/UpdateParameter',
                data,
                method: 'post'
            }).then(res => {
                if (res.data.ret === 1) {
                    this.obj = {}
                    this.$Message.success(this.$t('1060'))
                    this.search(this.page, this.pagesize)
                    this.editmodal = false
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            })
        },
        initproducts () {
            axios.request({
                url: '/config/projectproduct/GetProducts',
                method: 'get'
            }).then(res => {
                this.products = res.data.data
            })
        },
        initeditproducts () {
            axios.request({
                url: '/config/projectproduct/GetProducts',
                method: 'get'
            }).then(res => {
                this.editproducts = res.data.data
            })
        },
        changeenable (state) {
            if (this.selectids === undefined || this.selectids === null || this.selectids === []) {
                this.$Message.error((this.$t('1017')))
                return false
            } else {
                this.$Modal.confirm({
                    title: this.$t('1124'),
                    content: state === 1 ? this.$t('1950576') : this.$t('1950577'),
                    onOk: () => {
                        let parameter = {
                            ids: this.selectids,
                            enable: state
                        }
                        axios.request({
                            url: state === 1 ? '/qms/ParameterConfig/StartEnable' : '/qms/ParameterConfig/EndEnable',
                            data: parameter,
                            method: 'post'
                        }).then(res => {
                            if (res.data.ret === 1) {
                                this.obj = {}
                                this.selectids = []
                                this.search(this.page, this.pagesize)
                                this.$Message.success(this.$t('1'))
                            } else {
                                this.$Message.error(this.$t(res.data.msg))
                            }
                        })
                    }
                })
            }
        },
        initmachines () {
            axios.request({
                url: '/config/Machine/GetAllMachines',
                method: 'get'
            }).then(res => {
                this.machines = res.data.data
            })
        },
        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
            })
        },
        initeditlevel () {
            axios.request({
                url: 'config/ProjectLevelSection/GetSectionTree',
                method: 'get'
            }).then(res => {
                const arr = []
                this.datalevel1 = res.data.data
                arr.push(res.data.data[0].value)
                this.leveldata1 = arr
            })
        },
        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)
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.obj = currentRow
        },
        handleSelectRow: function (data) {
            this.selectids = []
            for (var i in data) {
                if (this.selectids.indexOf(data[i].id) < 0) {
                    this.selectids.push(data[i].id)
                }
            }
        }
    }
}

</script>
<style lang="less" scoped>

</style>
