<template>
  <div>
    <el-form ref="form" :model="form" label-width="120px">
      <headerTitle title="基本信息">
        <!-- <el-button
                  size="small"
                  type="primary"
                  icon="el-icon-s-grid"
                  class="ele-btn-icon"
                  @click="exportTable"
                >
                 导出
                </el-button> -->
      </headerTitle>
      <el-row>
        <el-col :span="12" style="height: 40px">
          <el-form-item label="需求单名称：" prop="requirementName">
            <el-link
              type="primary"
              :underline="false"
              @click="openDetail(form)"
            >
              {{ form.requirementName }}
            </el-link>
            <!-- {{ form.requirementCode }} -->
          </el-form-item>
        </el-col>
        <el-col :span="12" style="height: 46px">
          <el-form-item label="计划单名称：" prop="planName">
            {{ form.planName }}
            <!-- {{ form.requirementCode }} -->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="负责人：" prop="responsibleName">
            {{ form.responsibleName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="需求类型：" prop="sourceTypeName">
            {{ form.sourceTypeName }}
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="需求部门：" prop="requireDeptName">
            {{ form.requireDeptName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="需求人：" prop="requireUserName">
            {{ form.requireUserName }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="remark" label="是否接受拆单：">
            {{ form.acceptUnpack === 1 ? '接受' : '不接受' }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="remark" label="是否需要核价：">
            {{ form.acceptUnpack === 1 ? '是' : '否' }}
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item
            label="用途"
            prop="requireUserName"
            style="margin-bottom: 22px"
          >
            {{ form.requireUserName }}
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
          <el-form-item label="完结日期：" prop="receiveDate">
            {{ form.receiveDate }}
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item prop="remark" label="备注：">
            {{ form.remark }}
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item prop="files" label="附件：">
            <fileMain v-model="form.files" type="view"></fileMain>

          </el-form-item>
          
      
        </el-col>
      </el-row>
      <headerTitle title="计划清单" style="margin-top: 15px"></headerTitle>
      <el-tabs v-model="activeName" style="margin-top: 15px" type="border-card">
        <el-tab-pane label="物品清单" name="1">
          <ele-pro-table
            ref="table"
            :needPage="false"
            :columns="columns"
            :toolkit="[]"
            :datasource="form.detailList"
            row-key="id"
            height="500px"
          >
            <template v-slot:totalCount="scope">
              <el-input-number
                :controls="false"
                :min="1"
                style="width: 100%"
                v-model="scope.row.totalCount"
              ></el-input-number>
            </template>
            <template v-slot:expectReceiveDate="scope">
              <div v-if="scope.row.arrivalWay == 1">
                {{ scope.row.expectReceiveDate }}
              </div>
              <div v-if="scope.row.arrivalWay == 2">
                <el-link
                  type="primary"
                  :underline="false"
                  @click.native="handleMethod(scope.row)"
                >
                查看分批时间
                </el-link>
              </div>
            </template>
            <template v-slot:technicalDrawings="{ row }">
              <fileMain v-model="row.technicalDrawings" type="view"></fileMain>
            </template>
            <template v-slot:files="{ row }">
              <fileMain v-model="row.files" type="view"></fileMain>
            </template>
          </ele-pro-table>
        </el-tab-pane>
        <el-tab-pane
          label="带料清单"
          name="2"
          v-if="['3', '4', '5', '6', '7'].includes(form.sourceType)"
        >
          <ele-pro-table
            ref="table"
            :needPage="false"
            :columns="columns"
            :toolkit="[]"
            :datasource="form.rawDetailList"
            row-key="id"
          >
            <template v-slot:totalCount="scope">
              <el-input-number
                :controls="false"
                :min="1"
                style="width: 100%"
                v-model="scope.row.totalCount"
              ></el-input-number>
            </template>
          </ele-pro-table>
        </el-tab-pane>
        <el-tab-pane
          label="产出品清单"
          name="3"
          v-if="['3', '4', '5', '6', '7'].includes(form.sourceType)"
        >
          <ele-pro-table
            ref="table"
            :needPage="false"
            :columns="columns"
            :toolkit="[]"
            :datasource="form.outputDetailList"
            row-key="id"
          >
            <template v-slot:totalCount="scope">
              <el-input-number
                :controls="false"
                :min="1"
                style="width: 100%"
                v-model="scope.row.totalCount"
              ></el-input-number>
            </template>
          </ele-pro-table>
        </el-tab-pane>
      </el-tabs>
    </el-form>
    <timeDialog ref="timeDialogRef" :view="true"></timeDialog>
  </div>
</template>

<script>
  import { getplanDetail } from '@/api/bpm/components/purchasingManage/purchasePlanManage';

  import { getFile } from '@/api/system/file';
  import dictMixins from '@/mixins/dictMixins';
  import { getInventoryTotalAPI } from '@/api/bpm/components/wms';
  // import fileMain from '@/components/addDoc/index.vue';
  import timeDialog from '@/components/timeDialog/index.vue';

  export default {
    mixins: [dictMixins],
    components: { 
      // fileMain,
      timeDialog },
    props: {
      taskDefinitionKey: {
        type: String,
        default: 'starter'
      },
      businessId: {
        default: ''
      }
    },
    data() {
      return {
        activeName: '1',
        visible: false,
        detailId: '',
        title: '详情',
        row: {},
        form: {
          files: []
        },
        columns: [
          {
            width: 45,
            type: 'index',
            columnKey: 'index',
            align: 'center',
            fixed: 'left'
          },
          {
            width: 150,
            prop: 'productCategoryName',
            label: '分类',
            align: 'center',
            slot: 'productCategoryName'
          },
          {
            width: 140,
            prop: 'productCode',
            label: '编码',
            align: 'center',
            slot: 'productCode'
          },
          {
            width: 240,
            prop: 'productName',
            label: '名称',
            align: 'center',
            slot: 'productName'
          },
          {
            width: 110,
            prop: 'batchNo',
            label: '批次号',
            slot: 'batchNo',
            align: 'center'
          },
          {
            prop: 'provenance',
            label: '产地',
            slot: 'provenance',
            align: 'center',
            minWidth: 200,
            showOverflowTooltip: true,
            formatter: (row, column) => {
              return row.provenance && row.provenance.length
                ? row.provenance
                    .map((item) => this.getDictValue('产地', item))
                    .join(',')
                : '';
            }
          },
          {
            width: 150,
            prop: 'taskName',
            label: '工序',
            slot: 'taskName',
            align: 'center'
          },
          {
            width: 150,
            prop: 'productBrand',
            label: '牌号',
            align: 'center',
            slot: 'productBrand'
          },
          {
            width: 100,
            prop: 'totalCount',
            label: '数量',
            align: 'center',
            slot: 'totalCount'
          },
          {
            width: 80,
            prop: 'availableCountBase',
            label: '库存数量',
            align: 'center',
            slot: 'availableCountBase'
          },
          {
            width: 120,
            prop: 'totalWeight',
            label: '重量',
            slot: 'totalWeight',
            align: 'center'
          },
          {
            width: 100,
            prop: 'measuringUnit',
            label: '单位',
            align: 'center',
            slot: 'measuringUnit'
          },
          {
            width: 130,
            prop: 'modelType',
            label: '型号',
            align: 'center',
            slot: 'modelType'
          },

          {
            width: 120,
            prop: 'specification',
            label: '规格',
            align: 'center',
            slot: 'specification'
          },
          {
            width: 150,
            prop: 'arrivalWay',
            label: '到货方式',
            formatter: (row, column, cellValue) => {
              return cellValue == 1 ? '一次性到货' : '分批到货';
            },
            align: 'center'
          },
          {
            width: 170,
            prop: 'receiveDate',
            label: '到货日期',
            slot: 'expectReceiveDate',
            align: 'center'
          },

          {
            width: 130,
            prop: 'supplierName',
            label: '供应商',
            slot: 'supplierName',
            headerSlot: 'headerSupplierName',
            align: 'center'
          },
          {
            width: 120,
            prop: 'packingSpecification',
            align: 'center',
            label: '包装规格',
            showOverflowTooltip: true
          },
          {
            width: 160,
            prop: 'technicalDrawings',
            label: '图纸附件',
            slot: 'technicalDrawings',
            align: 'center'
          },
          {
            width: 140,
            prop: 'files',
            label: '附件',
            slot: 'files',
            align: 'center'
          },
          {
            minWidth: 220,
            prop: 'remark',
            label: '备注',
            align: 'center',
            slot: 'remark'
          }
        ]
      };
    },
    created() {
      this.requestDict('产地');

      this.getPlanData(this.businessId);
    },
 
    methods: {
      downloadFile(file) {
        getFile({ objectName: file.storePath }, file.name);
      },
      async getPlanData(id) {
        this.loading = true;
        const data = await getplanDetail(id);
        this.loading = false;
        if (data) {
          let codeList = data.detailList.map((item) => item.productCode);
          let inventoryTotalList = await getInventoryTotalAPI(codeList);
          data.detailList.forEach((item) => {
            let find =
              inventoryTotalList.find((key) => key.code == item.productCode) ||
              {};
            item.availableCountBase = find.availableCountBase;
          });
          this.form = data;
          // if (data.files && data.files.length > 0) {
          //   this.form.files = data.files[0];
          // } else {
          //   this.form.files = null;
          // }
        }
      },
      handleMethod(row) {
        this.$refs.timeDialogRef.open(row);
      },
      //查看详情
      openDetail(row) {
        this.$refs.contactDetailDialogRef.open(row);
      },
      async getTableValue() {
        try {
          if (this.form.detailList.some((item) => !!item.totalCount == '')) {
            this.$message.warning('数量不能为空');
            return '';
          }
          // 表单验证通过，执行保存操作
          this.loading = true;
          this.form.detailList = [
            ...this.form.detailList,
            ...this.form.rawDetailList,
            ...this.form.outputDetailList
          ];
          return Object.assign({}, this.form);
        } catch (error) {
          console.log(error);
          // 表单验证未通过，不执行保存操作
        }
      }
    }
  };
</script>

<style scoped lang="scss">
  .ele-dialog-form {
    .el-form-item {
      margin-bottom: 10px;
    }
  }

  .headbox {
    display: flex;
    justify-content: space-between;
    align-items: center;

    .amount {
      font-size: 14px;
      font-weight: bold;
    }
  }
</style>
