<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <order-search @search="reload" ref="searchRef"> </order-search>
      <!--      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="未完成工单" name="first"></el-tab-pane>
        <el-tab-pane label="已完成工单" name="second"></el-tab-pane>
      </el-tabs> -->
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :columns="columns"
        :datasource="datasource"
        cache-key="workOrderTable"
      >
        <template v-slot:code="{ row }">
          <!-- <el-link type="primary" :underline="false" @click="goDetail(row)"> -->
          {{ row.code }}
          <!-- </el-link> -->
        </template>
        <template v-slot:status="{ row }">
          <span :class="{ 'ele-text-danger': row.status == 3 }">
            {{ statusFormatter(row.status) }}
          </span>
        </template>
        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-truck"
            @click="toRelease(row)"
            v-if="row.status == 8"
          >
            下达
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>

    <el-dialog :visible.sync="visible" title="选择班组" width="400px">
      <el-select v-model="releasParams.teamId">
        <el-option
          v-for="item in teamList"
          :value="item.id"
          :key="item.id"
          :label="item.name"
        ></el-option>
      </el-select>

      <div class="footer" slot="footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { getList, releaseWorkOrder } from '@/api/workOrder/index.js';
  import { teamPage } from '@/api/mainData/index.js';
  import OrderSearch from './components/order-search.vue';
  export default {
    components: {
      OrderSearch
    },
    data () {
      return {
        visible: false,
        loading: false,
        releasParams: {
          teamId: '',
          id: ''
        },
        teamList: [],

        statusOpt: [
          { label: '待生产', value: 4 },
          { label: '生产中', value: 5 },
          { label: '待下达', value: 8 }
        ],

        current: null,

        
      };
    },
    computed: {
      // 表格列配置
      columns () {
        return [
          {
            columnKey: 'index',
            label: '序号',
            type: 'index',
            width: 55,
            align: 'center',
            showOverflowTooltip: true,
            fixed: 'left'
          },
          {
            slot: 'code',
            label: '生产订单号',
            align: 'center',
            minWidth: 110
          },
          {
            prop: 'productionPlanCode',
            label: '计划编号',
            align: 'center'
          },
          {
            prop: 'produceRoutingName',
            label: '工艺路线',
            align: 'center'
          },
          {
            prop: 'productCode',
            label: '物料编号',
            align: 'center'
          },
          {
            prop: 'productName',
            label: '产品名称',
            align: 'center'
          },
          {
            prop: 'brandNo',
            label: '牌号',
            align: 'center'
          },
          {
            prop: 'model',
            label: '型号',
            align: 'center'
          },
          {
            prop: 'formingNum',
            label: '要求生产数量',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'formingWeight',
            label: '要求生产重量',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'planStartTime',
            label: '计划开始时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },

          {
            prop: 'planCompleteTime',
            label: '计划结束时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },

          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            slot: 'status',
            label: '状态',
            align: 'center',
            formatter: (row) => {
              const obj = this.statusOpt.find((i) => i.value == row.status);
              return obj && obj.label;
            }
          },

          
          {
            prop: 'serialNo',
            label: '客户代号',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'simpleName',
            label: '客户简称',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            columnKey: 'action',
            label: '操作',
            width: 90,
            align: 'center',
            resizable: false,
            fixed: 'right',
            slot: 'action',
            showOverflowTooltip: true
          }
        ];
      }
    },
    created () {
    
    },
    methods: {
      statusFormatter (status) {
        const obj = this.statusOpt.find((i) => i.value == status);
        return obj && obj.label;
      },
      /* 表格数据源 */
      datasource ({ page, limit, where }) {
        if (where.status) {
          where.statusList = [];
          where.statusList.push(where.status);
        }
        return getList({
          pageNum: page,
          size: limit,
          ...where
        });
      },
      async _teamPage () {
        const res = await teamPage({ page: 1, size: -1, produceVersionId: this.current.produceVersionId });

        this.teamList = res.list;
      },
      // 下达
      toRelease (row) {
        this.current = row
        this.visible = true;
        this._teamPage();
      
      },
      // 下达
      confirm () {
        if (!this.releasParams.teamId) {
          return this.$message.error('请选择班组');
        }
   

        const loading = this.$loading({ text: '加载中...' });
        releaseWorkOrder({id: this.current.id, teamId: this.releasParams.teamId})
          .then((res) => {
            if (res) {
              this.$message.success('成功');
              this.reload();
              this.visible = false;
            }
          })
          .finally(() => {
            loading.close();
          });
      },
      /* 刷新表格 */
      reload (where) {
        this.$nextTick(() => {
          this.$refs.table.reload({ page: 1, where });
        });
      }
    }
  };
</script>

<style lang="scss" scoped></style>
