<template>
  <div>
    <el-card :body-style="cardBodyStyle">
      <el-form inline>
        <el-row :span="24">
          <el-col :span="5">
            <el-form-item label="物料名称" prop="">
              <el-select v-model="query.leibiemc" placeholder="请选择名称" filterable clearable style="width: 200px;">
                <el-option v-for="item in typeList" :key="item.itemCd" :label="item.itemNm"
                           :value="item.itemNm"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item prop="wuliPdsj" label="是否接样">
              <el-select v-model="query.chemRcvTime" size="normal" style="width:200px" clearable>
                <el-option label="已接样" value="0"/>
                <el-option label="未接样" value="1"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="样品类型" prop="">
              <el-select v-model="query.smpType" placeholder="请选择样品类型" clearable style="width: 180px;">
                <el-option v-for="item in smpTypeList" :key="item.bm" :label="item.wtmc"
                           :value="item.bm"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="化学编号" prop="">
              <el-input v-model="query.huaxuezybh" placeholder="请输入化学制样编号" clearable style="width: 180px;"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" @click="show" link>高级查询
              <el-icon v-if="showA">
                <ArrowUp/>
              </el-icon>
              <el-icon v-else>
                <ArrowDown/>
              </el-icon>
            </el-button>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" @click="reset" link>重置</el-button>
          </el-col>
        </el-row>
        <el-row :span="24" v-if="showA">
          <el-col :span="5">
            <el-form-item label="制样提交时间" prop="">
              <el-date-picker v-model="query.zhiyangtjsj" type="datetimerange" align="right" unlink-panels
                              range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间"
                              style="width: 403px;" value-format="YYYY-MM-DD HH:mm:ss" format="YYYY-MM-DD HH:mm:ss">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

    </el-card>
    <el-card :body-style="cardBodyStyle">
      <el-button type="primary"  plain @click="handleSearch">查询</el-button>
      <el-button type="success"  plain @click="handleSure">接样确认</el-button>
      <el-button type="danger"  plain @click="handleCancel">取消接样</el-button>
    </el-card>
    <el-card :body-style="cardBodyStyle">
      <el-table :data="tableData" border highlight-current-row @selection-change="handleSelectionChange">
        <el-table-column type="selection" fixed></el-table-column>
        <el-table-column type="index" label="序号" align="center" width="60" fixed>
          <template #default="scope">
            <span>{{ scope.$index + (currentPage - 1) * pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="委托编号" width="180" header-align="center" align="center"
                         show-overflow-tooltip></el-table-column>

        <el-table-column prop="leibiemc" label="物料类别" width="150" header-align="center" align="center"
                         show-overflow-tooltip ></el-table-column>
        <el-table-column prop="quyangsj" label="取样时间" width="200" header-align="center" align="center"
                         show-overflow-tooltip></el-table-column>
<!--        <el-table-column prop="quyangdwname" label="取样单位" width="200" header-align="center" align="center"-->
<!--                         show-overflow-tooltip></el-table-column>-->
<!--        <el-table-column prop="zhiyangdwname" label="制样单位" width="200" header-align="center" align="center"-->
<!--                         show-overflow-tooltip></el-table-column>-->
        <el-table-column prop="weituoztMc" label="委托状态" width="180" header-align="center" align="center"
                         show-overflow-tooltip></el-table-column>
        <el-table-column prop="weituolx" label="委托类型" width="120" header-align="center" align="center"
                         show-overflow-tooltip></el-table-column>
        <el-table-column prop="weituotype" label="检验类型" width="100" header-align="center" align="center"
                         show-overflow-tooltip></el-table-column>
        <el-table-column prop="chemRcvSmpName" label="接样人" width="120" header-align="center" align="center"
                         show-overflow-tooltip></el-table-column>
        <el-table-column prop="chemRcvTime" label="接样时间" width="180" header-align="center" align="center"
                         show-overflow-tooltip></el-table-column>
        <el-table-column prop="zhiyangtjsj" label="制样提交时间" width="180" header-align="center" align="center"
                         show-overflow-tooltip></el-table-column>
      </el-table>
      <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>
  </div>
</template>

<script setup>
import {
  ref,
  reactive,
  onMounted,
  getCurrentInstance
} from 'vue'
import style from '@/tools/style'
import {ElMessage, ElMessageBox} from "element-plus";
import FineReport from "@/components/report/FineReport.vue";


onMounted(() => {
  queryType()
  queryWTLXType()
  handleSearch()
})


const {proxy} = getCurrentInstance()
const cardBodyStyle = ref(style.card)
const dialogVisible = ref(false)
const form = ref({})
const tableData = ref([])
const query = ref({
  leibiemc: '',
  jieyangsj: '',
  huaxuezybh: '',
  zhiyangtjsj: '',
  chemRcvTime: '1',
  smpType: '',

})
const showA = ref(false)
const show = () => {
  showA.value = !showA.value
}


let dept = localStorage.getItem('dept')
dept = JSON.parse(dept)
let deptCode = dept[0].deptCode
const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
  console.log(val)
}
/**
 * 重置查询条件
 */
const reset = () => {
  Object.keys(query.value).map(key => {
    query.value[key] = ''
  })
}
const typeList = ref([])
const queryType = () => {
  var url = 'sci/platform/queryType'
  var param = {}
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      typeList.value = response.data.data
      console.log(response.data.data)
      console.log(typeList.value)
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}

const smpTypeList = ref([])
// 查询委托类型
const queryWTLXType = () => {
  var url = 'sci/Weituogl/query_YLType'
  var param = {}
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      smpTypeList.value = response.data.data
      // console.log(smpTypeList.value)
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}


// ==========按钮功能===========
// 搜索
const handleSearch = () => {
  currentPage.value = 1
  getTableData()
}
var userName = localStorage.getItem('userDesc')
// 接样确认
const handleSure = () => {
  if (multipleSelection.value.length < 1) {
    ElMessage({
      type: 'warning',
      message: '请选择要提交的数据!'
    })
    return
  }
  var ids = []
  multipleSelection.value.forEach((item, index) => {
    ids.push(item.id)
  })
  var url = 'sci/sample/sure'
  var param = {
    ids: JSON.stringify(ids),
    userName: userName,
    deptCode: deptCode
  }
  proxy.$axios.post(url, param).then(response => {
    if (response.data.code == '0') {
      ElMessage({
        type: 'success',
        message: '操作成功!',
      });
      getTableData()
      dialogVisible.value = false
    } else {
      ElMessage({
        type: 'error',
        message: '操作失败!' + response.data.msg
      });
    }
  })
}

const handleCancel = () => {
  if (multipleSelection.value.length < 1) {
    ElMessage({
      type: 'warning',
      message: '请选择要提交的数据!'
    })
    return
  }
  var ids = []
  multipleSelection.value.forEach((item, index) => {
    ids.push(item.id)
  })
  var url = 'sci/sample/cancel'
  var param = {
    ids: JSON.stringify(ids),
    userName: userName,
    deptCode: deptCode
  }
  proxy.$axios.post(url, param).then(response => {
    if (response.data.code == '0') {
      ElMessage({
        type: 'success',
        message: '操作成功!',
      });
      getTableData()
      dialogVisible.value = false
    } else {
      ElMessage({
        type: 'error',
        message: '操作失败!' + response.data.msg
      });
    }
  })
}
// ==========查询数据===========
const getTableData = () => {
  var url = 'sci/sample/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
      console.log(tableData.value)
      totalRows.value = response.data.data.total
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}

/**
 * 分页
 */
const currentPage = ref(1)
const totalRows = ref(0)
const pageSizes = ref([10, 20, 30, 40])
const pageSize = ref(10)
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  getTableData()
}
</script>

<style scoped></style>