<template>
  <div>
    <el-tabs type="border-card">
      <el-tab-pane label="合同信息">
        <el-card :body-style="cardBodyStyle">
          <el-form :model="query" label-width="auto" label-position="right">
            <el-row :gutter="10" style="margin-bottom: 5px;">
              <el-col :span="5">
                <el-form-item label="客户名称" style="margin-bottom: 0px;">
                  <el-input v-model="query.supplierNm" placeholder="" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="合同号" style="margin-bottom: 0px;">
                  <el-input v-model="query.contrNo" placeholder="" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="对方合同号" style="margin-bottom: 0px;">
                  <el-input v-model="query.khContrNo" placeholder="" clearable></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <el-card :body-style="cardBodyStyle">
          <el-button type="primary" @click="search" plain>查询</el-button>
<!--          <el-button type="success" @click="insertDelv" plain>生成发货指示</el-button>-->
        </el-card>
<!--        <ButtonBar @search="search"></ButtonBar>-->

        <el-card :body-style="cardBodyStyle">
          <el-table :data="tableData" border highlight-current-row height="300"
                    @selection-change="handleSelectionChange">
<!--            <el-table-column type="selection" fixed></el-table-column>-->
            <el-table-column type="index" label="序号" align="center" width="60">
              <template #default="scope">
                <span>{{ scope.$index + (currentPage - 1) * pageSize + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column v-if="true" prop="supplierNm" label="客户名称" width="220" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="khContrNo" label="对方合同号" header-align="center" align="center"
                             width="160"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="specType" label="品位" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="contrNm" label="合同名称" width="160" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="itemNm" label="物料名称" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="qtyLine" label="还需数量" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="taxPriceBill" label="含税单价" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
<!--              <template #default="scope">-->
<!--                {{ifPriceDcnMth(scope.row, scope.$index)}}-->
<!--              </template>-->
            </el-table-column>
            <el-table-column v-if="true" prop="uomLine" label="计量单位" header-align="center" align="center"
                             width="140"
                             show-overflow-tooltip>
              <template #default="scope">
                <Selector :value="scope.row.uomLine" :optionList="dictList.MEASURING_UNIT">
                </Selector>
              </template>
            </el-table-column>

            <el-table-column v-if="true" prop="conLine" label="合同行号" width="160" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>

            <el-table-column v-if="true" prop="contrNo" label="合同号" width="160" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>

            <el-table-column v-if="true" prop="itemCd" label="物料编码" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="false" prop="stlGrd" label="钢种" header-align="center" align="center" width="100"
                             show-overflow-tooltip>
            </el-table-column>
            
            <el-table-column v-if="true" prop="taxRate" label="税率(%)" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="false" prop="houdu" label="厚度" header-align="center" align="center" width="80"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="false" prop="dingchi" label="定尺" header-align="center" align="center" width="80"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="storeCd" label="库房号" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="storeNm" label="库房名称" header-align="center" align="center"
                             width="220"
                             fixed="right"
                             show-overflow-tooltip>
              <template #default="scope" @click="blurKf(scope.$index)">
                <div :class="{'highlighted-cell': isCellHighlighted(scope.row)}"  >

                  <Popup_sto_config_kf_bx :key="getTableDataTime" :if-item-kf="true" :item-cd="scope.row.itemCd"
                      @receive-from-child="(val) => returnKf(scope.$index,val)"></Popup_sto_config_kf_bx>

                </div>
              </template>
            </el-table-column>
            <el-table-column v-if="true" prop="qty" label="发货数量" header-align="center" align="center" fixed="right"
                             width="220"
                             show-overflow-tooltip>
              <template #default="scope">
                <div :class="{'highlighted-cell': isCellHighlightedQty(scope.row)}"  >
                  <el-input-number v-model="scope.row.qty" min="0.01" value="0" :step="0.1" :precision="2" style="width: 100%"
                                   controls-position="right" @change="qtyChange(scope.$index)"
                                   step-strictly/>
                </div>
              </template>
            </el-table-column>
            <el-table-column label="操作" header-align="center" align="center" width="200px" fixed="right">
              <template #default="scope">
                <div class="flex flex-wrap items-center">
                  <el-dropdown>
                    <el-button type="success" plain>
                      生成发货指示<el-icon class="el-icon--right"><arrow-down /></el-icon>
                    </el-button>
                    <template #dropdown>
                      <el-dropdown-menu>
                        <el-dropdown-item @click="insertDelv(scope.row , 'A')">现汇</el-dropdown-item>
                        <el-dropdown-item @click="insertDelv(scope.row , 'B')">承兑</el-dropdown-item>
                      </el-dropdown-menu>
                    </template>
                  </el-dropdown>
                </div>
<!--                <el-button type="success" @click="insertDelv(scope.row)" plain>生成发货指示</el-button>-->
              </template>
            </el-table-column>
          </el-table>
        </el-card>
        <el-card :body-style="cardBodyStyle">
          <el-pagination :current-page="currentPage" :page-size="pageSize" :page-sizes="pageSizes" :total="totalRows"
                         background="background" layout="total, sizes, prev, pager, next, jumper"
                         @size-change="handleSizeChange"
                         @current-change="handleCurrentChange">
          </el-pagination>
        </el-card>
      </el-tab-pane>
    </el-tabs>
    <el-tabs type="border-card">
      <el-tab-pane label="发货指示">
        <el-card :body-style="cardBodyStyle">
          <el-form :model="queryFHZS" label-width="auto" label-position="right">
            <el-row :gutter="10" style="margin-bottom: 5px;">
              <el-col :span="5">
                <el-form-item label="发货号" style="margin-bottom: 0px;margin-left: 28px;">
                  <el-input v-model="queryFHZS.dordNo" placeholder="" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="合同号" style="margin-bottom: 0px;margin-left: 28px;">
                  <el-input v-model="queryFHZS.contrNo" placeholder="" clearable></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
        <el-card :body-style="cardBodyStyle">
          <el-button type="primary" @click="searchFHZS" plain>查询</el-button>
          <el-button type="success" @click="confirmDelv" plain>确认</el-button>
          <el-button type="success" @click="successDelv" plain>发货完成</el-button>
          <el-button type="danger" @click="deleteDelv" plain>删除</el-button>
        </el-card>

        <el-card :body-style="cardBodyStyle">
          <el-table :data="tableDataFHZS" border highlight-current-row height="300"
                    @selection-change="handleSelectionChangeDelv">
            <el-table-column type="selection" fixed></el-table-column>
            <el-table-column type="index" label="序号" align="center" width="60">
              <template #default="scope">
                <span>{{ scope.$index + (currentPageFHZS - 1) * pageSizeFHZS + 1 }}</span>
              </template>
            </el-table-column>
            <el-table-column v-if="true" prop="dordNo" label="发货指令号" width="200" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="contrNo" label="合同号" width="130" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="contrLineNo" label="合同行号" width="130" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="dordSts" label="指令状态" width="140" header-align="center"
                             align="center" show-overflow-tooltip>
              <template #default="scope">
                <el-tag type="info" v-if="scope.row.dordSts == 'A'">{{ formatterStr( "status",scope.row.dordSts) }}</el-tag>
                <el-tag v-else-if="scope.row.dordSts == 'B'">{{ formatterStr( "status",scope.row.dordSts) }}</el-tag>
                <el-tag type="success"  v-else>{{ formatterStr( "status",scope.row.dordSts) }}</el-tag>
<!--                <Selector :value="scope.row.dordSts" :optionList="dictList.SAL_DELV_STS">-->
<!--                </Selector>-->
              </template>
            </el-table-column>
            <el-table-column v-if="true" prop="itemNm" label="物料名称" header-align="center" align="center" width="120"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="dordPrice" label="单价" header-align="center" align="center" width="120"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="qty" label="数量" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="dordAmt" label="金额" header-align="center" align="center" width="120"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="actQty" label="实际发货数量" header-align="center" align="center" width="120"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="addTime" label="指令日期" header-align="center" align="center"
                             width="200"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="tradePartnerName" label="客户名称" header-align="center" align="center"
                             width="150"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="itemCd" label="产品编号" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="khContrNo" label="对方合同号" header-align="center" align="center"
                             width="120"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="false" prop="stlGrd" label="钢种" header-align="center" align="center" width="100"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="specType" label="品位" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
            </el-table-column>

            <el-table-column v-if="true" prop="uom" label="单位" header-align="center" align="center" width="140"  :formatter="formatter"
                             show-overflow-tooltip>
            </el-table-column>

            <el-table-column v-if="true" prop="storeCd" label="库房" header-align="center" align="center" width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="storeNm" label="库房名称" header-align="center" align="center"
                             width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="payMth" label="付款方式" header-align="center" align="center" :formatter="formatter"
                             width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <!--        <el-table-column label="操作" header-align="center" align="center" width="120px" fixed="right">-->
            <!--          <template #default="scope">-->

            <!--          </template>-->
            <!--        </el-table-column>-->
          </el-table>
        </el-card>
        <el-card :body-style="cardBodyStyle">
          <el-pagination :current-page="currentPageFHZS" :page-size="pageSizeFHZS" :page-sizes="pageSizesFHZS"
                         :total="totalRowsFHZS"
                         background="background" layout="total, sizes, prev, pager, next, jumper"
                         @size-change="handleSizeChangeFHZS"
                         @current-change="handleCurrentChangeFHZS">
          </el-pagination>
        </el-card>
      </el-tab-pane>
    </el-tabs>


  </div>
</template>

<script setup>
import {
  ref,
  reactive,
  onMounted,
  getCurrentInstance
} from 'vue'
import {Search} from '@element-plus/icons-vue'
import dayjs from 'dayjs'
import style from '@/tools/style'
import SearchBar from '@/components/SearchBar.vue';
import PopupMenuTree from '@/components/PopupMenuTree.vue'
import ButtonBar from "@/components/ButtonBar.vue";
import ButtonBox from "@/components/ButtonBox.vue";
import SearchBox from "@/components/SearchBox.vue";
import TableBox from "@/components/TableBox.vue";
import Popup_sto_config_kf_bx from "@/views/sto/components/Popup_sto_config_kf_bx.vue";
import Selector from "@/views/sal/components/Selector_text.vue";

const {
  proxy
} = getCurrentInstance()


const cardBodyStyle = ref(style.card)

const tableStatus = reactive({
  tableRowEditIndex: undefined,
  tableColumnEditIndex: undefined,
})


const SearchBoxRef = ref(null)
const searchBarRef = ref(null)
const pageType = ref(true)
const formItem = ref([
  {
    prop: 'birthDate',
    type: 'input',
    label: '订单号'
  },
  {
    prop: 'age',
    type: 'input',
    label: '客户名称',
    searchButton: true,
    searchCallback: 'showPop'
  }

])

const searchForm = (val) => {
  query.value = searchBarRef.value.form

}

/**
 *   查询
 */

const query = ref({
  supplierNm: '',
  contrNo: '',
  khContrNo: '',
})
const queryFHZS = ref({
  birthDate: '',
  type: '',
  code: '',
  name: '',
  sex: '',
  contractType: '',
  contractFlag: '',
  addTime: ['', '']
})

const reset = () => {
  Object.keys(query.value).map(key => {
    query.value[key] = ''
  })
}

const tableData = ref([])
const tableDataFHZS = ref([])

const tableDataWl = ref([])
const tableDataTwo = ref([])

const TableBoxRef = ref(null)
const handleSearch = (val) => {
  // query.value = searchBarRef.value.form

  TableBoxRef.value.search()
}
const getTableData = () => {
  getTableDataTime.value = new Date().getTime()
  var url = 'cot/Head/queryHeadAndLineByXsht'
  var param = {
    page: currentPage.value,
    rows: pageSize.value,
    params: JSON.stringify(query.value)
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      for(var i = 0; i < response.data.data.records.length ; i++){
        if (Number(response.data.data.records[i].taxPriceBill) == 0){
          response.data.data.records[i].taxPriceBill = response.data.data.records[i].taxPriceCash
        }
      }
      tableData.value = response.data.data.records
      totalRows.value = response.data.data.total
      console.log(response.data.data.records)
      for(var i = 0; i < response.data.data.records.length ; i++){
        if (response.data.data.records[i].priceDcnMth != '10'){
          getNewPrice(response.data.data.records[i].itemCd , response.data.data.records[i].specType,i)

        }
        getDelvQty(response.data.data.records[i].contrNo,response.data.data.records[i].conLine ,response.data.data.records[i].qtyLine ,i)
      }

      tableData.value.forEach((item) => {
        console.log(item)
        params.value.contrNo = item.contrNo
        params.value.qtyLine = item.qtyLine
        params.value.itemCd = item.itemCd
        getComputeCount(params.value).then(res => {
          console.log(res)
          item.qty = res
        })
      })
      console.log(tableData.value)
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}
const getTableDataFHZS = () => {
  var url = 'sal/Delv/query'
  var param = {
    page: currentPageFHZS.value,
    rows: pageSizeFHZS.value,
    params: JSON.stringify(queryFHZS.value)
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {

      tableDataFHZS.value = response.data.data.records
      totalRowsFHZS.value = response.data.data.total
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}
/**
 *   查询
 */
/**
 * 分页
 */
const currentPage = ref(1)
const currentPageFHZS = ref(1)
const totalRows = ref(0)
const totalRowsFHZS = ref(0)
const pageSizes = ref([5, 10, 20, 30, 40])
const pageSizesFHZS = ref([5, 10, 20, 30, 40])
const pageSize = ref(5)
const pageSizeFHZS = ref(5)
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}
const handleSizeChangeFHZS = (val) => {

  pageSizeFHZS.value = val
  getTableDataFHZS()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  getTableData()
}
const handleCurrentChangeFHZS = (val) => {
  currentPageFHZS.value = val
  getTableDataFHZS()
}
/**
 * 分页
 */


const PopupMenuTreeRef = ref(null)
const showPop = () => {

  PopupMenuTreeRef.value.show()
}
/**
 * 新增表单form
 */
const insertForm = reactive({
  id: '',
  ddh: '',
  ordTy: '',
  supplierNm: '',
  salTy: '',
  payMth: '',
  salMth: '',
  kczz: '',
  bz: '',
  salOrdNo: '',
  contrNo: '',
  htrq: '',
  delvDt: '',
  invMth: '',
  delvPlace: '',
  jhfs: '',
  scfs: '',
  ysfs: '',
  hthl: '',
  myfs: '',
  shhl: '',
  ckgj: '',
  qsd: '',
  mdd: '',
  djysbl: '',
  dkysbl: '',
  remark: '',
  khContrNo: '',
  hzsl: '',
  shxx: '',
  jeht: '',
  hzje: '',
  zlsm: '',
})

// 新增订单
const insertDialogShow = ref(false)
const insertDialog = () => {
  insertDialogShow.value = true
}
const insertDialogSave = () => {
  insertDialogShow.value = false
  ElMessage({
    message: '模拟成功！',
    type: 'success',
  })
  var url = 'framework/SysUser/saveAll'
  // var param = {
  //   json: JSON.stringify(user.value),
  //   roles: JSON.stringify(yesRoleTableData.value),
  //   posts: JSON.stringify(yesPostTableData.value),
  //   departments: JSON.stringify(checkedKeys.value)
  // }
  // proxy.$axios.post(url, param).then(response => {
  //   if (response.data.code == 0) {
  //     ElMessage({
  //       message: '保存成功',
  //       type: 'success',
  //     })
  //     getTableData()
  //     dialogVisible.value = false
  //   } else {
  //     // ElMessage.error('操作失败！' + response.data.msg)
  //     ElMessage({
  //       type: 'error',
  //       message: '操作失败' + response.data.msg
  //     })
  //   }
  // });
}

// 生成调价申请编码
const createNm = () => {
  ElMessage({
    message: '模拟生成调价申请编码成功！',
    type: 'success',
  })

}
// 提交审批
const submit = () => {
  insertDialogShow.value = false
  ElMessage({
    message: '模拟提交审批成功！',
    type: 'success',
  })

}
// 校验
const rules = reactive({
  contrNo: [
    {required: true, message: 'Please input Activity name', trigger: 'blur'},
  ],
  ordTy: [
    {required: true, message: 'Please input Activity name', trigger: 'blur'},
  ],
  supplierNm: [
    {required: true, message: 'Please input Activity name', trigger: 'blur'},
  ],
  salTy: [
    {required: true, message: 'Please input Activity name', trigger: 'blur'},
  ],
  payMth: [
    {required: true, message: 'Please input Activity name', trigger: 'blur'},
  ],
  salMth: [
    {required: true, message: 'Please input Activity name', trigger: 'blur'},
  ],
  kczz: [
    {required: true, message: 'Please input Activity name', trigger: 'blur'},
  ],
  bz: [
    {required: true, message: 'Please input Activity name', trigger: 'blur'},
  ],
})

const otherQuery = () => {

}

// 首-列表 编辑
const handleEdit = (row) => {
  insertDialogShow.value = true

}

// 首-列表 编辑
const handleDelete = (row) => {
  ElMessageBox.confirm(
      '确定删除本条记录?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  )
      .then(() => {
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消',
        })
      })
}

// 调价申请物料 -add row、
const onAddItemWl = () => {
  const now = new Date()
  now.setDate(now.getDate() + 1)
  tableDataWl.value.push({
    id: now.getTime(),
    isEdit: false,
    itemCd: "",
    itemNm: "",
    startDt: "",
    priceCash: "",
  })
}
const tableRowInputRef = ref(null)
const rowEdit = (scope) => {
  scope.isEdit = true
}
const rowEditSave = (scope) => {
  scope.isEdit = false
}

// 表格中input取消焦点,select变化
const onInputTableBlur = (scope) => {

  tableStatus.tableRowEditIndex = undefined
  tableStatus.tableColumnEditIndex = undefined
  const id = scope.row.id
  const key = scope.column.property
  const value = scope.row[key]
  // 更新给后端的数据接口
  // 提交数据........
}

// 物料 行复制
const rowCopy = (row) => {


  const now = new Date()
  now.setDate(now.getDate() + 1)
  tableDataWl.value.push({
    id: now.getTime(),
    isEdit: false,
    itemCd: row.itemCd,
    itemNm: row.itemNm,
    startDt: row.startDt,
    priceCash: row.priceCash,
  })
}

// 物料 行删除
const rowRemove = (index) => {
  tableDataWl.value.splice(index, 1)
}

const tableEdit = (row, column, cell, event) => {

  if (column.property === 'itemCd') {
    this.tableDataWl.forEach(item => {
      if (item.id === row.id) {
        item.xEdit = true
      }
    })
  }

}
const PopupRefThree4 = ref(null)
const time = ref('')
const getTime = () => {

  time.value = new Date().getTime()

}

const ret = (val) => {

}
// 选中行
const checkRow = ref([])
const handleSelectionChange = (val) => {
  checkRow.value = val
}
// 选中行
const checkRowDelv = ref([])
const handleSelectionChangeDelv = (val) => {
  checkRowDelv.value = val
}
const insertDelv = (row ,val) => {

  if (row.storeCd == null || row.storeId == null || row.storeCd == null || row.storeId == '') {
    ElMessage({
      type: 'warning',
      message: '请选择库房名称!'
    })
    row.status = 'warning'
    return;
  }
  if (row.qty == null || row.qty == '' || row.qty == 0) {
    ElMessage({
      type: 'warning',
      message: '发货数量不允许为空，且不允许为0!'
    })
    row.statusQty = 'warning'

    return;
  }
  var list = []
  list.push(row)
  var url = 'sal/Delv/saveAll'
  var param = {
    json: JSON.stringify(list),
    payMth: val,
  }

  proxy.$axios.post(url, param).then(response => {
    if (response.data.code == 0) {
      ElMessage({
        message: '生成发货指示成功',
        type: 'success',
      })
      getTableDataTime.value = new Date().getTime()
      getTableData()
      getTableDataFHZS()

    } else {
      // ElMessage.error('操作失败！' + response.data.msg)
      ElMessage({
        type: 'error',
        message: '操作失败' + response.data.msg
      })
    }
  });

}
const confirmDelv = () => {
  if (checkRowDelv.value.length < 1) {
    ElMessage({
      type: 'warning',
      message: '请选择要确认的发货指示数据!'
    })
    return
  }
  for (var i = 0; i < checkRowDelv.value.length; i++) {
    if (checkRowDelv.value[i].dordSts != 'A') {
      ElMessage({
        type: 'warning',
        message: '勾选的第 ' + (i + 1) + '条数据，非生成状态，无法确认，请调整！'
      })
      return
    }
  }

  var ids = []
  checkRowDelv.value.forEach((item) => {
    ids.push(item.id)
  })
  var url = 'sal/Delv/confirmDelv'
  var param = {
    json: JSON.stringify(ids),
  }

  proxy.$axios.post(url, param).then(response => {
    if (response.data.code == 0) {
      ElMessage({
        message: '发货指示确认成功',
        type: 'success',
      })
      getTableDataFHZS()

    } else {
      // ElMessage.error('操作失败！' + response.data.msg)
      ElMessage({
        type: 'error',
        message: '操作失败' + response.data.msg
      })
    }
  });
}
const successDelv = () => {
  if (checkRowDelv.value.length < 1) {
    ElMessage({
      type: 'warning',
      message: '请选择要发货完成的数据!'
    })
    return
  }
  for (var i = 0; i < checkRowDelv.value.length; i++) {
    if (checkRowDelv.value[i].dordSts != 'B') {
      ElMessage({
        type: 'warning',
        message: '勾选的第 ' + (i + 1) + '条数据，非确认状态，无法发货完成，请调整！'
      })
      return
    }
  }

  var ids = []
  checkRowDelv.value.forEach((item) => {
    ids.push(item.id)
  })
  var url = 'sal/Delv/completeDelv'
  var param = {
    json: JSON.stringify(ids),
  }

  proxy.$axios.post(url, param).then(response => {
    if (response.data.code == 0) {
      ElMessage({
        message: '发货完成！',
        type: 'success',
      })
      getTableDataFHZS()

    } else {
      // ElMessage.error('操作失败！' + response.data.msg)
      ElMessage({
        type: 'error',
        message: '操作失败' + response.data.msg
      })
    }
  });
}
const deleteDelv = () => {
  if (checkRowDelv.value.length < 1) {
    ElMessage({
      type: 'warning',
      message: '请选择要删除的发货指示数据!'
    })
    return
  }
  for (var i = 0; i < checkRowDelv.value.length; i++) {
    if (checkRowDelv.value[i].dordSts != 'A') {
      ElMessage({
        type: 'warning',
        message: '勾选的第 ' + (i + 1) + '条数据，非生成状态，无法删除！'
      })
      return
    }
  }

  var ids = []
  checkRowDelv.value.forEach((item) => {
    ids.push(item.id)
  })
  var url = 'sal/Delv/removeBatch'
  var param = {
    ids: JSON.stringify(ids),
  }
  ElMessageBox.confirm(
      '确定删除本条记录?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  )
      .then(() => {
        proxy.$axios.post(url, param).then(response => {
          if (response.data.code == 0) {
            ElMessage({
              message: '发货指示删除成功',
              type: 'success',
            })
            getTableDataFHZS()

          } else {
            // ElMessage.error('操作失败！' + response.data.msg)
            ElMessage({
              type: 'error',
              message: '操作失败' + response.data.msg
            })
          }
        });
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消',
        })
      })

}

const returnKf = (index, val) => {


  tableData.value[index].storeCd = val.storeCd
  tableData.value[index].storeId = val.id
  tableData.value[index].storeNm = val.storeNm
  tableData.value[index].status = 'success'
}


const qtyChange = (index) => {
  tableData.value[index].statusQty = 'success'
}


const isCellHighlighted = (row) => {

  return row.status === 'warning'; // 根据状态高亮单元格
};
const isCellHighlightedQty = (row) => {

  return row.statusQty === 'warning'; // 根据状态高亮单元格
};

const search = () => {
  getTableData()
}
const searchFHZS = () => {
  getTableDataFHZS()
}
const dictList = ref({})
const dictData = () => {
  var url = 'framework/Common/querySelectorList'
  var param = {
    type: JSON.stringify(['SAL_DELV_STS' , 'MEASURING_UNIT' , 'COM_PAY_MTH'])
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      dictList.value = response.data.data
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}

// 获取最新定价
const getNewPrice = (val  , specType, index) => {

  var url = 'sal/UpriceLine/selectNewDateByItemCd'
  var param = {
    itemCd: val,
    specType: specType,
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      tableData.value[index].taxPriceBill =  response.data.data.priceBill


    }
  })
}
// 获取 还需发货数量
const getDelvQty = (contrNo,contrLineNo ,qtyLine, index) => {

  var url = 'sal/Delv/queryContrLineDelvQty'
  var obj = {
    contrNo: contrNo,
    contrLineNo: contrLineNo,
    qtyLine: qtyLine,
  }
  var param = {
    params: JSON.stringify(obj)
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      tableData.value[index].qtyLine =  response.data.data


    }
  })
}

const formatterStr = (col,value) => {
  if (!value || value == null || value == '') {
    return value
  }
  switch (col) {
    case 'status':
      value = dictList.value && Array.isArray(dictList.value.SAL_DELV_STS)
          ? dictList.value.SAL_DELV_STS.find(item => item.dicCode == value)?.dicName
          : undefined
      break
  }
  return value

}

const formatter = (row, column, cellValue, index) => {
  if (!cellValue || cellValue == null || cellValue == '') {
    return cellValue
  }
  switch (column.property) {
    case 'uom':
      cellValue = dictList.value && Array.isArray(dictList.value.MEASURING_UNIT)
          ? dictList.value.MEASURING_UNIT.find(item => item.dicCode == cellValue)?.dicName
          : undefined
      break;
    case 'payMth':
      cellValue = dictList.value && Array.isArray(dictList.value.COM_PAY_MTH)
          ? dictList.value.COM_PAY_MTH.find(item => item.dicCode == cellValue)?.dicName
          : undefined
      break;
  }
  return cellValue
}

const getTableDataTime = ref('')
onMounted(() => {
  getTableData()
  getTableDataFHZS()
  dictData()

})
const params = ref({
  contrNo: '',
  qtyLine:'',
  itemCd:''
})
const getComputeCount = () => {
  return new Promise((resolve, reject) => {
    var url = 'sal/Delv/computeCount'
    var param = {
      params: JSON.stringify(params.value)
    }
    proxy.$axios.get(url, param).then(response => {
      if (response.data.code == '0') {
        console.log(response.data.data)
        resolve(response.data.data)
      }
    })
  })
}
</script>

<style scoped>
::v-deep .diaClass .el-form-item {
  width: 100%
}

::v-deep .el-dialog__body {
  background-color: #F2F6FC;
}

.el-date-editor--date {
  width: 100%;
}

.highlighted-cell {
  border: 1px solid red;
  border-radius: 5px;
  padding: 1px;
  color: #67c23a;
}

</style>
