import {stringify} from 'qs';
import request from './../utils/request';

export async function queryReportCustomerData(params) {
    return request(`/customer/report_customer/?${stringify(params)}`);
}

export async function queryReportDict() {
    return request(`/customer/dict/`);
}

export async function getUser() {
    return request(`/customer/get_user/`);
}

export async function queryReviewToday(params) {
    return request(`/customer/new_customer/?${stringify(params)}`);
}

export async function queryCustomerDetail(params) {
    return request(`/customer/new_customer/` + params.id + `/`);
}

export async function fetchCustomerReviewDetail(params) {
    return request(`/customer/review/` + params.id + `/`);
}

export async function queryReviewRecord(params) {
    return request(`/customer/get_review/?${stringify(params)}`);
}

export async function queryCheckCustomer(params) {
    return request(`/customer/review/?${stringify(params)}`);
}

export async function getProcess(params) {
    return request(`/order/get_process/?dispatch=1&customer_id=` + params.customer_id);
}

export async function queryOrderList(params) {
    return request(`/order/?${stringify(params)}`);
}

export async function queryProcessDetail(params) {
    return request(`/order/` + params.id + `/`);
}

export async function queryProcessRecord(params) {
    return request(`/order/get_details/?${stringify(params)}`);
}

export async function queryProcessDict(params) {
    return request(`/option/get_option/?types=4`);
}

export async function dispatchUser(params) {
    let formdata = new FormData();
    formdata.append('user', params.user[0]);
    return request('/customer/report_customer/' + params.id + '/dispatch_customer/', {
        method: 'POST',
        body: formdata,
    });
}

export async function dispatchService(params) {
    let formdata = new FormData();
    formdata.append('service', params.service[0]);
    formdata.append('next_process', params.next_process[0]);
    return request('/order/' + params.order + '/dispatch_service/', {
        method: 'POST',
        body: formdata,
    });
}

export async function addOrder(params) {
    let formdata = new FormData();
    formdata.append('notes', params.notes);
    formdata.append('stage_progress', params.next_process_id);

    params.file.map((item, index) => {
        formdata.append('file' + index.toString(), {
            uri: item.uri,
            type: 'application/octet-stream',
            name: item.fileName,
        });
    });
    return request('/customer/new_customer/' + params.customer + '/add_order/', {
        method: 'POST',
        body: formdata,
    });
}

export async function againDispatchUser(params) {
    let formdata = new FormData();
    formdata.append('track_user', params.user[0]);
    return request('/customer/new_customer/' + params.id + '/again_dispatch/', {
        method: 'POST',
        body: formdata,
    });
}

export async function addCustomerProject(params) {
    let formdata = new FormData();
    formdata.append('projects', JSON.stringify(params.projects));
    return request('/customer/new_customer/' + params.id + '/add_projects/', {
        method: 'POST',
        body: formdata,
    });
}

export async function checkReview(params) {
    let formdata = new FormData();
    formdata.append('check_status', params.check_status);
    formdata.append('check_comment', params.check_comment);
    formdata.append('next_time', params.next_time);
    return request('/customer/review/' + params.item.id + '/track_review/', {
        method: 'POST',
        body: formdata,
    });
}

export async function editCustomerInfo(params) {
    let formdata = new FormData();
    formdata.append('name', params.name);
    formdata.append('gender', params.gender);
    formdata.append('tel', params.tel);
    formdata.append('village', params.village);
    formdata.append('address', params.address);
    formdata.append('source', params.source[0]);
    formdata.append('project', JSON.stringify(params.project));
    formdata.append('notes', params.notes);
    return request('/customer/new_customer/' + params.id + '/', {
        method: 'PUT',
        body: formdata,
    });
}

export async function addReview(params) {
    let formdata = new FormData();
    formdata.append('instruction', params.instruction);
    formdata.append('description', params.description);
    formdata.append('next_time', params.next_time);
    formdata.append('is_giveup', params.is_giveup);
    formdata.append('is_entry', params.is_entry);
    return request('/customer/new_customer/' + params.item.id + '/add_review/', {
        method: 'POST',
        body: formdata,
    });
}

export async function addInternalReview(params) {
    let formdata = new FormData();
    formdata.append('description', params.description);
    formdata.append('next_time', params.next_time);
    formdata.append('is_copy', params.is_copy);
    return request('/customer/new_customer/' + params.item.id + '/inner_review/', {
        method: 'POST',
        body: formdata,
    });
}

export async function signRepeat(params) {
    return request('/customer/report_customer/' + params.id + '/sign_hit/', {
        method: 'POST',
    });
}

export async function saveReportCustomer(params) {
    let formdata = new FormData();
    formdata.append('name', params.name);
    formdata.append('gender', params.gender);
    formdata.append('tel', params.tel);
    formdata.append('village', params.village);
    formdata.append('address', params.address);
    formdata.append('source', params.source[0]);
    formdata.append('project', JSON.stringify(params.project));
    formdata.append('notes', params.notes);
    return request('/customer/report_customer/', {
        method: 'POST',
        body: formdata,
    });
}
