<template>
    <div class="supplierToMessage" style="display:flex;">
        <DropDownSelectFilter
            :single="true"
            class="input_box"
            :data="shopData"
            :total-row-count="shopData.totalRowCount"
            :show-colname-key="'isak'"
            :default-selected="defaultSelected"
            @on-page-change="changePageShop($event)"
            @on-input-value-change="InputValueChangeShop($event)"
            @on-popper-show="searchShopData()"
            @on-fkrp-selected="checkDataShop($event)"
            @on-clear="clearDataShop()"
            :data-empty-message="dataEmptyMessage"
            :columns="columnsShop"
            :auto-data="AutoDataShop">
        </DropDownSelectFilter>
    </div>
</template>
  
  <script type="text/ecmascript-6">
  const R3 = window.R3;
  const { network, urlSearchParams } = R3;
  const { setFormItemValue } = R3.componentsAttribute;
  export default {
    name: 'supplierToMessage',
  
    props: {
      options: {
        type: Object,
        default: () => ({})
      },
      value: {
        type: [Array,String,Number],
        default: () => ''
      }
    },
  
    data() {
      return {
        defaultSelected:[],
        shopData:{},
        currentIndex:0,//当前也从第几条索引开始
        dataEmptyMessage: '数据加载中...', // 无数据的提示
        columnsShop:['name','value'],// 店铺数据展现的组
        AutoDataShop:[],//店铺模糊搜索数据

        provinceDefault:[], // 供货商带出来的省
        cityDefault:[], // 供货商带出来的市
        areaDefault:[], // 供货商带出来的区
        TOWN:'', // 街道
        ADDRESS: '', // 详细地址

        shopDataAll:{},
      };
    },
    created() {
      console.log(111,this.options)
    },
    // watch: {
    //   value(val){
    //     this.defaultSelected = val
    //   }
    // },
  
    methods: {
      // 下拉分页模糊搜索框
      // 外键下拉框
      searchShopData(){
        let virtual={};
        if(window.location.href.indexOf('T_RET_PUR') > -1){
          virtual={
            searchdata:{
              "isdroplistsearch":true,
              "refcolid":205925,
              "startindex":this.currentIndex,
              "range":10,
            }
          }
        }else if(window.location.href.indexOf('T_SALE') > -1) {
          virtual= {
            searchdata:{
              "isdroplistsearch":true,
              "refcolid":206378,
              "startindex":this.currentIndex,
              "range":10,
            }
          }
        }else if(window.location.href.indexOf('T_V_WMSALLOCATEBILL') > -1) {
          virtual= {
            searchdata:{
              "isdroplistsearch":true,
              "refcolid":198106,
              "startindex":this.currentIndex,
              "range":10,
            }
          }
        }
        network.post('/p/cs/QueryList',urlSearchParams(virtual)).then((res) => {
          if(res.data.code==0){
            this.shopData=res.data.data
            this.currentIndex=0
          }
        })
      },
        // 单选下拉选中
        checkDataShop(val){
          let virtual={};
          if(window.location.href.indexOf('T_RET_PUR') > -1){
            virtual={
              searchdata:{
                "isdroplistsearch":true,
                "refcolid":205925,
                "startindex":this.currentIndex,
                "range":10,
                "fixedcolumns":{"ID":val[0].ID},
              }
            }
          }else if(window.location.href.indexOf('T_SALE') > -1) {
            virtual= {
              searchdata:{
                "isdroplistsearch":true,
                "refcolid":206378,
                "startindex":this.currentIndex,
                "range":10,
                "fixedcolumns":{"ID":val[0].ID},
              }
            }
          }else if(window.location.href.indexOf('T_V_WMSALLOCATEBILL') > -1) {
            virtual= {
              searchdata:{
                "isdroplistsearch":true,
                "refcolid":198106,
                "startindex":this.currentIndex,
                "range":10,
                "fixedcolumns":{"ID":val[0].ID},
              }
            }
          }
          network.post('/p/cs/QueryList',urlSearchParams(virtual)).then((res) => {
            if(res.data.code==0){
              res.data.data.row.forEach(item => {
            if(item.ID.val==val[0].ID){
              if(window.location.href.indexOf('T_V_WMSALLOCATEBILL') > -1){
                let paramsOne = {
                  table: 'T_V_IESWAREHOUSE',
                  objid: item.IESWAREHOUSEID.refobjid
                }
                network.post('/p/cs/getObject',urlSearchParams(paramsOne)).then((res) => {
                  if(res.data.code==0){
                      let warehouseMsg = res.data.data.addcolums[1].childs;
                      this.provinceDefault = warehouseMsg.find(item=>item.colname=='SELLPROVINCEID').valuedata?[{
                        ID: warehouseMsg.find(item=>item.colname=='SELLPROVINCEID').valuedata,
                        Label: warehouseMsg.find(item=>item.colname=='SELLPROVINCEID').refobjid
                      }]:[];
                      this.cityDefault = warehouseMsg.find(item=>item.colname=='SELLCITYID').valuedata?[{
                        ID: warehouseMsg.find(item=>item.colname=='SELLCITYID').valuedata,
                        Label: warehouseMsg.find(item=>item.colname=='SELLCITYID').refobjid
                      }]:[];
                      this.areaDefault = warehouseMsg.find(item=>item.colname=='SELLAREAID').valuedata?[{
                        ID: warehouseMsg.find(item=>item.colname=='SELLAREAID').valuedata,
                        Label: warehouseMsg.find(item=>item.colname=='SELLAREAID').refobjid
                      }]:[];
                      this.ADDRESS = warehouseMsg.find(item=>item.colname=='ADDRESS').valuedata;
                      // this.TOWN = item.STREET.val;

                      let ChangeList = [
                        {
                          "COLUMN_NAME":"RECEIVER_NAME",
                          "COLUMN_TYPE":1,
                          "LABLE_VALUES":[
                            {
                              "LABLE":warehouseMsg.find(item=>item.colname=='CONTACTNAME').valuedata,
                              "VALUE":warehouseMsg.find(item=>item.colname=='CONTACTNAME').valuedata
                            }
                          ]
                        },
                        {
                          "COLUMN_NAME":"RECEIVER_MOBILE",
                          "COLUMN_TYPE":1,
                          "LABLE_VALUES":[
                            {
                              "LABLE":warehouseMsg.find(item=>item.colname=='MOBILE').valuedata,
                              "VALUE":warehouseMsg.find(item=>item.colname=='MOBILE').valuedata
                            }
                          ]
                        },
                        {
                          "COLUMN_NAME":"RECEIVER_PHONE",
                          "COLUMN_TYPE":1,
                          "LABLE_VALUES":[
                            {
                              "LABLE":warehouseMsg.find(item=>item.colname=='PHONE').valuedata,
                              "VALUE":warehouseMsg.find(item=>item.colname=='PHONE').valuedata
                            }
                          ]
                        },
                        {
                          "COLUMN_NAME":"RECEIVER_PROVINCE",
                          "COLUMN_TYPE":0,
                          "LABLE_VALUES":[
                            {
                              "LABLE":warehouseMsg.find(item=>item.colname=='SELLPROVINCEID').valuedata,
                              "VALUE":warehouseMsg.find(item=>item.colname=='SELLCITYID').refobjid
                            }
                          ]
                        },
                        {
                          "COLUMN_NAME":"RECEIVER_CITY",
                          "COLUMN_TYPE":0,
                          "LABLE_VALUES":[
                            {
                              "LABLE":warehouseMsg.find(item=>item.colname=='SELLCITYID').valuedata,
                              "VALUE":warehouseMsg.find(item=>item.colname=='SELLCITYID').refobjid
                            }
                          ]
                        },
                        {
                          "COLUMN_NAME":"RECEIVER_AREA",
                          "COLUMN_TYPE":0,
                          "LABLE_VALUES":[
                            {
                              "LABLE":warehouseMsg.find(item=>item.colname=='SELLAREAID').valuedata,
                              "VALUE":warehouseMsg.find(item=>item.colname=='SELLAREAID').refobjid
                            }
                          ]
                        },
                        // {
                        //   "COLUMN_NAME":"RECEIVER_STREET",
                        //   "COLUMN_TYPE":1,
                        //   "LABLE_VALUES":[
                        //     {
                        //       "LABLE":item.STREET.val,
                        //       "VALUE":item.STREET.val
                        //     }
                        //   ]
                        // },
                        {
                          "COLUMN_NAME":"RECEIVER_ADDRESS",
                          "COLUMN_TYPE":1,
                          "LABLE_VALUES":[
                            {
                              "LABLE":warehouseMsg.find(item=>item.colname=='ADDRESS').valuedata,
                              "VALUE":warehouseMsg.find(item=>item.colname=='ADDRESS').valuedata
                            }
                          ]
                        },
                      ]
                      setFormItemValue(ChangeList,this.$parent.$parent.$parent)
                  }
                })
              }else {
                this.provinceDefault = item.PROVINCE.val?[{
                  ID: item.PROVINCE.reftableid,
                  Label: item.PROVINCE.val
                }]:[];
                this.cityDefault = item.CITY.val?[{
                  ID: item.CITY.reftableid,
                  Label: item.CITY.val
                }]:[];
                this.areaDefault = item.AREA.val?[{
                  ID: item.AREA.reftableid,
                  Label: item.AREA.val
                }]:[];
                this.ADDRESS = item.ADDRESS.val;
                this.TOWN = item.STREET.val;

                let ChangeList = [
                  {
                    "COLUMN_NAME":"RECEIVER_NAME",
                    "COLUMN_TYPE":1,
                    "LABLE_VALUES":[
                      {
                        "LABLE":window.location.href.indexOf('T_SALE') > -1?item.CONTACTORNAME.val:item.CONTACTER.val,
                        "VALUE":window.location.href.indexOf('T_SALE') > -1?item.CONTACTORNAME.val:item.CONTACTER.val
                      }
                    ]
                  },
                  {
                    "COLUMN_NAME":"RECEIVER_MOBILE",
                    "COLUMN_TYPE":1,
                    "LABLE_VALUES":[
                      {
                        "LABLE":item.MOBILE.val,
                        "VALUE":item.MOBILE.val
                      }
                    ]
                  },
                  {
                    "COLUMN_NAME":"RECEIVER_PHONE",
                    "COLUMN_TYPE":1,
                    "LABLE_VALUES":[
                      {
                        "LABLE":item.PHONE.val,
                        "VALUE":item.PHONE.val
                      }
                    ]
                  },
                  {
                    "COLUMN_NAME":"RECEIVER_PROVINCE",
                    "COLUMN_TYPE":0,
                    "LABLE_VALUES":[
                      {
                        "LABLE":item.PROVINCE.val,
                        "VALUE":item.PROVINCE.refobjid
                      }
                    ]
                  },
                  {
                    "COLUMN_NAME":"RECEIVER_CITY",
                    "COLUMN_TYPE":0,
                    "LABLE_VALUES":[
                      {
                        "LABLE":item.CITY.val,
                        "VALUE":item.CITY.refobjid
                      }
                    ]
                  },
                  {
                    "COLUMN_NAME":"RECEIVER_AREA",
                    "COLUMN_TYPE":0,
                    "LABLE_VALUES":[
                      {
                        "LABLE":item.AREA.val,
                        "VALUE":item.AREA.refobjid
                      }
                    ]
                  },
                  {
                    "COLUMN_NAME":"RECEIVER_STREET",
                    "COLUMN_TYPE":1,
                    "LABLE_VALUES":[
                      {
                        "LABLE":item.STREET.val,
                        "VALUE":item.STREET.val
                      }
                    ]
                  },
                  {
                    "COLUMN_NAME":"RECEIVER_ADDRESS",
                    "COLUMN_TYPE":1,
                    "LABLE_VALUES":[
                      {
                        "LABLE":item.ADDRESS.val,
                        "VALUE":item.ADDRESS.val
                      }
                    ]
                  },
                ]
                setFormItemValue(ChangeList,this.$parent.$parent.$parent)
              }
            }
          })

          this.$emit('change', val);
              this.currentIndex=0
            }
          })
          // console.log(2222,val)
          // console.log(this.shopDataAll)
          // 
        },
        // 清空选中项
        clearDataShop(){
          let ChangeList = [
                {
                  "COLUMN_NAME":"RECEIVER_PROVINCE",
                  "COLUMN_TYPE":0,
                  "LABLE_VALUES":[
                    {
                      "LABLE":'',
                      "VALUE":''
                    }
                  ]
                },
                {
                  "COLUMN_NAME":"RECEIVER_CITY",
                  "COLUMN_TYPE":0,
                  "LABLE_VALUES":[
                    {
                      "LABLE":'',
                      "VALUE":''
                    }
                  ]
                },
                {
                  "COLUMN_NAME":"RECEIVER_AREA",
                  "COLUMN_TYPE":0,
                  "LABLE_VALUES":[
                    {
                      "LABLE":'',
                      "VALUE":''
                    }
                  ]
                },
                {
                  "COLUMN_NAME":"RECEIVER_STREET",
                  "COLUMN_TYPE":1,
                  "LABLE_VALUES":[
                    {
                      "LABLE":'',
                      "VALUE":''
                    }
                  ]
                },
                {
                  "COLUMN_NAME":"RECEIVER_ADDRESS",
                  "COLUMN_TYPE":1,
                  "LABLE_VALUES":[
                    {
                      "LABLE":'',
                      "VALUE":''
                    }
                  ]
                },
              ]

              // setFormItemValue(ChangeList,this.$parent.$parent.$parent)
        },
        // 单选下拉分页改变
        changePageShop(val){
          this.currentIndex=(val-1)*10
          this.searchShopData()
        },
        // 单选下拉模糊搜索数据
        InputValueChangeShop(val){
            let virtual={};
            if(window.location.href.indexOf('T_RET_PUR') > -1){
              virtual={
                ak: val,
                colid: 205925,
                fixedcolumns: {}
              }
            }else if(window.location.href.indexOf('T_SALE') > -1) {
              virtual={
                ak: val,
                colid: 206378,
                fixedcolumns: {}
              }
            }else if(window.location.href.indexOf('T_V_WMSALLOCATEBILL') > -1) {
              virtual={
                ak: val,
                colid: 198106,
                fixedcolumns: {}
              }
            }
            network.post('/p/cs/fuzzyquerybyak',urlSearchParams(virtual)).then((res) => {
                if(res.data.code==0){
                    this.AutoDataShop=res.data.data
                }
            })
        },
    },
  
    mounted() {
      this.defaultSelected = this.options.valuedata?[{
        ID: this.options.refobjid,
        Label: this.options.valuedata
      }]:[];
      // fix:31480946 销售单明细复制后首次打开参数丢失
      this.$nextTick(()=> {
        this.$emit('change', this.defaultSelected);
      })
    }
  };
  </script>
  
  <style lang="scss" >
  .supplierToMessage .ark-input-group-prepend .ark-select-selection,.supplierToMessage .ark-input-group-append .ark-select-selection {
    border: 1px solid #d8d8d8 !important;
    margin-top: -1px;
  }
  </style>
