<template>
  <el-dialog
    title="需求申报"
    append-to-body
    :modelValue="showDialog"
    width="80%"
    @close="closeDialog"
    @open="open"
    fullscreen
  >
    <el-form :inline="true" v-model="submitForm">
      <el-form-item label="银钾系数" prop="coefficient" v-if="searchForm.materalType == 2">
        <el-input placeholder="请输入银钾系数" v-model="submitForm.coefficient"></el-input>
      </el-form-item>
      <el-form-item :label="labelValue + '需求量'" prop="" v-if="searchForm.materalType == 1">
        <el-input placeholder="请输入需求量" v-model="submitForm.goodsQuantity"></el-input>
      </el-form-item>
      <el-form-item label="银钾需求量" prop="saveBoardVal" v-if="searchForm.materalType == 2">
        <el-input placeholder="请输入银钾需求量" v-model="submitForm.saveBoardVal"></el-input>
      </el-form-item>
      <!-- <el-form-item>
                <el-button type="primary" @click="onSubmit">提 交</el-button>
            </el-form-item> -->
    </el-form>
    <el-tag type="primary" style="width: 100%; display: block; line-height: 22px"
      >选择XX个订单 合计：物料需求量：711.00克</el-tag
    >
    <div style="border: 1px solid #ccc; padding: 20px 0px">
      <div>
        <el-form :inline="true" v-model="searchForm" label-width="90px">
          <el-form-item label="作业中心" prop="wcId">
            <el-select style="width: 182px" v-model="searchForm.wcId" placeholder="请选择作业中心">
              <el-option v-for="item in workList" :key="item.id" :label="item.wcName" :value="item.id"></el-option>
              <!-- <el-option label="作业中心二" value="2"></el-option> -->
            </el-select>
          </el-form-item>
          <el-form-item label="物料类型" prop="materalType">
            <el-radio-group v-model="searchForm.materalType" @input="changeType">
              <el-radio :label="1">金钾</el-radio>
              <el-radio :label="2">银钾</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item>
            <el-checkbox v-model="searchForm.overHalfMon">允许使用半月以上的数据</el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSearch" :loading="loading">查 询</el-button>
          </el-form-item>
        </el-form>

        <el-table :data="tableData" @selection-change="handleSelectionChange" ref="multipleTable" :loading="loading">
          <el-table-column type="selection"></el-table-column>
          <el-table-column
            label="计划单号"
            prop="ypCode"
            align="center"
          ></el-table-column>
          <el-table-column label="生产订单号" prop="yoCode" align="center"></el-table-column>
          <el-table-column
            label="零件号"
            prop="partCode"
            align="center"
          ></el-table-column>
          <el-table-column
            label="零件名称"
            prop="partName"
            align="center"
          ></el-table-column>
          <el-table-column label="物料编码" prop="plateGoodsCode" align="center"></el-table-column>
          <el-table-column
            label="生产标识"
            prop="productIdent"
            align="center"
          ></el-table-column>
          <el-table-column
            label="生产数量"
            prop="ypQty"
            align="center"
          ></el-table-column>
          <el-table-column
            label="物料需求量(克)"
            prop="plateSingleQuotaTotal"
            align="center"
          ></el-table-column>
          <el-table-column label="计划下达时间" prop="releaseDate" align="center"></el-table-column>

          <!-- <el-table-column label="计划生产时间" prop="createTime" align="center"></el-table-column> -->
        </el-table>
        <div
          v-show="totalNum"
          style="
            display: flex;
            justify-content: flex-end;
            align-items: center;
            margin: 20px 20px 0 0;
          "
        >
          合计：{{ totalNum }}
        </div>
      </div>
    </div>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { getListlistNoFinished,getAddRequireList,getWorkCenter } from '@/api/productionManagement/coatingMaterial';
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      loading: false,
      submitForm: {},
      searchForm: {
        materalType: 1,
      },
      labelValue: '金钾',
      tableData: [],
      totalNum: null,
      selectionList: [],
      workList:[]
    };
  },
  mounted() {
    this.getWorkCenterList()
  },
  methods: {
    getWorkCenterList() { 
      getWorkCenter().then((res) => { 
        this.workList = res.data.data
      })
    },
    // 切换金钾/银钾类型
    changeType(val) {
      console.log('val-------------');
      if (val.target.value == 1) {
        this.labelValue = '金钾';
      } else {
        this.labelValue = '银钾';
      }
    },
    // 点击查询按钮
    async onSearch() {
      this.loading = true;
      try {
        // const params = { ...this.submitForm, ...this.query };
        console.log('params', this.searchForm);
        let params = {
          ...this.searchForm,
          overHalfMon:this.searchForm.overHalfMon ? 1 : 0,
          current:1,
          size:99999
        }
        const res = await getAddRequireList( params);
        this.tableData = res.data.data.records;
        console.log('res', res);
        // if (res && res.code === 200) {
        //   this.data = res.data?.data?.records || [];
        //   this.$refs?.multipleTable?.clearSelection();
        // } else {
        //   console.warn('查询失败:', '未知错误');
        // }
      } finally {
        this.loading = false; 
      }
    },
    // 多选
    handleSelectionChange(val) {
      this.selectionList = val;
      console.log('lisy-------------', this.selectionList);
      var strarr = [];
      for (let i in this.selectionList) {
        strarr.push(Number(this.selectionList[i]['plateSingleQuotaTotal']));
      }
      var total = eval(strarr.join('+'));
      this.totalNum = total;
      this.submitForm.goodsQuantity = total;
    },
    closeDialog() {
      this.$emit('closeDialog');
    },
    submit() {
      // this.submitForm
      //this.selectionList
      const form = {
        data: this.submitForm,
        list: this.selectionList,
      };
      console.log(form);
    },
  },
};
</script>

<style></style>
