<!--物料信息维护-->
<template>
  <div class="vue-element-page-wrap" ref="tableContainer">
    <el-row class="page-search" justify="space-between" align="middle">
      <el-form ref="queryFormRef" :model="state.queryParams.params" :inline="true" @submit.native.prevent>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="物料编号" prop="MATERIALSPECNAME">
              <el-Input v-model="state.queryParams.params.MATERIALSPECNAME" placeholder="物料编号"
                v-on:keyup.enter="queryInfo" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料名称" prop="MATERIALSPECNAME">
              <el-Input v-model="state.queryParams.params.DESC_CN" placeholder="物料名称" v-on:keyup.enter="queryInfo" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="恒温恒湿管控" prop="IS_CON_TEMP_T">
              <el-select v-model="state.queryParams.params.IS_CON_TEMP_T" placeholder="恒温恒湿管控" style="width: 240px"
                default-first-option>
                <el-option v-for="item in state.TEMPList" :key="item.value" :label="item.label"
                  :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="page-search-btns">
        <el-button type="primary" @click="queryInfo" :disabled="state.disableds" v-loading="state.loadings">搜索</el-button>
      </div>
    </el-row>

    <div class="vue-element-page-table">
      <div class="page-table-operate">
        <div class="page-table-title">
          <div class="page-table-title-left">
            <span>物料信息列表</span>
          </div>
        </div>
        <div class="page-table-operateBtns">
          <el-popover :persistent="false" placement="right" popper-class="config-table-wrap" trigger="click">
            <template #reference>
              <el-button class="operateBtns-setting">
                <svg-icon icon-class="table_title" width="16px" height="16px" />
              </el-button>
            </template>
            <ConfigTable :list="materialTableConfig.column" @updateList="updateList" />
          </el-popover>
        </div>
      </div>
      <el-table border :height="tableHeight" v-loading="materialTableConfig.loading" :data="state.MaterialList" highlight-current-row
        row-key="id"  fit flexible  >
        <template v-for="(col, index) in materialTableConfig.column" :key="index">
          <el-table-column v-if="!col.hide" :label="col.header" show-overflow-tooltip>
            <template #default="scope">
              <span v-if="col.field === 'IS_CON_TEMP_T'">
                <span v-if="scope.row[col.field] == 'Y'" >是</span>
                <span v-else>否</span>
              </span>
              <span v-else-if="col.field === 'MATURATIONFLAG_T'">
                <span v-if="scope.row[col.field] == 'Y'" >是</span>
                <span v-else>否</span>
              </span>
              <span v-else-if="col.field === 'IQCFLAG_T'">
                <span v-if="scope.row[col.field] == 'Y'">是</span>
                <span v-else>否</span>
              </span>
              <span v-else>{{ scope.row[col.field] }}</span>
            </template>
          </el-table-column>
        </template>

        <el-table-column width="100" label="操作" align="center" class-name="table-operation" fixed="right">
          <template #default="scope">
            <el-button type="primary" link @click.stop="handleUpdate(scope.row)" v-if="!scope.row.status"> 编辑
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-if="state.total > 0" :total="state.total" v-model:pageNumTo="state.queryParams.pageNum"
        v-model:pageSizeTo="state.queryParams.pageSize" @pagination="handleQuery" />

      <el-drawer title="物料维护" v-model="dialog.visible" @close="cancel" @opened="dialogOpend">
        <el-form ref="formDataRef" :model="formData" :rules="rules" label-position="top">
          <el-form-item label="物料编码" prop="MATERIALSPECNAME">
            <el-input :disabled="true" v-model="formData.MATERIALSPECNAME" placeholder="请输入账号" />
          </el-form-item>
          <!-- <el-form-item label="是否启用保质期" prop="userId" v-if="dialog.type === 'add'">
            <el-input v-model="formData.userId" placeholder="是否启用保质期" />
          </el-form-item> -->
          <el-form-item label="保质期天数" prop="EXPIRATIONDAY_T">
            <el-input v-model="formData.EXPIRATIONDAY_T" placeholder="保质期天数" />
          </el-form-item>
          <el-form-item label="恒温恒湿管控" prop="IS_CON_TEMP_T">
            <el-select v-model="state.formData.IS_CON_TEMP_T" placeholder="恒温恒湿管控" style="width: 240px"
                default-first-option>
                <el-option v-for="item in state.TEMPList" :key="item.value" :label="item.label"
                  :value="item.value" />
              </el-select>
          </el-form-item>
          <el-form-item label="是否熟化" prop="MATURATIONFLAG_T">
            <el-select v-model="state.formData.MATURATIONFLAG_T" placeholder="是否熟化" style="width: 240px"
                default-first-option>
                <el-option v-for="item in state.TEMPList" :key="item.value" :label="item.label"
                  :value="item.value" />
              </el-select>
          </el-form-item>
          <el-form-item label="熟化天数" prop="MATURATIONTIME_T">
            <el-input v-model="state.formData.MATURATIONTIME_T" placeholder="熟化天数" />
          </el-form-item>
          <el-form-item label="辅单位" prop="FUNIT_T">
            <el-input :disabled="true" v-model="state.formData.FUNIT_T" placeholder="辅单位" />
          </el-form-item>
          <el-form-item label="换算率" prop="UNIT_RATES_T">
            <el-input v-model="state.formData.UNIT_RATES_T" placeholder="换算比例" />
          </el-form-item>
          <el-form-item label="是否质检" prop="IQCFLAG_T">
            <el-select v-model="state.formData.IQCFLAG_T" placeholder="是否质检" style="width: 240px"
                default-first-option>
                <el-option v-for="item in state.TEMPList" :key="item.value" :label="item.label"
                  :value="item.value" />
              </el-select>
          </el-form-item>
          <el-form-item label="质检周期(天)" prop="INSPECTION_CYCLE_T">
            <el-input v-model="state.formData.INSPECTION_CYCLE_T" placeholder="质检周期" />
          </el-form-item>
        </el-form>

        <template #footer>
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </template>
      </el-drawer>
    </div>
  </div>
</template>
<script lang="ts">
export default {
  name: "materialMaint",
};
</script>
<script lang="ts" setup>
import { reactive, ref, onMounted, onUnmounted, toRefs, getCurrentInstance, nextTick } from "vue";
import { ElForm, ElMessageBox, ElTable } from "element-plus";
import { getQueryList, getQueryPageList, BpelEvent } from '@/api/common';
import { editMaterial } from '@/api/basics/material'
import { bTableHeight } from "../../../../composables/calcTableHeight";

const { tableContainer, tableHeight, updateTableHeight, handleResize } = bTableHeight(180);
const { proxy }: any = getCurrentInstance();

const queryFormRef = ref(ElForm);
const formDataRef = ref(ElForm);

const formDataRefData = ref({
  SITENAME: '',//工厂
  TEMPList: [{ label: '是', value: 'Y' }, { label: '否', value: 'N' }] as any,
  SITENAMElIST: [{ label: 'A', value: 'a' }],//工厂列表
  MATERIALSPECNAME: '',//物料编号
  MATERIALSPECDESCRIPTION: '',//物料描述
  CHECKSTATE: '',//check state
  CHECKSTATELIST: [{ label: 'A', value: 'a' }],//check state列表
  ERPLOCATION: '',//ERP库位
  MATERIALTYPE: '',//物料类型
  MATERIALLIFEMARK: '',//寿命管理标记
  MATERIALGROUP: '',//物料组
  DESC_CN: '',//中文描述
  DESC_EN: '',//英文描述
  EXPIRINGDATE: '',//过期时间
  STOCKDATE: '',//入库有效期
  MATERIALGROUPDESCRIPTION: '',//物料组描述
  MATERIALUNIT: '',//单位
  MATERIALWEIGHT: '',//物料重量
  ERPFACTORY: '',//ERP工厂
  MATERIALSPECTYPE: '',//物料类别
})

/***---------------------------------------------------------------------------
 * -------------------------------------页面SQL语句配置------------------------------------------ */
const state = reactive({
  loadings : false,
  disableds : false,
  rsa: '',
  DBProcessType: 'save',
  queryParams: {
    queryId: 'GetMMaterialSpecList',
    version: '10005',
    params: {
      MATERIALSPECNAME: '',
      SITENAME: 'SDK',
      DESC_CN: '',
      IS_CON_TEMP_T: '',
      userId: localStorage.getItem('userId')
    },
    pageNum: 1,
    pageSize: 20
  },
  ChangeParams: {
    header: {
      MESSAGENAME: '',
      LANGUAGE: 'Chinese',
    },
    body: {
      INRATIO: '',
      OUTRATIO: '',
      IS_CON_TEMP: '',
      DESCRIPTION: '',
      MATERIALSPECNAME: '',
      SITENAME: 'SDK',
      MATERIALSPECDESCRIPTION: '',
      CHECKSTATE: '',
      MATERIALSPECTYPE: '',
      MAKEDATE: '',
      MAKEUSER: '',
      DESC_CN: '',
      DESC_EN: '',
      MATERIALTYPE: '',
      MATERIALGROUP: '',
      MATERIALGROUPDESCRIPTION: '',
      MATERIALUNIT: '',
      MATERIALWEIGHT: '',
      ERPFACTORY: '',
      ERPLOCATION: '',
      MATERIALLIFEMARK: '',
      EXPIRINGDATE: '',
      STOCKDATE: '',
    }
  },
  total: 0,
  loading: false,
  MaterialList: [] as any,
  dialog: {
    title: "物料维护",
    type: 'add',
    visible: false,
  },
  TEMPList: [{ label: '全部', value: '' },{ label: '是', value: 'Y' }, { label: '否', value: 'N' }] as any,
  formData: {
    siteName: 'SDK',
    MATERIALSPECNAME:"",
    EXPIRATIONDAY_T: null,
    IS_CON_TEMP_T: "",
    MATURATIONFLAG_T: "",
    MATURATIONTIME_T: "",
    FUNIT_T: "",
    UNIT_RATES_T: "",
    IQCFLAG_T: "",
    INSPECTION_CYCLE_T: ""
  } as any,
  column: [] as any,
  selectItem: undefined as any,
  currentTime: '',
  allRole: [] as any

})
const { rsa, queryParams, total, loading, userList, dialog, formData, rules, column, selectItem, currentTime, roleDialog, allRole } = toRefs(state);
const materialTableConfig = ref({
  loading: false,
  column: [] as any,
  data: [],
})

/***---------------------------------------------------------------------------
 * -------------------------------------钩子函数区域------------------------------------------ */
onMounted(() => {
  materialTableConfig.value.column = [
    { header: "物料编号", field: "MATERIALSPECNAME", hide: false },
    { header: "品名", field: "DESC_CN", hide: false },
    { header: "规格型号", field: "DESCRIPTION", hide: false },
    { header: "物料类型", field: "MATERIALTYPE", hide: false },
    { header: "是否恒温管控", field: "IS_CON_TEMP_T", hide: false },
    { header: "是否熟化", field: "MATURATIONFLAG_T", hide: false },
    { header: "烘烤天数", field: "MATURATIONTIME_T", hide: false },
    { header: "主单位", field: "MATERIALUNIT", hide: false },
    { header: "辅单位", field: "FUNIT_T", hide: false },
    { header: "换算率", field: "UNIT_RATES_T", hide: false },
    { header: "有效期计算规则", field: "REPLACED_VALUE", hide: false },
    { header: "是否启用批次", field: "IS_BATCH", hide: false },
    { header: "是否质检", field: "IQCFLAG_T", hide: false },
    { header: "检验周期", field: "INSPECTION_CYCLE_T", hide: false }
  ]
  // 初始化时调用 handleQuery
  handleQuery();
  updateTableHeight(); // 初始化时计算一次高度
  window.addEventListener('resize', handleResize);
});
onUnmounted(() => {
  window.removeEventListener('resize', handleResize);
});

const queryInfo = () =>  {
  state.queryParams.pageNum = 1
  handleQuery()
}
const handleQuery = () => {
  state.loadings = true;
  state.disableds = true;
  console.log(state.queryParams)
  getQueryPageList(state.queryParams)
    .then((res: any) => {
      console.log(res.data);
      if(res.data.list.length != 0){
        state.MaterialList = res.data.list;
        state.total = res.data.total;
        state.loadings = false;
        state.disableds = false;
      } else {
        ElMessageBox.alert('查无此数据', '提醒框', {
          confirmButtonText: 'OK',
        })
        state.loadings = false;
        state.disableds = false;
      }
    })
    .catch(() => { });
}
function submitForm() {
  formDataRef.value.validate((valid: boolean) => {
    if (valid) {
      // console.log(valid)
      // state.formData.EXPIRATIONDAY_T = state.formData.IS_CON_TEMP_T == '是' ? "Y" : "N";
      // state.formData.MATURATIONFLAG_T = state.formData.MATURATIONFLAG_T == '是' ? "Y" : "N";
      // state.formData.IQCFLAG_T = state.formData.IQCFLAG_T == '是' ? "Y" : "N";
      state.formData.IS_CHECK = "N"
    
      editMaterial({...state.formData, user: localStorage.getItem('userId')})
      .then((res: any) => {
        dialog.value.visible = false;
        handleQuery();
      }).then(() => proxy.$ElMessage.success('修改成功'))
      .catch(() => { })
    } else {
      return false;
    }
  });
}
const updateList = (col: any) => {
  materialTableConfig.value.column = col
}
const buildInfo = () => { state.DBProcessType = 'Insert' }
const editInfo = () => { state.DBProcessType = 'Update' }

function handleUpdate(row: any) {
  state.dialog = {
    title: "物料信息维护",
    visible: true,
    type: 'edit'
  };
  state.formData = {...row};
  // state.selectItem = row;
  if(state.formData.EXPIRATIONDAY_T) {
    state.formData.EXPIRATIONDAY_T = Number(state.formData.EXPIRATIONDAY_T)
  }else {
    state.formData.EXPIRATIONDAY_T =  360
  }

  if(!state.formData.MATURATIONTIME_T) {
   state.formData.MATURATIONTIME_T =  '0'
  }

}
function cancel() {
  state.dialog.visible = false;
}
const saveInfo = () => {
  if (state.DBProcessType == 'Insert') {
    state.ChangeParams.header.MESSAGENAME = 'CreateMaterialSpec'
  } else if (state.DBProcessType == 'Update') {
    state.ChangeParams.header.MESSAGENAME = 'ModifyMaterialSpec'
  }
  BpelEvent(state.ChangeParams).then((res: any) => {
  })
    .catch(() => { });
}
const exiteInfo = () => {
  formDataRef.value.resetFields()
}
</script>

<style scoped></style>
