<template>
  <div id="outgoingManagement_index">
    <el-card shadow="never">
      <el-form :model="formData" ref="formName" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="出库时间" prop="time">
              <el-date-picker
                class="w100"
                size="small"
                v-model="formData.time"
                type="daterange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                value-format="yyyy-MM-dd"
              ></el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="6">
            <el-form-item label="出库单号" prop="sourceBizNo">
              <el-input
                size="small"
                class="w100"
                placeholder="请输入"
                v-model="formData.sourceBizNo"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="6">
            <el-form-item label="出库场景" prop="bizType">
              <el-select
                style="width: 100%"
                filterable
                placeholder="请选择"
                v-model="formData.bizType"
                clearable
              >
                <el-option
                  v-for="item in outputSceneState"
                  :key="item.code"
                  :value="item.code + ''"
                  :label="item.label"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="6">
            <el-form-item label="物品编码" prop="categoryCode">
              <el-input
                size="small"
                class="w100"
                placeholder="请输入"
                v-model="formData.categoryCode"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="6">
            <el-form-item label="物品名称" prop="categoryName">
              <el-input
                size="small"
                class="w100"
                placeholder="请输入"
                v-model="formData.categoryName"
              ></el-input> </el-form-item
          ></el-col>

          <el-col :span="6">
            <el-form-item label="状态" prop="status">
              <el-select
                filterable
                size="small"
                class="w100"
                v-model="formData.status"
                placeholder="请选择"
                clearable
              >
                <el-option
                  v-for="item in auditStatus"
                  :key="item.code"
                  :value="item.code"
                  :label="item.label"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <!-- <el-col :span="6">
            <el-form-item label="组织机构:" prop="deptIds">
              <auth-selection
                v-model="formData.deptIds"
                style="width: 100%"
              ></auth-selection>
            </el-form-item>
          </el-col> -->
          <el-col :span="6">
            <el-form-item label="所属工厂" prop="factoryId">
              <el-select
                filterable
                placeholder="请选择"
                v-model="formData.factoryId"
                clearable
                class="w100"
                @change="getTreeData"

              >
                <el-option
                  v-for="item in factoryList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col v-bind="styleResponsive ? { lg: 6, md: 12 } : { span: 6 }">
            <el-form-item label="仓库名称" prop="warehouseId">
              <el-select
                filterable
                placeholder="请选择"
                v-model="formData.warehouseId"
                clearable
                class="w100"
              >
                <el-option
                  v-for="item in warehouseList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col
            style="
              display: flex;
              justify-content: flex-end;
              margin-bottom: 10px;
            "
          >
            <div>
              <el-button icon="el-icon-refresh-left" size="small" @click="reset"
                >重置</el-button
              >
              <el-button
                type="primary"
                icon="el-icon-search"
                size="small"
                @click="search"
                >搜索</el-button
              >
            </div></el-col
          >
        </el-row>
      </el-form>

      <ele-pro-table
        ref="table"
        :columns="columns"
        height="calc(100vh-300px)"
        :datasource="datasource"
        @cell-click="cellClick"
        :pageSize="20"
        cache-key="systemRoleTable"
        v-loading="loading"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button icon="el-icon-plus" type="primary" @click="add"
            >新建</el-button
          >
          <el-button
            icon="el-icon-receiving"
            type="primary"
            @click="putIntoStorage"
            >退货入库</el-button
          >
          <!-- <el-button
            icon="el-icon-printer"
            type="primary"
            @click="openPrint('print')"
            >打印出库单(嘉实)</el-button
          > -->
          <!-- <el-button
            icon="el-icon-printer"
            type="primary"
            @click="openPrint('print1')"
            >打印出库单(宝悦)</el-button
          > -->
          <!-- <el-button
            icon="el-icon-printer"
            type="primary"
            @click="openPrint('print2')"
            >打印产品出库通知单</el-button
          >
          <el-button
            icon="el-icon-printer"
            type="primary"
            @click="openPrint('print3')"
            >打印产品入库及发运台账</el-button
          >
          <el-button
            icon="el-icon-printer"
            type="primary"
            @click="openPrint('print4')"
            >打印产品总账</el-button
          > -->
        </template>
        <template v-slot:selection="{ row }">
          <el-radio class="radio" v-model="currentId" :label="row.id"
            ><i></i
          ></el-radio>
        </template>
        <!-- 单号链接 -->
        <template v-slot:bizNo="{ row }">
          <el-link type="primary" @click="details(row)">
            {{ row.bizNo }}
          </el-link>
        </template>
        <!-- 状态 -->
        <template v-slot:verifyStatus="{ row }">
          <span :class="status[row.verifyStatus].class">
            {{ status[row.verifyStatus].label }}
          </span>
        </template>
        <!-- 工作流审批状态 -->
        <template v-slot:flowableStatus="{ row }">
          <span :class="status[row.flowableStatus].class">
            {{ status[row.flowableStatus].label }}
          </span>
        </template>

        <!-- 领料人 -->
        <template v-slot:fromUser="{ row }">
          <div class="deviceName">
            <p class="n-p">{{ row.fromUser }}</p>
            <!-- <p class="col">出库:{{ row.createTime }}</p> -->
          </div>
        </template>

        <template v-slot:bizType="{ row }">
          {{ handleBizType(row.bizType) }}
        </template>
        <template v-slot:assetType="{ row }">
          {{ handleAssetType(row.extInfo.assetType) }}
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="success"
            :underline="false"
            icon="el-icon-position"
            v-if="row.verifyStatus == 0 || row.verifyStatus == 3"
            @click="submit(row)"
          >
            提交
          </el-link>
          <el-link
            type="warning"
            :underline="false"
            icon="el-icon-edit"
            v-if="row.verifyStatus == 0 || row.verifyStatus == 3"
            @click="edit(row)"
          >
            修改
          </el-link>
          <!-- <el-link
            type="primary"
            :underline="false"
            icon="el-icon-view"
            @click="details(row)"
          >
            详情
          </el-link> -->
          <el-link
            type="primary"
            v-if="row.verifyStatus == 0 || row.verifyStatus == 3"
            :underline="false"
            icon="el-icon-delete"
            @click="deleted(row)"
          >
            删除
          </el-link>
          <!-- <el-link
            type="primary"
            :underline="false"
            icon="el-icon-delete"
            @click="print(row)"
          >
            打印出库单
          </el-link> -->
        </template>
      </ele-pro-table>
    </el-card>
    <outboundOrderPrint ref="outboundOrderPrintRef" />
    <print ref="printRef"></print>
    <print1 ref="print1Ref"></print1>
    <print2 ref="print2Ref"></print2>
    <print3 ref="print3Ref"></print3>
    <print4 ref="print4Ref"></print4>
  </div>
</template>

<script>
  import storageApi from '@/api/warehouseManagement';
  import print from './components/print.vue';
  import print1 from './components/print1.vue';
  import print2 from './components/print2.vue';
  import print3 from './components/print3.vue';
  import print4 from './components/print4.vue';
  import { allCategoryLevel } from '@/api/classifyManage';
  import outin from '@/api/warehouseManagement/outin';
  import warehouseDefinition from '@/api/warehouseManagement/warehouseDefinition';
  import outboundOrderPrint from '../components/outboundOrderPrint.vue';
  import { auditStatus, useDict, outputSceneState } from '@/utils/dict/index';

  // import { getPage } from '@/api/stockManagement'
  // import { deepClone } from '@/utils'
  export default {
    components: { outboundOrderPrint, print, print1, print2, print3, print4 },
    data() {
      return {
        currentId: '',
        currentRow: {},
        auditStatus,
        outputSceneState,
        codeList: [],
        factoryList: [],
        warehouseList: [],
        tableData: [],
        fromUserList: [],
        loading: false,
        formData: {
          deptIds: '',
          categoryCode: '',
          categoryName: '',
          sourceBizNo: '',
          startTime: '',
          endTime: '',
          status: '',
          factoryId: '',
          time: [],
          warehouseId: ''
        },
        total: 0,
        status: [
          { label: '未审核', class: 'ele-text-info' },
          { label: '审核中', class: 'ele-text-primary' },
          { label: '审核通过', class: 'ele-text-success' },
          { label: '驳回', class: 'ele-text-danger' }
        ],
        columns: [
          {
            slot: 'selection',
            label: '选择',
            width: 50,
            align: 'center'
          },
          {
            type: 'index',
            label: '序号',
            width: 50,
            align: 'center'
          },
          {
            prop: 'bizNo',
            label: '单号',
            align: 'center',
            slot: 'bizNo',
            showOverflowTooltip: true,
            width: 160
          },
           {
            prop: 'warehouseName',
            label: '仓库名称',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 100,
        
          },
          {
            prop: 'bizType',
            slot: 'bizType',
            label: '出库场景',
            align: 'center',
            showOverflowTooltip: true,
            width: 90
          },
          {
            prop: 'assetType',
            slot: 'assetType',
            label: '物品类型',
            align: 'center',
            showOverflowTooltip: true,
            width: 100
          },
          {
            prop: 'categoryNames',
            label: '物品名称',
            align: 'center',
            showOverflowTooltip: true,
            width: 150
          },
          {
            prop: 'categoryCodes',
            label: '物品编码',
            align: 'center',
            showOverflowTooltip: true
          },
          // {
          //   prop: 'verifyStatus',
          //   label: '状态',
          //   align: 'center',
          //   slot: 'verifyStatus',
          //   showOverflowTooltip: true,
          //   width: 120
          // },

          {
            prop: 'sourceBizNo',
            label: '来源单据',
            align: 'center',
            showOverflowTooltip: true,
            width: 150
          },

          {
            prop: 'fromUser',
            label: '领料人',
            align: 'center',
            slot: 'fromUser',
            showOverflowTooltip: true,
            width: 100
          },
          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            width: 200
          },
          {
            prop: 'storageTime',
            label: '出库时间',
            align: 'center',
            showOverflowTooltip: true,
            width: 200
          },
          {
            prop: 'verifyStatus',
            slot: 'verifyStatus',
            label: '状态',
            align: 'center',
            showOverflowTooltip: true,
            width: 120
          },
          {
            prop: 'verifyName',
            slot: 'verifyName',
            label: '审核人',
            align: 'center',
            showOverflowTooltip: true,
            width: 120
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 200,
            align: 'center',
            slot: 'action',
            showOverflowTooltip: true
          }
        ]
      };
    },
    created() {
      this.getUser();
      this.getTypeList();
      this.getFactoryList();
      this.getTreeData();
    },
    methods: {
      async getTreeData() {
        this.formData.warehouseId = '';
        try {
          let res = await storageApi.getWarehouseTrees({
            factoryId: this.formData.factoryId
          });
          if (res?.code === '0') {
            this.warehouseList = res.data;
          }
        } catch (error) {
          console.log(error);
        }
      },
      //获取工厂列表
      async getFactoryList() {
        const res = await warehouseDefinition.getFactoryarea({
          pageNum: 1,
          size: 9999,
          type: 1
        });
        this.factoryList = res.list;
      },
      openPrint(ref) {
        if (this.currentId) {
          this.$refs[ref + 'Ref'].open(this.currentId);
        } else {
          this.$message.warning('请选择一条单据！');
        }
      },
      putIntoStorage() {
        if (this.currentId) {
          if (this.currentRow.verifyStatus == 2) {
            this.$router.push({
              path: '/warehouseManagement/stockManagement/add',
              query: {
                detailId: this.currentId
              }
            });
          } else {
            this.$message.warning('该单据未审核通过！');
          }
        } else {
          this.$message.warning('请选择一条单据！');
        }
      },
      // 单击获取id
      cellClick(row) {
        this.currentRow = row;
        this.currentId = row.id;
      },
      print(row) {
        this.$refs.outboundOrderPrintRef.open(row.id);
      },
      handleAssetType(r) {
        const code = this.codeList.find((item) => item.dictCode == r);
        return code?.dictValue;
      },
      async getTypeList() {
        const { data } = await allCategoryLevel();
        this.codeList = data.map((item) => {
          return { dictCode: item.id, dictValue: item.name };
        });
      },
      edit(row) {
        this.$router.push({
          path: '/warehouseManagement/outgoingManagement/add',
          query: {
            detailId: row.id
          }
        });
      },
      submit(row) {
        this.$confirm('此操作将提交流程, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            this.loading = true;

            const data = await outin.outApprove({ outInId: row.id });
            this.loading = false;

            if (data.code == 0) {
              this.$message.success(data.message);
              this.getList();
            }
          })
          .catch(() => {});
      },
      handleBizType(code) {
        for (const key in this.outputSceneState) {
          if (this.outputSceneState[key].code == code) {
            return this.outputSceneState[key].label;
          }
        }
      },
      getUserName(id) {
        let obj = this.fromUserList.find((x) => x.id == id);
        return obj && obj.name;
      },
      async getUser() {
        let res22 = await warehouseDefinition.getUserPage({
          groupId: 1,
          size: 9999,
          page: 1
        });
        this.fromUserList = res22.list;
      },
      async datasource({ page, limit, where }) {
        const params = Object.assign({}, this.formData);
        if (params.time?.length) {
          params.startTime = params.time[0];
          params.endTime = params.time[1];
        }
        delete params.time;
        // params.bizStatus = 1;
        const res = await storageApi.getInboundList({
          pageNum: page,
          size: limit,
          ...params,
          type: 2
        });
        return res;
      },
      getAuditStatus: useDict(auditStatus),
      getList() {
        this.$refs.table.reload();
      },
      async deleted(row) {
        this.$confirm('是否确定删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            const data = await outin.delete([row.id]);
            if (data.code == '0') {
              this.$message.success('删除成功!');
              this.getList();
            }
          })
          .catch(() => {});
      },
      add() {
        this.$router.push({
          path: '/warehouseManagement/outgoingManagement/add'
        });
      },
      details(row) {
        this.$router.push({
          path: '/warehouseManagement/outgoingManagement/details',
          query: {
            id: row.id
          }
        });
      },
      handleCurrentChange() {
        this.getList();
      },
      handleSizeChange() {
        this.formData.page = 1;
        this.getList();
      },
      search() {
        this.formData.page = 1;
        this.getList();
      },
      reset() {
        this.$refs.formName.resetFields();
        this.search();
      }
    }
  };
</script>

<style lang="scss" scoped>
  #outgoingManagement_index {
    height: calc(100vh - 96px);
    width: 100%;
    padding: 10px;
    box-sizing: border-box;
    // element-ui样式穿透
    :deep(.el-card) {
      height: 100%;
      .el-card__body {
        height: 100%;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        .ele-pro-table {
          flex: 1;
          display: flex;
          flex-direction: column;
          .el-table {
            flex: 1;
            // display: flex;
            // flex-direction: column;
            // .el-table__body-wrapper {
            //   flex: 1;
            // }
          }
        }
      }
      .el-form-item {
        margin-bottom: 5px !important;
      }
    }
    .w100 {
      width: 100% !important;
    }
    .p20 {
      padding: 20px;
    }
    .mt20 {
      margin-top: 20px;
    }
    .mt10 {
      margin-top: 10px;
    }
    .el-form {
      overflow: hidden;
    }
    .float-right {
      float: right;
      text-align: right;
      margin-right: 20px;
    }
    .right {
      text-align: right;
    }
    .col {
      color: #aaa;
    }
    .pr10 {
      padding-right: 10px;
    }
  }
</style>
