<!--
 * @Descripttion: (出库管理/装配记录表/out_assemble_record)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2023-04-14)
 * @LastEditors: (admin)
 * @LastEditTime: (2023-04-14)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent
      size="large">
      <el-row>
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="queryParams.materialCode" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="queryParams.materialName" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="物料类型" prop="category">
          <!-- <el-select v-model="queryParams.category" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.materialTypeOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.materialTypeOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.category" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.materialTypeOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="规格型号 / 图号" prop="materialSpec">
          <el-input v-model="queryParams.materialSpec" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="装配结果" prop="result">
          <!-- <el-select v-model="queryParams.result" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.operationResultOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.operationResultOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.result" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.operationResultOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="关键件标识" prop="keyFlag">
          <!-- <el-select v-model="queryParams.keyFlag" placeholder="请选择" clearable style="width:120px" @change="handleQuery">
            <el-option v-for="dict in options.keyFlagOptions" :key="dict.dictValue" :label="dict.dictLabel"
              :value="dict.dictValue">
              <dict-tag :options="options.keyFlagOptions" :value="dict.dictValue" />
            </el-option>
          </el-select> -->
          <el-radio-group v-model="queryParams.keyFlag" @change="handleQuery">
            <el-radio-button>全部</el-radio-button>
            <el-radio-button v-for="dict in options.keyFlagOptions" :label="dict.dictValue">
              {{ dict.dictLabel }}
            </el-radio-button>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="生产令号" prop="bomNo">
          <el-input v-model="queryParams.bomNo" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="BN / SN码">
          <el-input v-model="queryParams.bnSnCode" placeholder="请输入" class="z-el-input" clearable
            @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-date-picker v-model="queryCreateTime" style="width: 400px" type="datetimerange" range-separator="-"
            @change="handleQuery" start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
          <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
        </el-form-item>
      </el-row>

    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['business:outassemblerecord:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="success" :disabled="single" v-hasPermi="['business:outassemblerecord:edit']" plain icon="edit"
          @click="handleUpdate">
          {{ $t('btn.edit') }}
        </el-button>
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button type="danger" :disabled="multiple" v-hasPermi="['business:outassemblerecord:delete']" plain
          icon="delete" @click="handleDelete">
          {{ $t('btn.delete') }}
        </el-button>
      </el-col> -->

      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['business:outassemblerecord:export']">
          {{ $t('btn.export') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <!-- 数据区域 -->
    <el-table :data="dataList" v-loading="loading" ref="table" highlight-current-row @sort-change="sortChange"
      @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="50" align="center" /> -->
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="assembleId" label="ID" align="center" v-if="columns.showColumn('assembleId')" />
      <el-table-column prop="bomNo" label="生产令号" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('bomNo')" />
      <el-table-column prop="materialCode" label="物料编码" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialCode')" />
      <el-table-column prop="materialName" label="物料名称" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialName')" />
      <el-table-column prop="materialSpec" label="规格型号/图号" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('materialSpec')" />
      <el-table-column prop="category" label="物料类型" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('category')">
        <template #default="scope">
          <dict-tag :options="options.materialTypeOptions" :value="scope.row.category" />
        </template>
      </el-table-column>
      <el-table-column prop="keyFlag" label="关键件" align="center" v-if="columns.showColumn('keyFlag')">
        <template #default="scope">
          <el-icon v-if="scope.row.keyFlag" size="large" color="red">
            <StarFilled />
          </el-icon>
          <el-icon v-else />
        </template>
      </el-table-column>
      <el-table-column prop="bnSnCode" label="BN/SN码" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('bnSnCode')" />
      <el-table-column prop="result" label="装配结果" align="center" v-if="columns.showColumn('result')">
        <template #default="scope">
          <dict-tag :options="options.operationResultOptions" :value="scope.row.result" />
        </template>
      </el-table-column>
      <el-table-column prop="picture" label="实物图片" align="center" v-if="columns.showColumn('picture')">
        <template #default="scope">
          <el-image preview-teleported :hide-on-click-modal="true" lazy class="table-td-thumb" fit="contain"
            :src="scope.row.picture" :preview-src-list="[scope.row.picture]">
            <div>
              <el-icon :size="15">
                <document />
              </el-icon>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="assembleQty" label="装配数量" align="center" v-if="columns.showColumn('assembleQty')" />
      <el-table-column prop="remark" label="备注" align="center" :show-overflow-tooltip="true"
        v-if="columns.showColumn('remark')" />
      <el-table-column prop="createBy" label="创建人" align="center" v-if="columns.showColumn('createBy')" />
      <el-table-column prop="createTime" label="创建时间" align="center" width="160"
        v-if="columns.showColumn('createTime')" />
      <el-table-column prop="updateBy" label="更新人" align="center" v-if="columns.showColumn('updateBy')" />
      <el-table-column prop="updateTime" label="更新时间" align="center" width="160"
        v-if="columns.showColumn('updateTime')" />
      <!-- <el-table-column label="操作" align="center" width="160">
                                      <template #default="scope">
                                        <el-button v-hasPermi="['business:outassemblerecord:edit']" type="success" plain icon="edit" title="编辑"
                                          @click="handleUpdate(scope.row)"></el-button>
                                        <el-button v-hasPermi="['business:outassemblerecord:delete']" type="danger" plain icon="delete" title="删除"
                                          @click="handleDelete(scope.row)"></el-button>
                                      </template>
                                    </el-table-column> -->
    </el-table>
    <pagination class="mt10" background :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />

    <!-- 添加或修改出库管理/装配记录表对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open">
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">

          <el-col :lg="12">
            <el-form-item label="装配记录Id" prop="assembleId">
              <el-input v-model="form.assembleId" placeholder="请输入装配记录Id" :disabled="opertype != 1" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="生产令号" prop="bomNo">
              <el-input v-model="form.bomNo" placeholder="请输入生产令号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料编码" prop="materialCode">
              <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="规格型号/图号" prop="materialSpec">
              <el-input v-model="form.materialSpec" placeholder="请输入物料规格" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料类型" prop="category">
              <el-radio-group v-model="form.category">
                <el-radio-button v-for=" item  in  options.materialTypeOptions " :key="item.dictValue"
                  :label="item.dictValue">{{
                    item.dictLabel }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="关键件标识" prop="keyFlag">
              <el-radio-group v-model="form.keyFlag">
                <el-radio-button v-for=" item  in  options.keyFlagOptions " :key="item.dictValue"
                  :label="item.dictValue">{{
                    item.dictLabel }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="装配数量" prop="assembleQty">
              <el-input-number v-model="form.assembleQty" />
            </el-form-item>
          </el-col>

          <el-col :lg="24">
            <el-form-item label="实物图片" prop="picture">
              <UploadImage v-model="form.picture" :data="{ uploadType: 1 }" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="装配结果" prop="result">
              <el-radio-group v-model="form.result">
                <el-radio-button v-for=" item  in  options.operationResultOptions " :key=item.dictValue
                  :label="item.dictValue">{{
                    item.dictLabel }}</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建者" prop="createBy">
              <el-input v-model="form.createBy" placeholder="请输入创建者" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker v-model="form.createTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="更新者" prop="updateBy">
              <el-input v-model="form.updateBy" placeholder="请输入更新者" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="更新时间" prop="updateTime">
              <el-date-picker v-model="form.updateTime" type="datetime" :teleported="false"
                placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="BN/SN码" prop="bnSnCode">
              <el-input v-model="form.bnSnCode" placeholder="请输入BN/SN码" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="outassemblerecord">
import {
  listOutAssembleRecord, addOutAssembleRecord, delOutAssembleRecord, updateOutAssembleRecord, getOutAssembleRecord,
  exportOutAssembleRecord
}
  from '@/api/business/outassemblerecord.js'

const { proxy } = getCurrentInstance()
// 选中assembleId数组数组
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const loading = ref(false)
const showSearch = ref(true)
const queryCreateTime = ref([])
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
  bomNo: undefined,
  materialCode: undefined,
  materialName: undefined,
  materialSpec: undefined,
  category: undefined,
  keyFlag: undefined,
  result: undefined,
  bnSnCode: undefined,
  beginTime: undefined,
  endTime: undefined,
})
const columns = ref([
  { visible: false, prop: 'assembleId', label: 'ID' },
  { visible: true, prop: 'bomNo', label: '生产令号' },
  { visible: true, prop: 'materialCode', label: '物料编码' },
  { visible: true, prop: 'materialName', label: '物料名称' },
  { visible: true, prop: 'materialSpec', label: '物料规格' },
  { visible: true, prop: 'category', label: '物料类型' },
  { visible: true, prop: 'keyFlag', label: '关键件' },
  { visible: true, prop: 'bnSnCode', label: 'BN/SN码' },
  { visible: true, prop: 'result', label: '装配结果' },
  { visible: true, prop: 'picture', label: '实物图片' },
  { visible: true, prop: 'assembleQty', label: '装配数量' },
  { visible: true, prop: 'createBy', label: '创建人' },
  { visible: true, prop: 'createTime', label: '创建时间' },
  { visible: false, prop: 'updateBy', label: '更新人' },
  { visible: false, prop: 'updateTime', label: '更新时间' },
  { visible: false, prop: 'remark', label: '备注' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])

function getPictureUrl(picture) {
  // let urlCreater = window.URL || window.webkitURL
  const blob = new Blob([picture], { type: 'image/jpeg' })
  const imageUrl = URL.createObjectURL(blob)
  console.log(blob)
  console.log(imageUrl)
  return imageUrl
}
function getList() {
  loading.value = true
  listOutAssembleRecord(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  if (queryCreateTime.value != null) {
    queryParams.beginTime = queryCreateTime.value[0]
    queryParams.endTime = queryCreateTime.value[1]
    // console.log(queryParams)
  }
  else {
    queryParams.beginTime = undefined
    queryParams.endTime = undefined
  }
  getList()
}

// 重置查询操作
function resetQuery() {
  queryCreateTime.value = []
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.assembleId);
  single.value = selection.length != 1
  multiple.value = !selection.length;
}

// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

const title = ref("")
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  form: {},
  rules: {
    assembleId: [{ required: true, message: "装配记录Id不能为空", trigger: "blur", type: "number" }],
    bomNo: [{ required: true, message: "生产令号不能为空", trigger: "blur" }],
    materialCode: [{ required: true, message: "物料编码不能为空", trigger: "blur" }],
    materialName: [{ required: true, message: "物料名称不能为空", trigger: "blur" }],
    materialSpec: [{ required: true, message: "物料规格不能为空", trigger: "blur" }],
    category: [{ required: true, message: "类别不能为空", trigger: "blur" }],
    keyFlag: [{ required: true, message: "关键件标识不能为空", trigger: "blur" }],
    assembleQty: [{ required: true, message: "装配数量不能为空", trigger: "blur", type: "number" }],
    result: [{ required: true, message: "装配结果不能为空", trigger: "blur" }],
    bnSnCode: [{ required: true, message: "BN/SN码不能为空", trigger: "blur" }],
  },
  options: {
    keyFlagOptions: [], // 关键件标识字典
    operationResultOptions: [],// 操作结果字典
    materialTypeOptions: []// 物料类型字典
  }
})

const { form, rules, options } = toRefs(state)

var dictParams = [
  { dictType: 'my_key_flag', columnName: 'keyFlagOptions' },
  { dictType: 'my_operation_result', columnName: 'operationResultOptions' },
  { dictType: 'my_material_type', columnName: 'materialTypeOptions' },
]
proxy.getDicts(dictParams).then((response) => {
  response.data.forEach((element) => {
    state.options[element.columnName] = element.list
  })
})

// 关闭dialog
function cancel() {
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    assembleId: undefined,
    bomNo: undefined,
    materialCode: undefined,
    materialName: undefined,
    materialSpec: undefined,
    category: undefined,
    keyFlag: undefined,
    assembleQty: undefined,
    picture: undefined,
    result: undefined,
    createBy: undefined,
    createTime: undefined,
    updateBy: undefined,
    updateTime: undefined,
    remark: undefined,
    bnSnCode: undefined,
  };
  proxy.resetForm("formRef")
}

// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加'
  opertype.value = 1
}

// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.assembleId || ids.value
  getOutAssembleRecord(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = "修改数据"
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.assembleId != undefined && opertype.value === 2) {
        updateOutAssembleRecord(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
          .catch(() => { })
      } else {
        addOutAssembleRecord(form.value).then((res) => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
          .catch(() => { })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.assembleId || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？')
    .then(function () {
      return delOutAssembleRecord(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
    .catch(() => { })
}

// 导出按钮操作
function handleExport() {
  proxy
    .$confirm('是否确认导出当前装配记录?', '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    })
    .then(async () => {
      await exportOutAssembleRecord(queryParams)
    })
}
handleQuery()
</script>
