<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <!-- <el-button type="primary" icon="el-icon-plus" @click="handleDelete">申报
            </el-button> -->
      </template>
      <template #menu-right> </template>
      <template #menu="scope">
        <el-button type="text" @click="detailsView(scope.row)">详情 </el-button>
        <el-button type="text" @click="handle(scope.row)" v-if="scope.row.tbStatus == 0"
          >处理
        </el-button>
      </template>
    </avue-crud>
    <!-- 处理 -->
    <inPlantProcessHandle
      v-if="showHandle"
      :showDialog="showHandle"
      :moldAddMore="moldAddMore"
      :rowData="rowData"
      @closeDialog="cancel()"
    ></inPlantProcessHandle>
    <!-- 详情 -->
    <inPlantProcessDetails
      v-if="showDetails"
      :showDialog="showDetails"
      :rowData="rowData"
      @closeDialog="cancel()"
    ></inPlantProcessDetails>
  </basic-container>
</template>
<script>

import { pageList, detailItem, handleOemTaskBillItem, removeItem } from '@/api/oem/inplan';
import inPlantProcessHandle from './components/inPlantProcessHandle.vue';
import inPlantProcessDetails from './components/inPlantProcessDetails.vue';
export default {
  components: {
    inPlantProcessHandle,
    inPlantProcessDetails,
  },
  data() {
    return {
      loading: false,
      selectionList: [],
      option: {
        height: 'auto',
        calcHeight: 32,
        tip: false,
        // size: 'medium',
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: false,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtn: false,
        editBtnText: '修改',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        viewBtnText: '详情',
        labelWidth: 120,
        menuWidth: 120,
        dialogWidth: 1040,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: true,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        showOverflowTooltip: true,
        menuAlign: 'left',
        gridBtn: false,
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        searchMenuPosition: 'right',
        align: 'center',
        menu: true,
        column: [
         {
            label: '外协厂商',
            prop: 'ocId',
             search: true,
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'left',
            type: 'select',
            dicUrl:"/api/blade-desk/BA/Oem/listForSelect",
            props:{
              label:"ocName",
              value:"id",
              res:"data"
            },
            formatter: (row, value, label, column) => {
              return row.oemName || value || '-';
            }
          },
          {
            label: '任务号',
            prop: 'tbCode',
            sortable: true,
            filter: true,
            span: 12,
            search: true,
            display: false,
            headerAlign: 'center',
            align: 'left',
            searchLabelWidth: 60,
          },
          {
            label: '产线',
            prop: 'bsWorkCenter.wcName',
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'left',
            searchLabelWidth: 45,
          },
          {
            label: '确认编号',
            prop: 'confirmCondition',
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'left',
          },
          {
            label: '确认到期日期',
            prop: 'lastTime',
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '提醒天数',
            prop: 'remDays',
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
          {
            label: '工艺员',
            prop: 'proManName',
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
            searchLabelWidth: 60,
            dicUrl: '/blade-system/user/page?current=1&&size=99999',
            props: {
              label: 'name',
              value: 'id',
              res: 'data.records',
            },
          },
          {
            label: '备注',
            prop: 'remarks',
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'left',
          },
          {
            label: '状态',
            prop: 'tbStatusName',
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            headerAlign: 'center',
            align: 'center',
          },
        ],
      },
      form: {},
      data: [],
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      rowData: {},
      showHandle: false,
      moldAddMore: true,
      showDetails: false,
    };
  },
  methods: {
    // 查看详情
    detailsView(row) {
      console.log(row, 'row');
      this.showDetails = true;
      this.rowData = row;
      this.moldAddMore = false;
    },
    handle(row) {
      this.rowData = row;
      this.showHandle = true;
    },
    cancel() {
      this.showHandle = false;
      this.showDetails = false;

      this.onLoad(this.page, this.query);
    },
    // 删除单行
    rowDel(row, index, done) {
      console.log('【删除行】row:', row);
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        return removeItem(String(row.id)).then(() => {
          this.$message.success('删除成功');
          this.onLoad(this.page);
          done();
        });
      }).catch(error => {
        console.error('删除失败:', error);
        this.$message.error('删除失败，请稍后重试');
      });
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      pageList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        console.log('API返回数据:', res);

        // 检查返回的数据结构
        if (res.data && res.data.code === 200) {
          const data = res.data.data;
          this.page.total = data.total || 0;
          this.data = data.records || [];
          console.log('设置的数据:', { total: this.page.total, data: this.data });
        } else {
          this.$message.error(res.data.msg || '获取数据失败');
          this.data = [];
        }
        this.loading = false;

      }).catch(error => {
        console.error('获取数据失败:', error);
        this.$message.error('获取数据失败');
        this.data = [];
        this.loading = false;
      });

      setTimeout(() => {
        this.selectionClear();
      }, 500);
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page, this.query);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page, this.query);
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
  },
};
</script>
<style lang="scss" scoped></style>
