<template>
    <a-modal
      title="选择到货单(Select arrival Order)"
      width="95%"
      :visible="visible"
      :maskClosable="false"
      switchFullscreen
      @ok = "handleOk"
      @cancel="handleCancel">
        <div>
          <a-card  :body-style="{ padding: '10px' }" :bordered="false" style="margin: 10px;">
            <div class="table-page-search-wrapper">
                <a-form :model="queryParams" :label-col="labelCol" :wrapper-col="wrapperCol" @keyup.enter.native="searchQuery">
                <a-row :gutter="24">
                    <a-col :md="6" :sm="8">
                        <a-form-item label="到货单号(bill code)">
                            <j-input placeholder="请输入" v-model:value="queryParams.billCode"></j-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md="6" :sm="8">
                        <a-form-item label="到货日期(arrival date)">
                            <a-range-picker value-format="YYYY-MM-DD"  v-model:value="arrivalDate"  @change="changeBillDate" class="query-group-cust"/>
                        </a-form-item>
                    </a-col> 
                    <template v-if="toggleSearchStatus">
                        <a-col :md="6" :sm="8">
                            <a-form-item label="项目(project)">
                                <ApiSelect
                                    :api="ProjectOption"
                                    showSearch
                                    v-model:value="queryParams.project"
                                    optionFilterProp="label"
                                    resultField="records"
                                    labelField="name"
                                    valueField="id"
                                    :params='{pageSize:-1}'
                                    :disabled="fatherProject!==''"
                                />
                            </a-form-item>
                        </a-col>
                        <a-col :md="6" :sm="8">
                            <a-form-item label="产品分类(production class)" :label-col="labelCol1" :wrapper-col="wrapperCol1">
                                <ApiSelect
                                :api="ClassList"
                                showSearch
                                v-model:value="queryParams.productionClass"
                                optionFilterProp="label"
                                resultField="records"
                                labelField="name"
                                valueField="id"
                                :params='{pageSize:-1}'
                                />
                            </a-form-item>
                        </a-col>
                        <a-col :md="6" :sm="8">
                            <a-form-item label="供应商(supplier)">
                                <ApiSelect
                                :api="supplierOption"
                                showSearch
                                v-model:value="queryParams.supplier"
                                optionFilterProp="label"
                                resultField="records"
                                labelField="name"
                                valueField="id"
                                :params='{pageSize:-1}'
                                />
                            </a-form-item>
                        </a-col>
                        <a-col :md="6" :sm="8">
                            <a-form-item label="优先级(priority)" >
                                <JDictSelectTag v-model:value="queryParams.priority" placeholder="请选择" dictCode="priority" style="width: 100%;"/>
                            </a-form-item>
                        </a-col>
                        
                        <a-col :md="6" :sm="8">
                            <a-form-item label="机型(model)">
                                <JDictSelectTag v-model:value="queryParams.model" placeholder="请选择" dictCode="model_typer"/>
                            </a-form-item>
                        </a-col>
                        <a-col :md="6" :sm="8">
                            <a-form-item label="产品编码(product code)" :label-col="labelCol1" :wrapper-col="wrapperCol1">
                                <j-input placeholder="请输入" v-model:value="queryParams.productCode"></j-input>
                            </a-form-item>
                        </a-col>                  
                    </template>
                    <a-col :md="6" :sm="8">
                        <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                            <a-button type="primary" @click="searchQuery" >查询(search)</a-button>
                            <a-button type="primary" @click="searchReset"  style="margin-left: 8px">重置(reset)</a-button>
                            <a @click="handleToggleSearch" style="margin-left: 8px">
                            {{ toggleSearchStatus ? '收起' : '展开' }}
                            <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
                            </a>
                        </span>
                    </a-col>
                </a-row>
            </a-form>
           </div>
        </a-card>

        <a-card  :body-style="{ padding: '10px' }" :bordered="false" style="margin: 10px;">
            <a-alert type="info" show-icon class="alert" style="margin-bottom: 8px">
            <template #message>
                <template v-if="selectedRowKeys.length > 0">
                <span>已选中 {{ selectedRowKeys.length }} 条记录</span>
                <a-divider type="vertical" />
                <a @click="selectedRowKeys = []">清空</a>
                </template>
                <template v-else>
                <span>未选中任何数据</span>
                </template>
            </template>
            </a-alert>
            <a-table
                :columns="columns"
                :row-key="record => record.childId"
                :data-source="dataSource"
                bordered
                size="small"
                @change="handleTableChange"
                :pagination="pagination"
                :scroll="{ x: 3200, y: 300 }"
                :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            >
            </a-table>
        </a-card>
      </div>
    </a-modal>
</template>
<script lang="ts" setup>
    import {ref, reactive } from 'vue';
    import { defHttp} from '/@/utils/http/axios';
    import { message } from 'ant-design-vue';
    import { filterObj} from '/@/utils/common/compUtils';
    import { JDictSelectTag,ApiSelect,JInput} from '/@/components/Form';
    const emit = defineEmits([ 'selectArrrivalOrder']); //定义emit
    import {supplierOption,ProjectOption,ClassList} from '../inventiry/purchaseIn/purchaseInForm.api';
    var visible = ref(false)
    var fatherProject = ref('')
    var fatherSourceCode = ref('')
    const columns = [
        { 
            type: 'selection',
            fixed: 'left',
            width:0
        },
        {
            title: '到货单号(bill code)',
            dataIndex: 'billCode',
            key: 'billCode',
            align:"center",
            width:250,
            
        },
        {
            title: '到货日期(arrival date)',
            dataIndex: 'arrivalDate',
            key: 'arrivalDate',
            align:"center"
        },
        {
            title: '项目(project)',
            dataIndex: 'projectName',
            key: 'projectName',
            align:"center",
            width:250,
        },
        {
            title: '供应商(supplier)',
            dataIndex: 'supplierName',
            key: 'supplierName',
            align:"center"
        },
        {
            title: '优先级(priority)',
            dataIndex: 'priority_dictText',
            key: 'priority_dictText',
            align:"center"
        },
        {
            title: '产品分类(production class)',
            dataIndex: 'productionClass_dictText',
            key: 'productionClass_dictText',
            align:"center",
            width:250,
        },
        {
            title: '机型(model)',
            dataIndex: 'model',
            key: 'model',
            align:"center"
        },
        {
            title: '采购部门(puechase department)',
            dataIndex: 'purchaseDepartment',
            key: 'purchaseDepartment',
            align:"center"
        },
        {
            title: '采购员(puechaseman)',
            dataIndex: 'purchaseman',
            key: 'purchaseman',
            align:"center"
        },
        {
            title: '产品英文名(English name)',
            key: 'englishName',
            dataIndex: 'englishName',
            align:"center",
            width:250,
        },
        {
            title: '型号(childModel)',
            key: 'childModel',
            dataIndex: 'childModel',
            align:"center"
        },
        {
            title: '厂家(factory)',
            key: 'factory',
            dataIndex: 'factory',
            align:"center",
            width:250
        },
        {
            title: '质量等级(quantity grade)',
            key: 'qualityGrade',
            dataIndex: 'qualityGrade',
            align:"center",
            width:250,
        },
    ];
    const labelCol = ref({
    xs: { span: 24 },
    sm: { span: 9 },
    });
    const wrapperCol = ref({
        xs: { span: 24 },
        sm: { span: 15 },
    });
    const labelCol1 = ref({
    xs: { span: 24 },
    sm: { span: 10 },
    });
    const wrapperCol1 = ref({
        xs: { span: 24 },
        sm: { span: 14 },
    });
    const dataSource =ref([]);
    let selectedRowKeys = ref([]);
    let selectedRows = ref([]);
    const toggleSearchStatus = ref(false);
    var arrivalDate = ref([])
    const queryParams = ref({
        billCode:'',
        project:'',
        projectName:'',
        productionClass:'',
        priority:'',
        supplier:'',
        supplierName:'',
        model:'',
        productCode:'',
        arrivalDate_begin:'',
        arrivalDate_end:'',
    });
    let pagination = ref({
      current: 1,
      pageSize: 10,
      total: '', // 假设总共有100条数据
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
      },
      size:'small'
    });
    function loadData(){
        let params = getQueryParams();
        defHttp.get({ url: '/purCode/purDeliveryNote/purDeliveryNoteArrivalAlert',params}, { isTransformResponse: false })
        .then((res) => {
            if (res.success) {
                dataSource.value = res.result.records;
                pagination.value.total = res.result.total;
                pagination.value.current = res.result.current;
                pagination.value.pageSize = res.result.size;                
            } else {
                message.error(res.message);
            }
        })
        .finally(() => {
            // loading.value = false;
        });
    }
    function getQueryParams(){
        let params = Object.assign(queryParams.value);
        params.pageNo = pagination.value.current;
        params.pageSize = pagination.value.pageSize;
        if(fatherProject.value&&fatherProject.value!==''){
            queryParams.value.project = fatherProject.value
        }else {
            queryParams.value.project = params.project
        }
        return filterObj(params);
    }
    function handleTableChange(paginations, filters, sorter){
        pagination.value.total = paginations.total;
        pagination.value.current = paginations.current;
        pagination.value.pageSize = paginations.pageSize;
        loadData()
    };
    function searchQuery(){
        pagination.value.current =1;
        loadData();
    }
    function searchReset(){
        arrivalDate.value = []
        queryParams.value = {
            billCode:'',
            project:'',
            projectName:'',
            productionClass:'',
            priority:'',
            supplier:'',
            supplierName:'',
            model:'',
            productCode:'',
            arrivalDate_begin:'',
            arrivalDate_end:'',
        }
        pagination.value.current =1;
        pagination.value.pageSize = 10; 
        loadData();
    }
    function handleToggleSearch(){
        toggleSearchStatus.value = !toggleSearchStatus.value;
    }
    function onSelectChange(keys,rows){
        selectedRowKeys.value = keys
        selectedRows.value = rows
    }
    function handleOk(){
        var arr = []
        selectedRows.value.map(item=>arr.push(item.billCode))
        if(fatherSourceCode.value&&fatherSourceCode.value!==''){
            arr.push(fatherSourceCode.value)
        }
        if(selectedRowKeys.value.length==0){
            message.error('请勾选数据');
        }else if(new Set(arr).size!==1){
            message.error('请勾选到货单号相同的数据');
        }else{
            emit('selectArrrivalOrder', selectedRows.value)
            handleCancel()
        }
    }
    function handleCancel(){
      visible.value = false
      selectedRowKeys.value = []
      selectedRows.value=[]
      queryParams.value = {
            billCode:'',
            project:'',
            projectName:'',
            productionClass:'',
            priority:'',
            supplier:'',
            supplierName:'',
            model:'',
            productCode:'',
            arrivalDate_begin:'',
            arrivalDate_end:'',
        }
        pagination.value.current =1;
        pagination.value.pageSize = 10; 
    }
    function getTable(formData){
        visible.value = true
        if(formData.projectName&&formData.projectName!==''){
            fatherProject.value = formData.project
        }else{
            fatherProject.value = ''
        }
        if(formData.sourceCode&&formData.sourceCode!==''){
            fatherSourceCode.value = formData.sourceCode
        }else{
            fatherSourceCode.value = ''
        }
        loadData()
    }
    function changeBillDate(prop){
       if(prop){
            arrivalDate.value = prop
            queryParams.value.arrivalDate_begin = prop[0]
            queryParams.value.arrivalDate_end = prop[1]
       }else{
            arrivalDate.value = []
            queryParams.value.arrivalDate_begin = ''
            queryParams.value.arrivalDate_end = ''
       }
       
    }
    defineExpose({
      getTable
    });
</script>
<style scoped lang="less">
/deep/.ant-form-item{
    margin-bottom: 8px !important;
}
// /deep/.ant-table-wrapper .ant-table-thead > tr > th, .ant-table-wrapper .ant-table-thead > tr > td{
//     padding: 8px !important;
// }

</style>