<template>
    <a-modal
      title="选择供应商报价选定(select supplier quotation selection)"
      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)">
                            <a-input placeholder="请输入" v-model:value="queryParams.billCode"></a-input>
                        </a-form-item>
                    </a-col>
                    <a-col :md="6"  :sm="8">
                        <a-form-item label="单据日期(bill date)">
                            <a-range-picker value-format="YYYY-MM-DD"  v-model:value="queryParams.billDate" class="query-group-cust"/>
                        </a-form-item>
                    </a-col> 
                    <template v-if="toggleSearchStatus">
                        <a-col :md="6"  :sm="8">
                            <a-form-item label="项目(quotation project)">
                                <ApiSelect
                                    :api="projectListList"
                                    showSearch
                                    v-model:value="queryParams.inquiryProject"
                                    optionFilterProp="label"
                                    resultField="records"
                                    labelField="name"
                                    valueField="id"
                                    :disabled="fatherProject!==''"
                                    />
                            </a-form-item>
                        </a-col>
                           <a-col :md="6"  :sm="8">
                            <a-form-item  label="供应商(quotation supplier)">
                                <ApiSelect
                                    :api="supplierOption"
                                    showSearch
                                    v-model:value="queryParams.selectionSupplier"
                                    optionFilterProp="label"
                                    resultField="records"
                                    labelField="name"
                                    valueField="id"
                                />
                            </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"/>
                            </a-form-item>
                        </a-col>
                        <a-col :md="6"  :sm="8">
                            <a-form-item label="产品分类(production class" >
                                <JSelectInput   v-model:value="queryParams.productionClass"  placeholder="请选择" :options="classOption" ></JSelectInput>
                            </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="厂家(maker)">
                                <JDictSelectTag v-model:value="queryParams.maker" placeholder="请选择" dictCode="factory"/>
                            </a-form-item>
                        </a-col>
                        <a-col :md="6"  :sm="8">
                            <a-form-item label="产品编号(production code)">
                                <a-input placeholder="请输入" v-model:value="queryParams.productionCode" allow-clear ></a-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: 3500, 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 {  ApiSelect, } from '/@/components/Form/index';
    import { JDictSelectTag} from '/@/components/Form';
    import JSelectInput from '/@/components/Form/src/jeecg/components/JSelectInput.vue';
    import { supplierOption } from '../saleCode/quotation/quotationForm.api';
    const emit = defineEmits([ 'selectSupplierQuatationConfirm']); //定义emit
    let classOption = ref([])
    var visible = ref(false)
    var fatherProject = ref('')
    var fatherSourceCode = ref('')
    const columns = [
        {
            title: '报价单号(bill code)',
            align:"center",
            dataIndex: 'billCode'
        },   
        {
            title: '单据日期(bill date)',
            align:"center",
            dataIndex: 'billDate',
            customRender:({text}) =>{
            text = !text ? "" : (text.length > 10 ? text.substr(0,10) : text);
            return text;
            },
        },
        
        {
            title: '报价项目(quotation project)',
            align:"center",
            dataIndex: 'projectName'
        },
        {
            title: '供应商(supplier)',
            align:"center",
            dataIndex: 'selectionSupplier_dictText'
        },
        // {
        //     title: '询价供应商(quotation supplier)',
        //     align:"center",
        //     dataIndex: 'suppilerName'
        // },
        {
            title: '交期(delivery time)',
            align:"center",
            dataIndex: 'deliveryTime',
            width:250,
        },
        {
            title: '优先级(priority)',
            align:"center",
            dataIndex: 'priority_dictText'
        },
        {
            title: '产品分类(production class)',
            align:"center",
            dataIndex: 'productClass'
        },
        {
            title: '机型(model)',
            align:"center",
            dataIndex: 'model'
        },
        {
            title: '产品编码(product code)',
            align:"center",
            dataIndex: 'productCode'
        },
        {
            title: '产品英文名(english name)',
            align:"center",
            dataIndex: 'englishName'
        },
        {
            title: '厂家(maker)',
            align:"center",
            dataIndex: 'maker'
        },
        {
            title: '质量等级(quality grade)',
            align:"center",
            dataIndex: 'qualityGrade'
        },
        {
            title: '单价(price)',
            align:"center",
            dataIndex: 'taxPrice'
        },
        {
            title: '含金额(amount)',
            align:"center",
            dataIndex: 'taxAmount'
        },
        {
            title: '单位(unit)',
            align:"center",
            dataIndex: 'unit'
        },
        {
            title: '数量(quantity)',
            align:"center",
            dataIndex: 'quantity'
        },
    ];
    const labelCol = ref({
    xs: { span: 24 },
    sm: { span: 11 },
    });
    const wrapperCol = ref({
        xs: { span: 24 },
        sm: { span: 13 },
    });
    const dataSource =ref([]);
    let selectedRowKeys = ref([]);
    let selectedRows = ref([]);
    const toggleSearchStatus = ref(false);
    const queryParams = ref({
        billCode:'',
        billDate:'',
        selectionSupplier:'',
        priority:'',
        productionClass:'',
        model:'',
        maker:'',
        productionCode:'',
        inquiryProject:'',
        projectName:''
    });
    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/purQuotationSelection/purQuotationSelectionProductAlert',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.inquiryProject = fatherProject.value
        }else{
            queryParams.value.inquiryProject = params.inquiryProject
        }
        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 getOptiom(){
    defHttp
        .get({ url: 'baseCode/baseProductClass/list'}, { isTransformResponse: false })
        .then((res) => {
            if (res.success) {
              classOption.value = []
              res.result.records.forEach(element => {
                  var obj = {
                    label: element.name?element.name:'无名称请维护',
                    value: element.id?element.id:''
                  };
                  classOption.value.push( obj)
              });    
            }
        })
        .finally(() => {
            // loading.value = false;
        });
  }
    function searchQuery(){
        loadData();
    }
    function searchReset(){
        queryParams.value = {
            billCode:'',
            billDate:'',
            selectionSupplier:'',
            priority:'',
            productionClass:'',
            model:'',
            maker:'',
            productionCode:'',
            inquiryProject:'',
            projectName:''
        }
        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('selectSupplierQuatationConfirm', selectedRows.value)
        handleCancel()
      }
    }
    function handleCancel(){
      visible.value = false
      selectedRowKeys.value = []
      selectedRows.value=[]
      queryParams.value = {
            billCode:'',
            billDate:'',
            selectionSupplier:'',
            priority:'',
            productionClass:'',
            model:'',
            maker:'',
            productionCode:'',
            inquiryProject:'',
            projectName:''
        }
    }
    function getTable(formData,data){
        visible.value = true
        if(data){
            if(formData.projectName&&formData.projectName!==''){
                fatherProject.value = formData.project
            }else{
                fatherProject.value = ''
            }
            if(formData.sourceCode&&formData.sourceCode!==''){
                fatherSourceCode.value = formData.sourceCode
            }else{
                fatherSourceCode.value = ''
            }
        }else{
            if(formData.quotationProjectName&&formData.quotationProjectName!==''){
             fatherProject.value = formData.quotationProject
            }else{
                fatherProject.value = ''
            }
            if(formData.sourceCode&&formData.sourceCode!==''){
                fatherSourceCode.value = formData.sourceCode
            }else{
                fatherSourceCode.value = ''
            }
        }       
        loadData()
        getOptiom()
    }
    function projectListList(){
        let params = {pageSize:-1}
        return defHttp.get({url:'/baseCode/baseProjectArchive/list',params});
    }
    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>