<template>
    <div>
        <search-bar label="计划名称"
                    highSearch
                    @search="_search"
                    @high-search="_highSearch"
                    @clear-high="_clearHigh">
            <template #highSearch>
                <van-field v-model="formObj.samplingNum" label="抽样单编号" placeholder="请输入抽样单编号"></van-field>
                <datetime-field label="抽样开始日期" v-model="formObj.samplingDateBegin"></datetime-field>
                <datetime-field label="抽样结束日期" v-model="formObj.samplingDateEnd"></datetime-field>
                <van-field v-model="formObj.taskName" label="任务名称" placeholder="请输入任务名称"></van-field>
                <van-field v-model="formObj.testedName" label="被抽样单位" placeholder="请输入被抽样单位名称"></van-field>
            </template>
        </search-bar>
        <div class="layout-cont-sh">
            <!--下拉刷新  https://vant-contrib.gitee.io/vant/#/zh-CN/pull-refresh-->
            <van-pull-refresh v-model="refreshing" @refresh="_refresh">
                <!--上拉加载 https://vant-contrib.gitee.io/vant/#/zh-CN/list-->
                <van-list
                        v-model="loading"
                        :finished="finished"
                        finished-text="没有更多了"
                        @load="_load">
                    <van-swipe-cell v-for="item in resultList" :key="item.name">
                        <div class="result-item" @click="_goto(item)">
                            <div>抽样单编号：{{item.samplingNum}}</div>
                            <div>抽样日期：{{item.samplingDate?$dateformat(item.samplingDate,'yyyy-mm-dd'):''}}</div>
                            <div>任务名称：{{item.taskName}}</div>
                            <div>任务编号：{{item.taskNum}}</div>
                            <div>生产者名称：{{item.proName}}</div>
                            <div>样品名称：{{item.sampleName}}</div>
                            <div>被抽样单位名称：{{item.testedName}}</div>
                        </div>
                        <template #right>
                            <van-button square type="primary" text="打印" class="swipe-cell-btn"
                                        @click="_print(item)"></van-button>
                        </template>
                    </van-swipe-cell>
                </van-list>
            </van-pull-refresh>
        </div>
    </div>
</template>

<script>
    import {samplingSummary} from '../../api'

    export default {
        name: "SamplingList",
        data() {
            return {
                formObj: {
                    samplingNum: '',
                    samplingDateBegin: '',
                    samplingDateEnd: '',
                    taskName: '',
                    testedName: '',
                    isHistory: 1,
                    isSelf: 1
                },
                key: '',
                resultList: [],
                page: 1,
                rows: 10,
                refreshing: false,//刷新中...
                loading: false,//加载中...
                finished: false,//没有更多数据
            }
        },
        mounted() {
            this._getData()
        },
        methods: {
            //简单查询 清空高级查询条件 赋值简单查询数据
            _search(value) {
                this.key = value
                this.formObj = this.$resetFields(this.formObj)
                this.formObj.isHistory = 1
                this.formObj.isSelf = 1
                this._refresh()
            },
            //高级搜索 清空key 查询
            _valParams() {
                if (this.formObj.samplingDateBegin === '' && this.formObj.samplingDateEnd !== '') {
                    this.$toast('请选择抽样开始日期！')
                    return false
                } else if (this.formObj.samplingDateBegin !== '' && this.formObj.samplingDateEnd === '') {
                    this.$toast('请选择抽样结束日期！')
                    return false
                } else if (this.formObj.samplingDateBegin !== '' && this.formObj.samplingDateEnd !== '' && (new Date(this.formObj.planDateBegin) > new Date(this.formObj.planDateEnd))) {
                    this.$toast('抽样开始日期不能大于抽样结束日期！')
                    return false
                }
                return true
            },
            _highSearch() {
                this.key = ''
                if (this._valParams()) {
                    this.$refs.searchBar._hideHighSearch()
                    this._refresh()
                }
            },
            _clearHigh() {
                this.formObj = this.$resetFields(this.formObj)
                this.formObj.isHistory = 1
                this.formObj.isSelf = 1
            },
            _searchParams() {
                let data = {
                    page: this.page,
                    rows: this.rows,
                    ...this.formObj
                };
                if (this.key) {
                    data.samplingNum = this.key
                }
                return this.$serializeForm(data)
            },
            _getData: async function () {
                let result = await samplingSummary.page(this._searchParams())
                this.resultList = [...(this.page === 1 ? [] : this.resultList), ...result.records]
                this.refreshing = false
                this.loading = false
                if (this.resultList.length === result.total) {
                    this.finished = true
                }
            },
            _refresh() {
                this.page = 1;
                this._getData()
            },
            _load() {
                this.page = this.page + 1;
                this._getData()
            },
            _print(item) {
                //调用第三放程序打开指定文件（必须是本地路径）
                this.$toast.loading({
                    message: '加载中...',
                    forbidClick: true,
                });
                var dtask = plus.downloader.createDownload(
                    this.$global.baseURL + '/drs/v1/sampling_summary/export_multi_sampling?id=' + item.id, {}, (d, status) => {
                        if (status == 200) {
                            plus.runtime.openFile(d.filename);//cn.wps.moffice_eng-----wps的包名
                        }
                        this.$toast.clear();
                    });
                dtask.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                dtask.setRequestHeader('accessToken', localStorage.getItem('accessToken'));
                dtask.start();
            },
            _goto(item) {
                if (item.testedType === 1) {
                    this.$router.push({path: '/sampling_list/sampling_list_detail_net', query: {id: item.id}})
                } else {
                    this.$router.push({path: '/sampling_list/sampling_list_detail', query: {id: item.id}})
                }
            }
        }
    }
</script>

<style scoped>

</style>
