<template>
    <view class="home" style="padding-bottom: 150upx">
        <cu-custom class="home-custom" bgColor=".bg-sancolor"
                   bgImage="https://ahwgh.oss-cn-hangzhou.aliyuncs.com/enterprise/6525f808df760.png" :isBack="true">
            <block slot="content">抽样列表</block>
        </cu-custom>
        <view class="padding">
<!--            @tap="toSampleDetail(item.id,item.sampleStatus)"-->
            <view class="cu-list menu bg-white" v-for="(item, index) in sampleSheetDataList">
                <view class="cu-item solids-bottom">
                    <view class="title">
                        <text class="cuIcon-titles text-green"></text>
                        <text class="text-black text-bold text-lg">{{ index + 1 }}.{{ item.samplingNumber }}</text>
                    </view>
                    <view class="action">
                        <view class="text-blue" v-show="item.sampleStatus === 'SAMPLE_FILLED'">已填报</view>
                        <view class="text-gray" v-show="item.sampleStatus === 'SAMPLE_REPORT'">已上报</view>
                        <view class="text-red" v-show="item.sampleStatus === 'SAMPLE_WAIT_FILL'">待填报</view>
                    </view>
                </view>
                <view class="cu-item">
                    <view class="content detail padding-bottom">
                        <!-- 	<image src="/static/logo.png" class="png" mode="aspectFit"></image> -->
                        <div class="text-gray" v-if="item.samplingPeopleFirst">抽样人员1：{{ item.samplingPeopleFirst }}</div>
                        <div class="text-gray" v-if="item.samplingPeopleSecond">抽样人员2：{{ item.samplingPeopleSecond }}</div>
                        <div class="text-gray">创建时间：{{ item.createTime }}</div>
                        <div class="text-gray">更新时间：{{ item.updateTime }}</div>
<!--                        <div class="text-gray">状态：{{ setSampleStatus(item.sampleStatus) }}</div>-->
                    </view>
                </view>
                <view class="tools padding-sm">
                    <view @tap="deleteAhSampleSheet(item.id)">
                        <view class="btn-item text-black text-bold">
                            <text class="cuIcon-deletefill lg"></text>
                            <text class="text-sm">删除</text>
                        </view>
                    </view>
                    <view style="display: flex;justify-content: space-between;gap:30upx">
                        <view class="tool margin-left-sm" @tap="exportFile(item.id)"  v-if="item.sampleStatus === 'SAMPLE_FILLED'">
                            <view class="cu-tag bg-green light text-df text-bold">
                                <text class="cuIcon-newshotfill lg"></text>
                                导出抽样单
                            </view>
                        </view>

                        <view class="tool" @tap="toSampleDetail(item.id)">
                            <view class="cu-tag bg-blue light text-df text-bold">
                                去填报
                                <text class="cuIcon-right lg"></text>
                            </view>
                        </view>
                    </view>
                </view>
            </view>
        </view>
        <view class="reviewBtn" @tap="addSheet" v-if="showUpBtn">
            <view class="subBtn bg-green">
                添加
            </view>
        </view>
        <view class="padding flex flex-direction padding-bottom-xl fixedFootBtn bg-white padding-top-xs" v-if="sampleSheetDataList.length >= 1 && showUpBtn">
            <button class="cu-btn bg-green lg margin-top-sm shadow-blur" type="" @tap="upSheet">
                上报
            </button>
        </view>

        <view class="cu-modal" :class="modalName==='addSample'?'show':''">
            <view class="cu-dialog">
                <view class="cu-bar bg-white justify-end">
                    <view class="content" style="width: 100%">添加抽样单</view>
                    <view class="action" @tap="hideModal">
                        <text class="cuIcon-close text-red"></text>
                    </view>
                </view>
                <view class="padding-lr-lg bg-white padding-bottom">
                    <view class="cu-list menu">
                        <view class="cu-form-group ">
                            <view class="title">抽样人员1</view>
                            <input name="input" v-model="options.samplingPeopleFirst" placeholder="请输入抽样人员1">
                        </view>
                        <view class="cu-form-group ">
                            <view class="title">抽样人员2</view>
                            <input name="input" v-model="options.samplingPeopleSecond" placeholder="请输入抽样人员2">
                        </view>
                    </view>
                    <view class="padding flex flex-direction">
                        <button class="cu-btn bg-blue lg" @tap="submitSampleSheet">提交</button>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>


<script>
    import {
        _sampleList
    } from "./sample-list-model.js";
    import {
        _taskList
    } from "../task-list/task-list-model.js";
    import {_sampleSheetAdd} from '../sample-sheet-add/sample-sheet-add-model.js'

    export default {

        data() {
            return {
                sampleSheetDataList: [],
                sampleStatus: [],
                addBackground: "background-color:#c0c0c0",
                uploadBackground: "background-color:#c0c0c0",
                options: {},
                modalName:null,
                showUpBtn:true
            };
        },

        onLoad(options) {
            if (options.status === "SAMPLE_SUCCESS" || options.status === "CHECK_PROCESS" || options.status === "SAMPLE_CHECK_SUCCESS") {
                this.addBackground = "background-color: #c0c0c0";
                this.uploadBackground = "background-color: #c0c0c0";
                this.showUpBtn = false;
            } else {
                this.addBackground = "background-color: #00B525";
                this.uploadBackground = "background-color: #0081ff";
            }
            this.options = options;
            this.getSampleStatus();
        },

        onShow() {
            this.getAhSampleSheetList(this.options);
        },
        methods: {
            //添加
            addSheet() {
                if (this.options.status === "SAMPLE_SUCCESS" || this.options.status === "CHECK_PROCESS" || this.options.status === "SAMPLE_CHECK_SUCCESS") {
                    console.log("不可添加");
                } else {
                    console.log("调用添加弹窗");
                    console.log(this.options);
                    this.modalName = "addSample"
                    /*uni.navigateTo({
                        url: "/pages/sample-sheet-add/index?checkId=" + this.options.checkId + "&sampleId=" + this.options.sampleId
                            + "&taskId=" + this.options.taskId + "&zoneId=" + this.options.zoneId
                    });*/
                }
            },
            //新建抽样单
            submitSampleSheet(){
                if(!this.options.samplingPeopleFirst && !this.options.samplingPeopleSecond){
                    this.$msg("请填写抽样人员");
                    return false;
                }
                _sampleSheetAdd.createAhSampleSheet(this.options, res=>{
                    console.log(res.data)
                    const {data:{code,message}} = res;
                    if(code===200){
                        this.$success("添加成功");
                        delete this.options.samplingPeopleFirst;
                        delete this.options.samplingPeopleSecond;
                        this.getAhSampleSheetList(this.options);
                        this.modalName = null;
                    }else{
                        this.$msg(message);
                    }
                })
            },
            /**
             * 上报抽样单
             */
            upSheet() {
                if (this.options.status === "SAMPLE_SUCCESS" || this.options.status === "CHECK_PROCESS" || this.options.status === "SAMPLE_CHECK_SUCCESS") {
                    console.log("不可上报");
                    this.$msg("不可重复上报");
                } else {
                    _sampleList.upBatchSampleSheet(this.options,res=>{
                        console.log(res);
                        const {code,message} = res;
                        if(code === 200){
                            this.$success("上报成功");
                            this.getAhSampleSheetList(this.options);
                            this.showUpBtn = false;
                        }else{
                            this.$msg(message);
                        }
                    })
                }
            },
            /**
             * 删除抽样单
             */
            deleteAhSampleSheet(id){
                uni.showModal({
                    title: "提示",
                    content: "确定要删除当前样本么",
                    cancelText: "取消",
                    confirmText: "确认",
                    success: res => {
                        if (res.confirm) {
                            _sampleList.deleteAhSampleSheet(id, res => {
                                const {code,message} = res;
                                if(code === 200){
                                    this.$success("删除成功");
                                    this.getAhSampleSheetList(this.options);
                                }else{
                                    this.$msg(message);
                                }
                            });
                        }
                    }
                });
            },
            toSampleDetail(id, status) {
                uni.navigateTo({
                    url: "/pages/sample-detail/index?id=" + id + "&status=" + status
                });
            },
            exportFile(id) {
                _sampleList.exportSampleSheetPdf(id,res=>{
                    const {code,message} = res;
                    if(code === 200){
                        uni.downloadFile({
                            url: res.data, //仅为示例，并非真实的资源
                            success:res=> {
                                // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
                                if (res.statusCode === 200) {
                                    /*wx.playVoice({
                                        filePath: res.tempFilePath
                                    })*/
                                    this.$msg(JSON.stringify(res),100000);
                                    const filePath = res.tempFilePath
                                    uni.openDocument({
                                        filePath: filePath,
                                        showMenu:true,
                                        fileType:"pdf",
                                        success: ()=>{
                                            console.log('打开文档成功')
                                        },
                                        fail:(err)=>{
                                            this.$msg("文件查看失败");
                                            console.log("文件查看失败,err：",err)
                                        }
                                    })
                                }
                            },
                            fail:(err)=>{
                                this.$msg("文件下载失败");
                                console.log("文件下载失败,err：",err)
                            }
                        })
                    }else{
                        this.$msg("文件下载失败");
                        console.log("文件生成失败,message：",message)
                    }

                })
            },
            getAhSampleSheetList(item) {
                _sampleList.getAhSampleSheet(item, res => {
                    this.sampleSheetDataList = res.data.data.records;
                });
            },
            getSampleStatus() {
                _taskList.getDictionary("SAMPLE_SHEET_STATUS", res => {
                    this.sampleStatus = res.data.data;
                });
            },
            setSampleStatus(value) {
                let sampleStatus = this.sampleStatus;
                for (let i = 0; i < sampleStatus.length; i++) {
                    if (sampleStatus[i].itemValue === value) {
                        return sampleStatus[i].itemKey;
                    }
                }
            },
            hideModal(){
                this.modalName = "";
            },
        }
    };
</script>

<style scoped>
    .detail view {
        height: 1.8em;
        line-height: 1.8em !important;
        border: none;
        color: gray;
    }

    .tools {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 20upx;
    }
    .btn-item{
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
    }
    /*.cu-tag{
        !*padding: 20upx;*!
        border-radius: 10upx 10upx;
    }*/
</style>