<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;margin-left: 14px">
                  <el-input v-model="query.tradePartnerName" placeholder="" clearable>
                    <!--                <template #append>-->
                    <!--                  <el-button :icon="Search"/>-->
                    <!--                </template>-->
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="收款编号" style="margin-bottom: 0px;margin-left: 14px">
                  <el-input v-model="query.prepaymentNo" placeholder="请输入收款编号" clearable></el-input>
                </el-form-item>

              </el-col>
              <el-col :span="5">
                <el-form-item label="收款日期" style="margin-bottom: 0px;margin-left: 14px">
                  <el-date-picker
                      style="width: 100%"
                      v-model="query.prepaymentDt"
                      type="daterange"
                      range-separator="-"
                      @select="aaa"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      value-format="YYYY-MM-DD"
                  />
                </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="insertDialog" plain>新增</el-button>-->
<!--          <el-button type="danger" @click="submit" plain>提交</el-button>-->
<!--        </el-card>-->
        <ButtonBar @search="search"  @insertDialog="insertDialog" @submit="submit"></ButtonBar>


        <el-card :body-style="cardBodyStyle">
          <el-table :data="tableData" border highlight-current-row height="380"
                    @selection-change="handleSelectionChange" @row-click="cellHover">
            <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="prepaymentNo" label="收款编号" width="150" header-align="center"
                             align="center" show-overflow-tooltip>
              <template #default="scope">
                <el-button @click="handleInfo(scope.row)" type="primary" link>{{ scope.row.prepaymentNo }}</el-button>
              </template>
            </el-table-column>
            <el-table-column v-if="true" prop="tradePartnerCode" label="客户编码" width="150" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="tradePartnerName" label="客户名称" width="150" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="prepaymentAmt" label="收款金额" width="150" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="workModel.flowStatusName" label="流程状态" width="120" header-align="center" align="center"
                             show-overflow-tooltip>
              <template #default="{ row }">
                <div>
                  <el-tag v-if="row.workId !== undefined && row.workModel.flowStatus === '110'">{{ row.workModel.flowStatusName }}</el-tag>
                  <el-tag type="success" v-else-if="row.workId !== undefined && row.workModel.flowStatus === '170'">{{ row.workModel.flowStatusName }}</el-tag>
                  <el-tag type="danger" v-else-if="row.workId !== undefined && (row.workModel.flowStatus === '130' || row.workModel.flowStatus === '180' || row.workModel.flowStatus === '190')">{{ row.workModel.flowStatusName }}</el-tag>
                  <el-tag type="warning" v-else>'---'</el-tag>
                </div>

              </template>
            </el-table-column>
            <el-table-column v-if="true" prop="workModel.flowNode" label="当前节点" width="120" header-align="center" align="center"
                             show-overflow-tooltip>
              <template #default="{ row }">
                <div>
                  <el-tag v-if="row.workId !== undefined && row.workModel.flowStatus === '110'">{{ row.workModel.flowNode }}</el-tag>
                  <el-tag type="success" v-else-if="row.workId !== undefined && row.workModel.flowStatus === '170'">{{ row.workModel.flowNode }}</el-tag>
                  <el-tag type="danger" v-else-if="row.workId !== undefined && (row.workModel.flowStatus === '130' || row.workModel.flowStatus === '180' || row.workModel.flowStatus === '190')">{{ row.workModel.flowNode }}</el-tag>
                  <el-tag type="warning" v-else>'---'</el-tag>
                </div>

              </template>
            </el-table-column>
            <el-table-column v-if="true" prop="prepaymentDt" label="收款日期" width="130" header-align="center"
                             align="center" show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="payMth" label="收款类型" width="140" header-align="center"
                             align="center" show-overflow-tooltip>
              <template #default="scope">
                {{ dic(scope.row.payMth) }}
              </template>
            </el-table-column>
            <el-table-column v-if="true" prop="billNoteNo" label="承兑编号" header-align="center" align="center"
                             width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="billNoteNum" label="票据张数" header-align="center" align="center"
                             width="150"
                             show-overflow-tooltip>
            </el-table-column>

            <el-table-column v-if="true" prop="manualFlag" label="是否手动录入" header-align="center" align="center"
                             width="140"
                             show-overflow-tooltip>
              <template #default="scope">
                {{ dicIF(scope.row.manualFlag) }}
              </template>

            </el-table-column>

            <el-table-column v-if="true" prop="writeOffNo" label="被核销的收款编号" header-align="center" align="center"
                             width="180"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="writeOffUserNm" label="核销人员" header-align="center" align="center"
                             width="120"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="writeOffDt" label="核销日期" header-align="center" align="center"
                             width="120"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="errMsg" label="自动分配时的报错信息" header-align="center" align="center"
                             width="180"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="remarks" label="收款备注信息" header-align="center" align="center"
                             width="140"
                             show-overflow-tooltip>
            </el-table-column>
            <el-table-column v-if="true" prop="writeOffFlag" label="是否核销" header-align="center" align="center"
                             width="120"
                             show-overflow-tooltip>
              <template #default="scope">
                {{ dicIF(scope.row.writeOffFlag) }}
              </template>
            </el-table-column>
            <el-table-column v-if="true" prop="addName" label="添加人" header-align="center" align="center"
                             width="140"
                             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="processInstanceStatusStr" label="状态" header-align="center"-->
<!--                             align="center"-->
<!--                             width="100"-->
<!--                             fixed="right"-->
<!--                             show-overflow-tooltip>-->
<!--            </el-table-column>-->
            <el-table-column label="操作" header-align="center" align="center" width="300px" fixed="right">
              <template #default="scope">
<!--                <div v-if="scope.row.workId == '' && scope.row.trackId == ''">-->
<!--                  <el-button plain type="success" @click="handleSubmit(scope.row)" >提交</el-button>-->
<!--                  <el-button plain type="warning" @click="handleEdit(scope.row)">编辑</el-button>-->
<!--                  <el-button plain type="danger" @click="handleDelete(scope.row )">删除</el-button>-->
<!--                </div>-->
                <el-button plain type="warning" @click="handleEdit(scope.row)" v-show="scope.row.workId === '' || scope.row.workModel.opType === '1'">编辑
                </el-button>
                <el-button plain type="danger" @click="handleDelete(scope.row )" v-show="scope.row.workId === '' || scope.row.workModel.opType === '1'">删除
                </el-button>
                <el-button plain type="success" @click="handleSubmit(scope.row ) "v-show="scope.row.workId === '' || scope.row.workModel.opType === '1'">发起
                </el-button>
                <el-button plain type="primary" @click="handleDetail(scope.row )" v-show="scope.row.workId !== ''">详情</el-button>
                <el-button plain type="info" @click="handleCancel(scope.row )" v-show="scope.row.workId !== '' && scope.row.workModel.opType !== '1' && scope.row.workModel.flowStatus !== '170'">撤回</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="附件信息">
        <Attachment ref="AttachmentRefManual" :fId="fId" :uploadButton="uploadButton"></Attachment>
      </el-tab-pane>
    </el-tabs>


    <el-dialog class="diaClass" :close-on-click-modal="false" draggable title="新增" v-model="insertDialogShow"
               width="45%"
               top="5vh">
      <el-card class="box-card">
        <el-form ref="formRef" :model="insertForm" :rules="rules" label-width="110" label-position="right"
                 :inline="true">
          <el-row>
            <el-col :span="12">
              <el-form-item label="收款编号" prop="prepaymentNo">
                <el-input v-model="insertForm.prepaymentNo" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户名称" prop="tradePartnerName">
                <el-input v-model="insertForm.tradePartnerName" readonly>
                  <template #append>
                    <el-button :icon="Search" @click="ksDialog"/>
                  </template>
                </el-input>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="收款金额（元）" prop="prepaymentAmt">
                <el-input-number style="width: 100%" min="0" :precision="2" :step="0.1" controls-position="right"
                                 v-model="insertForm.prepaymentAmt"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="收款日期">
                <el-date-picker
                    style="width: 100%"
                    v-model="insertForm.prepaymentDt"
                    type="date"
                    placeholder=""
                    value-format="YYYY-MM-DD"
                    size="default"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="收款类型" prop="payMth">
                <Selector_component :option-list="dictList.COM_PAY_MTH" :value="insertForm.payMth"
                                    @change="collectionTySelect"></Selector_component>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="承兑编号">
                <el-input v-model="insertForm.billNoteNo"></el-input>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="票据张数">
                <el-input-number style="width: 100%" min="0" :step="1" controls-position="right"
                                 v-model="insertForm.billNoteNum"></el-input-number>

              </el-form-item>
            </el-col>
            <!--            <el-col :span="12">-->
            <!--              <el-form-item label="收款分配标志">-->
            <!--                <el-input v-model="insertForm.prepDistributeSign"></el-input>-->
            <!--              </el-form-item>-->
            <!--            </el-col>-->
          </el-row>

          <el-row>

            <el-col :span="24" class="aaa">
              <el-form-item label="收款备注信息">
                <el-input
                    v-model="insertForm.remarks"
                    :autosize="{ minRows: 2, maxRows: 4 }"
                    type="textarea"
                    placeholder=""
                />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </el-card>


      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" plain @click="insertDialogSave(formRef)">保存</el-button>
          <el-button type="danger" plain @click="insertDialogShow = false">取消</el-button>
        </div>
      </template>


    </el-dialog>
    <el-dialog class="diaClass" :close-on-click-modal="false" draggable title="查看" v-model="detailsDialogShow"
               width="45%"
               top="5vh">
      <el-card class="box-card">
        <el-form ref="formRef" :model="insertForm" :rules="rules" label-width="110" label-position="right"
                 :inline="true">
          <el-row>
            <el-col :span="12">
              <el-form-item label="收款编号" prop="prepaymentNo">
                <el-input v-model="insertForm.prepaymentNo" readonly></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="客户名称" prop="tradePartnerName">
                <el-input v-model="insertForm.tradePartnerName" readonly>
                </el-input>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="收款金额（元）" prop="prepaymentAmt">
                <el-input-number style="width: 100%" min="0" :precision="2" :step="0.1" controls-position="right" disabled
                                 v-model="insertForm.prepaymentAmt"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="收款日期">
                <el-date-picker
                    disabled
                    style="width: 100%"
                    v-model="insertForm.prepaymentDt"
                    type="date"
                    value-format="YYYY-MM-DD"
                    placeholder=""
                    size="default"
                />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="收款类型" prop="payMth">
                <Selector_component :option-list="dictList.COM_PAY_MTH" :value="insertForm.payMth"  :allowSelect="true"
                                    @change="collectionTySelect"></Selector_component>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="承兑编号">
                <el-input v-model="insertForm.billNoteNo" readonly></el-input>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="票据张数">
                <el-input-number style="width: 100%" min="0" :step="1" controls-position="right" disabled
                                 v-model="insertForm.billNoteNum"></el-input-number>

              </el-form-item>
            </el-col>
            <!--            <el-col :span="12">-->
            <!--              <el-form-item label="收款分配标志">-->
            <!--                <el-input v-model="insertForm.prepDistributeSign"></el-input>-->
            <!--              </el-form-item>-->
            <!--            </el-col>-->
          </el-row>

          <el-row>

            <el-col :span="24" class="aaa">
              <el-form-item label="收款备注信息">
                <el-input readonly
                    v-model="insertForm.remarks"
                    :autosize="{ minRows: 2, maxRows: 4 }"
                    type="textarea"
                    placeholder=""
                />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </el-card>


      <template #footer>
        <div class="dialog-footer">
          <el-button type="danger" plain @click="detailsDialogShow = false">取消</el-button>
        </div>
      </template>


    </el-dialog>
    <Popup_base_ks ref="PopupRef" @receive-from-child="retu"></Popup_base_ks>

  </div>
</template>

<script setup>
import {
  ref,
  reactive,
  onMounted,
  getCurrentInstance
} from 'vue'
import {Search} from '@element-plus/icons-vue'

const {
  proxy
} = getCurrentInstance()


import style from '@/tools/style'

const cardBodyStyle = ref(style.card)

const tableStatus = reactive({
  tableRowEditIndex: undefined,
  tableColumnEditIndex: undefined,
})
import SearchBar from '@/components/SearchBar.vue';
import Selector_component from "@/views/sal/components/Selector_component.vue";
import Popup_base_ks from "@/views/bas/Popup_base_ks.vue";


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

])

const fId = ref('')

const searchForm = (val) => {
  query.value = searchBarRef.value.form
  console.log(JSON.stringify(query.value))
}

/**
 *   查询
 */

const query = ref({
  birthDate: '',
  skrq: '',
  type: '',
  code: '',
  name: '',
  sex: '',
  contractType: '',
  contractFlag: '',
  addTime: ['', '']
})
// 首 列表
const tableData = ref([])
// 物料 列表
const tableDataWl = ref([])
const tableDataTwo = ref([])
const tableDataMx = ref([])

const handleSearch = (val) => {
  query.value = searchBarRef.value.form
  console.log(JSON.stringify(query.value))
  currentPage.value = 1
  getTableData()
}
const getTableData = () => {
  var url = 'sal/PrepayErp/query'
  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') {
      tableData.value = response.data.data.records
      totalRows.value = response.data.data.total
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}
/**
 *   查询
 */
/**
 * 分页
 */
const currentPage = ref(1)
const totalRows = ref(0)
const pageSizes = ref([5, 10, 20, 30, 40])
const pageSize = ref(5)
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  getTableData()
}
/**
 * 分页
 */

import PopupMenuTree from '@/components/PopupMenuTree.vue'
import Attachment from "@/components/Attachment.vue";
import ButtonBar from "@/components/ButtonBar.vue";
import {workflowCancel, workflowSubmit} from "@/tools/workflow.js";

const PopupMenuTreeRef = ref(null)
const showPop = () => {
  console.log('showPop')
  PopupMenuTreeRef.value.show()
}
/**
 * 新增表单form
 */
const insertForm = ref({
  id: '',
  prepaymentNo: '',
  supplierNm: '',
  prepaymentAmt: 1,
  prepaymentDt: proxy.$dayjs(),
  payMth: '',
  billNoteNo: '',
  billNoteNum: 0,
  prepDistributeSign: '',
  remarks: '',
  prepaymentDtStart: '',
  prepaymentDtEnd: '',

})

// 新增订单
const insertDialogShow = ref(false)
const detailsDialogShow = ref(false)
const insertDialog = () => {
  insertForm.value = {
    id: '',
    prepaymentNo: '',
    supplierNm: '',
    prepaymentAmt: 1,
    prepaymentDt: proxy.$dayjs(),
    payMth: '',
    billNoteNo: '',
    billNoteNum: 0,
    prepDistributeSign: '',
    remarks: '',

  }
  insertDialogShow.value = true
}
const formRef = ref();
const insertDialogSave = (formRef) => {
  if (!formRef) return
  formRef.validate((valid) => {
    if (valid) {
      var url = 'sal/PrepayErp/save'
      var param = {
        json: JSON.stringify(insertForm.value),
      }
      proxy.$axios.post(url, param).then(response => {
        if (response.data.code == 0) {
          ElMessage({
            message: '保存成功',
            type: 'success',
          })
          getTableData()
          insertDialogShow.value = false
        } else {
          // ElMessage.error('操作失败！' + response.data.msg)
          ElMessage({
            type: 'error',
            message: '操作失败' + response.data.msg
          })
        }
      });
    } else {
      console.log('error submit!')
      return false
    }

  })
}

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

}

// 校验
const rules = reactive({
  tradePartnerName: [
    {required: true, message: '不允许为空', trigger: 'change'},
  ],
  prepaymentAmt: [
    {required: true, message: '不允许为空', trigger: 'blur'},
  ],
  payMth: [
    {required: true, message: '不允许为空', trigger: 'change'},
  ],
})

const otherQuery = () => {

}

// 首-列表 编辑
const handleEdit = (row) => {
  if (row.processInstanceStatus == 'Y') {
    ElMessage({
      type: 'warning',
      message: '数据审批中，不允许操作！'
    })
    return
  }
  var url = 'sal/PrepayErp/queryByPK'
  var param = {
    id: row.id
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      insertForm.value = response.data.data
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
  insertDialogShow.value = true

}

// 首-列表 编辑
const handleDelete = (row) => {
  if (row.processInstanceStatus == 'Y') {
    ElMessage({
      type: 'warning',
      message: '数据审批中，不允许操作！'
    })
    return
  }
  var url = 'sal/PrepayErp/remove'
  var param = {
    id: row.id
  }
  ElMessageBox.confirm(
      '确定删除本条记录?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  )
      .then(() => {
        proxy.$axios.get(url, param).then(response => {
          if (response.data.code == '0') {
            console.log(response.data.data)
            ElMessage({
              type: 'success',
              message: '删除成功！',
            });
            getTableData()
          } else {
            ElMessage.error('操作失败！' + response.data.msg)
          }
        })
      })
      .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) => {
  console.log('取消焦点', 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) => {
  console.log("row", 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) => {
  console.log("row", row)
  console.log("column", column)
  console.log("cell", cell)
  console.log("event", event)
  if (column.property === 'itemCd') {
    this.tableDataWl.forEach(item => {
      if (item.id === row.id) {
        item.xEdit = true
      }
    })
  }

}

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

}
const dic = (val) => {
  var reStr = ''
  for (var i = 0; i < dictList.value.COM_PAY_MTH.length; i++) {
    if (dictList.value.COM_PAY_MTH[i].dicCode == val) {
      reStr = dictList.value.COM_PAY_MTH[i].dicName
      return reStr
    }
  }
  return reStr
}
const dicIF = (val) => {
  var reStr = val
  for (var i = 0; i < dictList.value.IF.length; i++) {
    if (dictList.value.IF[i].dicCode == val) {
      reStr = dictList.value.IF[i].dicName
      return reStr
    }
  }
  return reStr
}
const collectionTySelect = (val) => {
  insertForm.value.payMth = val
}

const search = () => {
  getTableData()
}
const aaa = (val) => {
  insertForm.value.prepaymentDtStart = val[0]
  insertForm.value.prepaymentDtEnd = val[1]
}
const PopupRef = ref(null)
const ksDialog = () => {
  PopupRef.value.open()
}

const retu = (val) => {
  console.log("val", val)
  insertForm.value.tradePartnerName = val.tradePartnerName
  insertForm.value.tradePartnerCode = val.tradePartnerCode
  insertForm.value.tradePartnerId = val.id
}
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}
const submit = (param) => {
  if (multipleSelection.value.length < 1) {
    ElMessage({
      type: 'warning',
      message: '请选择要提交的数据!'
    })
    return
  }

  edit('提交', 'Y')

}
const flowId = ref('SalPayFinance')
const handleSubmit = (row) => {
  ElMessageBox.confirm(
      '确定提交流程?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  ).then(() => {
    var obj = {
      id : row.id
    }
    return workflowSubmit(
        flowId.value,
        '收款审批流程',
        '初始化提交',
        JSON.stringify(obj),
        row.workId,
        row.trackId)
  }).then((result) => {
    if(result.status === 'success'){

      console.log(result)
      // 将结果返回的workId和trackId保存
      var url = 'sal/PrepayErp/submitFq'
      var process = {
        'id': row.id,
        'workId': result.workId,
        'trackId': result.trackId,
      }
      var param = {
        json: JSON.stringify(process),
      }
      proxy.$axios.post(url, param).then(response => {
        if (response.data.code === 0) {
          // form.value = response.data.data
          ElMessage({
            type: 'success',
            message: '提交审批成功',
          })
          getTableData()
          uploadButton.value = false
        }
      })
    }
    else{
      ElMessage({
        type: 'error',
        message: '提交审批失败' + response.data.msg
      });
    }
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消提交',
    })
  })
}

const edit = (title, val) => {
  var ids = []
  for (var i = 0; i < multipleSelection.value.length; i++) {
    ids.push(multipleSelection.value[i].id)
  }

  var param = {
    ids: JSON.stringify(ids),
    status: val
  }
  var url = 'sal/PrepayErp/submit'
  ElMessageBox.confirm(
      '此操作将' + title + '该数据, 是否继续?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  )
      .then(() => {
        proxy.$axios.post(url, param).then(response => {
          if (response.data.code == '0') {
            ElMessage({
              type: 'success',
              message: title + '成功！',
            });

            getTableData()
          } else {
            ElMessage.error('操作失败！' + response.data.msg)
          }
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消',
        })
      })
}
const uploadButton = ref(false)
const cellHover = (row) => {
  console.log("row", row)

  if (row.processInstanceStatus !== '110' && row.processInstanceStatus !== '170') {
    uploadButton.value = true
  } else {
    uploadButton.value = false
  }
  fId.value = row.id
}
const handleDetail = (row) => {
  if(row.workId === null){
    ElMessage({
      type: 'info',
      message: '无法查看',
    })
  }else {

    sessionStorage.setItem('workflowQuery', JSON.stringify({
      flowId: flowId.value,
      workId: row.workId,
      trackId: row.trackId,
      date:new Date().getTime()
    }))

    proxy.$router.push({
      path: '/workflow/detail',
    })

  }
}

const handleCancel = (row) => {
  ElMessageBox.confirm(
      '确定撤回审批?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  ).then(() => {
    return workflowCancel(row.workId, row.trackId);
  }).then((result) => {
    if(result.status === 'success'){
      ElMessage({
        type: 'success',
        message: '撤回审批成功',
      })
      getTableData()
      uploadButton.value = true
    }else {
      ElMessage({
        type: 'error',
        message: '撤回审批失败，' + result.msg
      });
    }
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消提交',
    })
  })
}
const handleInfo = (row) => {
  insertForm.value = row
  detailsDialogShow.value = true
}
onMounted(() => {
  getTableData()
  dictData()
  console.log(`the component is now mounted.`)
})
</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%;
}

::v-deep .diaClass .el-form-item__label {
  width: 140px !important;
}

</style>
