<template>
  <div class="ele-body">
    <el-card shadow="never">
      <el-form :model="formData" ref="formName" label-width="80px">
        <el-row :gutter="10">
          <el-col :span="5">
            <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="4">
            <el-form-item label="入库单号" prop="bizNum">
              <el-input
                size="small"
                class="w100"
                placeholder="请输入"
                v-model="formData.bizNum"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="4">
            <el-form-item label="编码" prop="assetCode">
              <el-input
                size="small"
                class="w100"
                placeholder="请输入"
                v-model="formData.assetCode"
              ></el-input> </el-form-item
          ></el-col>
          <el-col :span="4">
            <el-form-item label="名称" prop="assetName">
              <el-input
                size="small"
                class="w100"
                placeholder="请输入"
                v-model="formData.assetName"
              ></el-input> </el-form-item
          ></el-col>

          <el-col :span="4">
            <el-form-item label="状态" prop="verifyStatus">
              <el-select
                filterable
                size="small"
                class="w100"
                v-model="formData.verifyStatus"
                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 :lg="6" :sm="8" :xs="8">
          <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="3">
            <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"
        :datasource="datasource"
        cache-key="systemRoleTable"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button icon="el-icon-plus" type="primary" @click="add"
            >新建</el-button
          >
        </template>
        <!-- 状态 -->
        <template v-slot:verifyStatus="{ row }">
          <span :class="status[row.verifyStatus].class">
            {{ status[row.verifyStatus].label }}
          </span>
        </template>
        <!-- 单号链接 -->
        <template v-slot:bizNo="{ row }">
          <el-link @click="details(row)">
            {{ row.bizNo }}
          </el-link>
        </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 == 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>
        </template>
        <template v-slot:bizType="{ row }">
          {{ handleBizType(row.bizType) }}
        </template>
      </ele-pro-table>
    </el-card>
  </div>
</template>

<script>
  import outin from '@/api/warehouseManagement/outin';

  import {
    warehousingType,
    sceneState,
    auditStatus,
    useDict
  } from '@/utils/dict/index';
  // import { getPage } from '@/api/stockManagement'
  // import { deepClone } from '@/utils'
  export default {
    //   components: { tablePagination },
    data() {
      return {
        auditStatus,
        warehousingType,
        sceneState,
        tableData: [],
        formData: {
          bizNum: '',
          deliveryName: '',
          time: [],
          verifyStatus: ''
          // type: 1
        },
        total: 0,
        status: [
          { label: '未审核', class: 'ele-text-info' },
          { label: '审核中', class: 'ele-text-primary' },
          { label: '审核通过', class: 'ele-text-success' },
          { label: '驳回', class: 'ele-text-danger' }
        ],

        columns: [
          {
            type: 'index',
            label: '序号',
            width: 50,
            align: 'center'
          },
          {
            prop: 'bizNo',
            label: '单号',
            align: 'center',
            slot: 'bizNo',
            showOverflowTooltip: true,
            width: 150
          },
          {
            prop: 'bizType',
            slot: 'bizType',
            label: '入库类型',
            align: 'center',
            showOverflowTooltip: true,
            width: 150
          },
          {
            prop: 'extInfo.documentSource',
            label: '来源单据',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'createUserName',
            label: '入库人',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            width: 160,
            prop: 'createTime',
            label: '入库时间',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'verifyName',
            label: '审核人',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'verifyStatus',
            label: '状态',
            align: 'center',
            slot: 'verifyStatus',
            showOverflowTooltip: true,
            width: 100
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 250,
            align: 'center',
            slot: 'action',
            showOverflowTooltip: true
          }
        ]
      };
    },
    created() {},
    methods: {
      submit(row) {
        this.$confirm('此操作将提交流程, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            const data = await outin.outApproves({ outInId: row.id });
            if (data) {
              this.$message.success('流程发起成功！');
              this.getList();
            }
          })
          .catch(() => {});
      },
      handleBizType(code) {
        for (const key in this.sceneState) {
          if (this.sceneState[key].code == code) {
            return this.sceneState[key].label;
          }
        }
      },
      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 outin.list({
          pageNum: page,
          size: limit,
          type: 1,
          ...params,
          bizStatus: 1
        });

        return res;
      },
      getAuditStatus: useDict(auditStatus),
      getList() {
        this.$refs.table.reload();
      },
      add(row) {
        const query = {};
        if (row?.id) {
          query.id = row.id;
        }
        this.$router.push({
          path: '/warehouseManagement/stockManagement/add'
          // query
        });
      },
      edit(row) {
        this.$router.push({
          path: '/warehouseManagement/stockManagement/edit',
          query: {
            id: row.id
          }
        });
      },
      details(row) {
        this.$router.push({
          path: '/warehouseManagement/stockManagement/details',
          query: {
            id: row.id
          }
        });
      },
      async deleted(row) {
        this.$confirm('是否确定删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          console.log('删除了')
        }).catch(() => {});
      },
      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>
  .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>
