<template>
  <div class="chooseUser-container">
    <el-dialog v-model="isShowDialog" width="80%" draggable="" :before-close="closeDialog" destroy-on-close :fullscreen="fullScreen">
      <template #header>
        <div style="color: #fff;display: flex;align-items: center;justify-content: space-between;">
          <span>{{ props.title }}</span>
          <el-icon size="16" @click="fullScreen = !fullScreen" style="margin-right: 16px; display: block; vertical-align: middle;cursor: pointer">
            <ele-FullScreen v-if="!fullScreen" />
            <ele-Notification v-else />
          </el-icon>
        </div>
      </template>
      <el-row :gutter="10" style="height: 100%;">
        <el-col :span="4">
          <treeFilter ref="orgTreeRef" @node-click="nodeClick" />
        </el-col>
        <el-col :span="20" style="display: flex;flex-direction: column;">
          <el-card shadow="hover" :body-style="{ paddingBottom: '0' }">
            <el-form :model="queryParams" ref="queryForm" :inline="true">
              <el-form-item label="产线编码">
                <el-input v-model="queryParams.productionLineCode" clearable="" placeholder="请输入产线编码" />
              </el-form-item>
              <el-form-item label="产线名称">
                <el-input v-model="queryParams.productionLineName" clearable="" placeholder="请输入产线名称" />
              </el-form-item>
              <el-form-item label="手机号码">
                <el-input v-model="queryParams.phone" placeholder="手机号码" clearable />
              </el-form-item>
              <el-form-item label=" 产线类别">
                <el-select clearable="" filterable="" v-model="queryParams.productionLineTypeCode"
                           placeholder="请选择 产线类别">
                  <el-option v-for="(item, index) in  mesProductionLineTypeProductionLineTypeCodeDropdownList"
                             :key="index" :value="item.value" :label="item.label" />

                </el-select>

              </el-form-item>
              <el-form-item label="所属工厂编码">
                <el-select clearable="" filterable="" v-model="queryParams.factoryCode" placeholder="请选择所属工厂编码">
                  <el-option v-for="(item, index) in  mesWorkShopFactoryCodeDropdownList" :key="index"
                             :value="item.value" :label="item.label" />

                </el-select>
              </el-form-item>
              <el-form-item label="公休日类型">
                <el-select clearable="" v-model="queryParams.holidayType" placeholder="请选择公休日类型">
                  <el-option v-for="(item, index) in dl('HolidayType')" :key="index" :value="item.code"
                             :label="`[${item.code}] ${item.value}`" />

                </el-select>

              </el-form-item>
              <el-form-item>
                <el-button-group>
                  <el-button type="primary" icon="ele-Search" @click="handleQuery" v-auth="'sysUser/page'"> 查询
                  </el-button>
                  <el-button icon="ele-Refresh" @click="() => queryParams = {}"> 重置 </el-button>
                </el-button-group>
              </el-form-item>
            </el-form>
          </el-card>
          <el-card class="full-table" shadow="hover" style="margin-top: 8px">
            <el-table :data="tableData" ref="tableRef" style="width: 100%" v-loading="loading"
                      tooltip-effect="light" row-key="id" border="" @selection-change="onSelectionChange"  :expand-row-keys="expandRowKeys" @expand-change="expandChange" >
              <el-table-column type="selection" :reserve-selection="true" v-if="props.isAny" width="55" align="center" />
              <el-table-column type="expand">
                <template #default="scope">
                  <el-table :data="scope.row.list || []" style="width: 100%" v-loading="loading" tooltip-effect="light"
                            row-key="id" border="">
                    <el-table-column type="index" label="序号" width="55" align="center"/>
                    <el-table-column prop="partCode" label="零部件编码" />
                    <el-table-column prop="partName" label="零部件名称" />
                  </el-table>
                </template>
              </el-table-column>
              <el-table-column type="index" label="序号" width="55" align="center" />
              <el-table-column prop="productionLineCode" label="产线编码" show-overflow-tooltip="" />
              <el-table-column prop="productionLineName" label="产线名称" show-overflow-tooltip="" />
              <el-table-column prop="productionLineTypeCode" label=" 产线类别" show-overflow-tooltip="">
                <template #default="scope">
                  <span>{{ scope.row.productionLineTypeCodeProductionLineTypeName }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="factoryCode" label="所属工厂" show-overflow-tooltip="">
                <template #default="scope">
                  <span v-show="scope.row.factoryCode">[{{ scope.row.factoryCode }}]</span><span>{{ scope.row.factoryName }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="workShopCode" label="所属车间" show-overflow-tooltip="">
                <template #default="scope">
                  <span v-show="scope.row.workShopCode">[{{ scope.row.workShopCode }}]</span><span>{{ scope.row.workShopName  }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="holidayType" label="公休日类型" show-overflow-tooltip="">
                <template #default="scope">
                  <el-tag :type="di('HolidayType', scope.row.holidayType)?.tagType"> {{ di("HolidayType",
                      scope.row.holidayType)?.value }} </el-tag>
                </template>
              </el-table-column>
              <el-table-column prop="remark" label="备注" show-overflow-tooltip="" />
              <el-table-column prop="orderNo" label="排序" width="70" align="center" show-overflow-tooltip />
              <el-table-column label="操作" width="140" align="center" fixed="right"
                               show-overflow-tooltip="" v-if="!props.isAny">
                <template #default="scope">
                  <el-button icon="ele-Edit" size="small" text="" type="primary"
                             @click="selectOne(scope.row)"> 选择 </el-button>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination v-model:currentPage="tableParams.page" v-model:page-size="tableParams.pageSize"
                           :total="tableParams.total" :page-sizes="[10, 20, 50, 100, 200, 500]" small="" background=""
                           @size-change="handleSizeChange" @current-change="handleCurrentChange"
                           layout="total, sizes, prev, pager, next, jumper" />


          </el-card>
        </el-col>
      </el-row>
      <template #footer v-if="props.isAny">
                <span class="dialog-footer">
                    <el-button @click="cancel">取 消</el-button>
                    <el-button type="primary" @click="confirm">确 定（{{ selectionNum }}条）</el-button>
                </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup="" name="mesMaterial">
import { ref } from "vue";
import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
import treeFilter from './components/productionLineTree.vue'
import {
  getMesProductionLineTypeProductionLineTypeCodeDropdown, getMesWorkShopFactoryCodeDropdown, getPartList,
  pageMesProductionLine
} from "/@/api/mes/mesProductionLine";

var props = defineProps({
  title: {
    type: String,
    default: "",
  },
  isAny: {
    type: Boolean,
    default: false,
  },
});

const showAdvanceQueryUI = ref(true);
const loading = ref(false);
const tableData = ref<any>([]);
const queryParams = ref<any>({});
const tableParams = ref({
  page: 1,
  pageSize: 10,
  total: 0
});
let fullScreen = ref(false);
const tableRef = ref<any>();
const isShowDialog = ref(false);
const options = ref<any>({});
//已选择的条数
const selectionNum = ref<number>(0);

//父级传递来的函数，用于回调
const emit = defineEmits(["finish"]);

// 打开弹窗
const openDialog = async (_options: any) => {
  queryParams.value = _options || {}
  handleQuery(true);
  isShowDialog.value = true;
};

// 关闭弹窗
const closeDialog = () => {
  isShowDialog.value = false;
  tableRef.value.clearSelection();
};

// 取消
const cancel = () => {
  isShowDialog.value = false;
  tableRef.value.clearSelection();
};

//确定
const confirm = () => {
  emit("finish", { rows: tableRef.value.getSelectionRows() });
  isShowDialog.value = false;
}
// 选择一个
const selectOne = (row: any) => {
  emit("finish", row);
  isShowDialog.value = false;
}

// 选择行变更
const onSelectionChange = () => {
  selectionNum.value = tableRef.value.getSelectionRows().length;
}

// 查询操作
const handleQuery = async (first?: boolean) => {
  loading.value = true;
  expandRowKeys.value = [];
  let params = Object.assign(Object.assign(queryParams.value, tableParams.value));
  var res = await pageMesProductionLine(params);
  tableData.value =  (res.data.result?.items ?? []).map((item) => {
    return {
      ...item,
      list: []
    };
  });
  tableParams.value.total = <number>res.data.result?.total || 0;
  loading.value = false;
};

// 改变页面容量
const handleSizeChange = (val: number) => {
  tableParams.value.pageSize = val;
  handleQuery();
};

// 改变页码序号
const handleCurrentChange = (val: number) => {
  tableParams.value.page = val;
  handleQuery();
};

// 树组件点击
const nodeClick = async (node: any) => {
  delete queryParams.value.factoryCode;
  delete queryParams.value.workShopCode;
  if(node.sign==1){
    queryParams.value.factoryCode = node.code;
  }else if(node.sign==2){
    queryParams.value.workShopCode = node.code;
  }
  handleQuery();
};

//将属性或者函数暴露给父组件
defineExpose({ openDialog });

const mesWorkShopFactoryCodeDropdownList = ref<any>([]);
const getMesWorkShopFactoryCodeDropdownList = async () => {
  let list = await getMesWorkShopFactoryCodeDropdown();
  mesWorkShopFactoryCodeDropdownList.value = list.data.result ?? [];
};
getMesWorkShopFactoryCodeDropdownList();


const mesProductionLineTypeProductionLineTypeCodeDropdownList = ref<any>([]);
const getMesProductionLineTypeProductionLineTypeCodeDropdownList = async () => {
  let list = await getMesProductionLineTypeProductionLineTypeCodeDropdown();
  mesProductionLineTypeProductionLineTypeCodeDropdownList.value = list.data.result ?? [];
};
getMesProductionLineTypeProductionLineTypeCodeDropdownList();



const expandRowKeys = ref([]);
const expandChange = async(row: any, expandedRows: any) => {
  if (expandedRows.length) {
    expandRowKeys.value = []
    if (row) {
      expandRowKeys.value = expandedRows.map(item => item.id)
       await getPartList(row.id).then(res => {
        row.list = res.data.result ?? [];
      })
    }
  } else {
    expandRowKeys.value = []
  }
}



</script>
<style scoped>
:deep(.el-ipnut),
:deep(.el-select),
:deep(.el-input-number) {
  width: 100%;
}
</style>
