@auth
<script>
    new Vue({
        el: "#demand-div",
        data: {
            types: [{name:'0',value:'需求'},{name:'1',value:'问题'}],
            addDemand: {},
            authorities: [],
            authoritiesFilter: [],
            demandErrors: {},
            ratio:{
                unClaimCount:null,
                ClaimCount:null,
            }
        },
        created() {
            this.authoritiesFilter = JSON.parse(JSON.stringify(this.authorities));
            this.getAuthority();
            this.setUnClaimDemandRatio();
            $('#demand-div').removeClass('d-none');
        },
        mounted() {
        },
        methods: {
            /** 筛选 */
            filterAuth($e) {
                let value = $($e.target).val();
                let authorities = JSON.parse(JSON.stringify(this.authorities));
                if (value === null) {
                    this.authoritiesFilter = authorities;
                    return;
                }
                this.authoritiesFilter = authorities.filter(function (item) {
                    return item['name'].includes(value);
                });
                if(this.authoritiesFilter.length > 0)
                    this.addDemand.authority_id = this.authoritiesFilter[0]['id'];
            },
            /** 创建 */
            showAddDemand() {
                this.addDemand = {};
                this.authoritiesFilter = JSON.parse(JSON.stringify(this.authorities));
                $('#add-demand-auth-filter').val('');
                $('#add-demand').modal('show')
                if(this.authoritiesFilter.length === 0){
                    if(this.authorities.length !==0)this.authoritiesFilter = JSON.parse(JSON.stringify(this.authorities));
                    else this.getAuthority();
                }
            },
            /** 创建 */
            createDemand() {
                let url = '{{url('apiLocal/demand/store')}}';

                window.tempTip.setIndex(1999);
                window.tempTip.setDuration(3000);

                let formData = new FormData();
                let file = document.querySelector('#add-demand-file-create').files[0];
                if(this.addDemand['authority_id'])formData.append('authority_id', this.addDemand['authority_id']);
                if(this.addDemand['type'])formData.append('type', this.addDemand['type']);
                if(this.addDemand['description'])formData.append('description', this.addDemand['description']);
                if(file)formData.append('file', file);

                window.axios.post(url, formData, {
                    'Content-Type': 'multipart/form-data'
                }).then(res => {
                    if (res.data.success) {
                        window.tempTip.showSuccess('问题提交成功');
                        $('#add-demand').modal('hide')
                        return;
                    }
                    if(res.data.errors) this.demandErrors = res.data.errors;
                    else  window.tempTip.show('问题提交失败');
                }).catch(err => {
                    window.tempTip.show(err);
                });
            },
            getAuthority(){
                let url = '{{url('apiLocal/authority/get')}}';
                window.axios.get(url).then(res=>{
                    if(res.data.success){
                        this.authorities = res.data.data;
                        this.authoritiesFilter = res.data.data;
                        this.$forceUpdate();
                    }
                }).catch(err=>{
                    window.tempTip.show(err);
                });
            },
            setUnClaimDemandRatio(){
                let url = '{{url('apiLocal/demand/unClaimDemandRatio')}}';
                window.axios.get(url).then(res=>{
                    if(res.data.data){
                        this.$set(this.ratio,'unClaimCount',res.data.data['unClaimCount']);
                        this.$set(this.ratio,'ClaimCount',res.data.data['ClaimCount']);
                        return;
                    }
                    this.$set(this.ratio,'unClaimCount',null);
                    this.$set(this.ratio,'unDoneCount',null);
                }).catch(error=>{

                });
            }
        }
    });
</script>
@endauth
