<template>
  <div class="pageIndexClass" style="font-size:12px !important">
    <div>
      <byTable :source="sourceList.data" :tableBorder="true" :pagination="sourceList.pagination" :config="config" :loading="loading"
               highlight-current-row :selectConfig="selectConfig" :hidePagination="true" :action-list="[
              
        ]" @get-list="getList">

        <template #product="{ item }">
          <div style="width:100%; ">
            <el-popover placement="bottom-start" title="" :width="300" trigger="hover">
              <div default>
                {{item.productName}}
              </div>
              <template #reference>
                <div style="overflow:hidden;white-space:nowrap;text-overflow:ellipsis;cursor:pointer">
                  {{item.productName}}
                </div>
              </template>
            </el-popover>
          </div>
        </template>

        <template #date="{ item }">
          <div style="width:100%; ">
            <div><el-icon :size="16" style="cursor:pointer;margin-right: 3px;position:relative;top:4px" color="#409EFF"
                       @click="clickDistributeProduction(item)" v-if="isCurrentCompanyData(item.companyId)">
                <Clock />
              </el-icon>
              {{item.produceTime}} ~ {{item.deliveryPeriod}}
            </div>
          </div>
        </template>

        <template v-for="(row) in allDay" v-slot:[row.key]="{ index }" :key="row.key">
          <div style="width:100%">
            <div style="height:12px" class="is-bk"
                 :class="{'left-class':index>=0? isAddLeftClass(row,index):false,'right-class':index>=0? isAddRightClass(row,index):false,'all-class':index>=0? (isAddLeftClass(row,index)&&isAddRightClass(row,index)):false}"
                 v-if="index>=0 &&sourceList.data && sourceList.data.length>0 &&  isAddClass(row.day,index)"></div>
          </div>
        </template>

      </byTable>
    </div>
    <el-dialog :title="'打印二维码'" v-model="dialogVisible" width="350px" destroy-on-close>
      <div>
        <div id="pdfDom" style="width:100%">
          <div style="font-size:20px;text-align:center">
            {{printData.name}}
          </div>
          <div style="border-top: 1px solid #000;border-bottom: 1px solid #000; padding: 10px 0; margin:10px auto;text-align:center">
            <div :ref="printData.id" style="width:200px;margin-left:55px">
              {{printData.scanValue}}
            </div>
            <div style="font-size:20px;text-align:center;font-weight:700;margin-top:10px">
              {{printData.produceTime}}
            </div>
            <!-- 换页 -->
            <!-- <div style="page-break-after: always"></div> -->
          </div>
          <div style="margin-left:10px;display:flex;flex-direction:column;justify-content:space-around">
            <div style="font-size:14px;margin-top:8px"> 产品编码：{{printData.productName}}</div>
            <div style="font-size:14px;margin-top:8px">
              产品名称：{{printData.productSpec}}
            </div>
            <div style="font-size:14px;margin-top:8px">
              原材料编码：{{printData.productSpec}}
            </div>
            <div style="font-size:14px;margin-top:8px">
              原材料名称：{{printData.productSpec}}
            </div>
          </div>
        </div>
      </div>
      <template #footer>
        <el-button @click="dialogVisible = false" size="defualt">取 消</el-button>
        <el-button type="primary" v-print="printObj" size="defualt">打 印</el-button>
      </template>
    </el-dialog>

    <el-dialog :title="'投产'" v-model="productionDialog" width="500px" destroy-on-close>
      <byForm :formConfig="productionFormConfig" :formOption="formOption" v-model="formData.data" :rules="productionRules" ref="productionFormDom"
              v-loading="formLoading">
      </byForm>
      <template #footer>
        <el-button @click="productionDialog =false" size="default" v-debounce>取 消</el-button>
        <el-button @click="submitProduction" type="primary" size="default" v-debounce>提 交</el-button>
      </template>
    </el-dialog>

  </div>
</template>

<script setup>
import byTable from "@/components/byTable/index";
import byForm from "@/components/byForm/index";
import QRCode from "qrcodejs2-fix";
import { getDateAllDay } from "@/utils/date.js";
import moment from "moment";
import { watch } from "vue";

const { proxy } = getCurrentInstance();
const companyData = ref([]);
const loading = ref(false);
const submitLoading = ref(false);
let str = moment().format("yyyy-MM");
const sourceList = ref({
  data: [],
  pagination: {
    total: 3,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    produceStatus: "",
    beginDate: str,
    endDeliveryPeriod: "",
    beginTime: "",
    endTime: "",
  },
});
const treeData = ref([]);
const dialogVisible = ref(false);
const modalType = ref("add");
const statusData = ref([
  {
    label: "未开始",
    value: "0",
  },
  {
    label: "进行中",
    value: "1",
  },
  {
    label: "已完成",
    value: "2",
  },
]);
const allDay = ref([]);
const changDay = (val) => {
  getList();
  allDay.value = [];
  config.value.splice(4, config.value.length - 4);
  let days = getDateAllDay(val);
  let list = [];
  for (let i = 0; i < days.length; i++) {
    const ele = days[i];
    list.push({
      key: "day" + ele,
      day: ele,
    });
    let attrs = {
      label: `${ele.substr(8, 10)}`,
      slot: "day" + ele,
      isNeedHeaderSlot: false,
      width: 35,
      align: "center",
      fixed: "right",
    };
    config.value.push({
      attrs,
    });
  }
  allDay.value = list;
};
const selectConfig = computed(() => [
  {
    label: "生产公司",
    prop: "companyId",
    data: companyData.value,
  },
  {
    type: "time",
    itemType: "month",
    label: "排程日期",
    placeholder: "请选择生产日期",
    prop: "beginDate",
    clearable: false,
    placeholderOne: "",
    propOne: "",
    fn: (val) => {
      changDay(val);
    },
  },
]);

const config = ref([
  {
    attrs: {
      label: "生产公司",
      prop: "companyName",
      width: 100,
      align: "center",
    },
  },
  {
    attrs: {
      label: "生产订单号",
      prop: "code",
      width: 130,
      align: "center",
    },
  },
  {
    attrs: {
      label: "生产日期",
      slot: "date",
      width: 200,
      align: "center",
    },
  },
  {
    attrs: {
      label: "产品",
      slot: "product",
      "min-width": 280,
      // align: "center",
    },
  },
]);

const getList = (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy.post("/produceOrder/schedulingList", sourceList.value.pagination).then(
    (res) => {
      sourceList.value.data = res.map((x, index) => ({
        ...x,
        // id: "id" + index,
        produceTimeOne: x.produceTime,
        produceTime: x.produceTime.substr(0, 10),
        deliveryPeriod: x.deliveryPeriod.substr(0, 10),
      }));
      // sourceList.value.pagination.total = res.total;
      setTimeout(() => {
        loading.value = false;
      }, 200);
    },
    (err) => {
      loading.value = false;
    }
  );
};

changDay(str);
const isAddClass = (day, index) => {
  if (index >= 0 && sourceList.value.data[index]) {
    let row = sourceList.value.data[index];
    let start = new Date(row.produceTime).getTime();
    let end = new Date(row.deliveryPeriod + " 23:59:59").getTime();
    let now = new Date(day + " 23:59:59").getTime();
    if (now >= start && now <= end) {
      return true;
    } else {
      return false;
    }
  }
};

const isAddLeftClass = (day, index) => {
  if (index >= 0 && sourceList.value.data[index]) {
    let row = sourceList.value.data[index];
    let start = row.produceTime;
    if (day.day == start) {
      return true;
    } else {
      return false;
    }
  }
};
const isAddRightClass = (day, index) => {
  if (index >= 0 && sourceList.value.data[index]) {
    let row = sourceList.value.data[index];
    let start = row.deliveryPeriod;
    if (day.day == start) {
      return true;
    } else {
      return false;
    }
  }
};
const formData = reactive({
  data: {},
});
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
});
const productionFormDom = ref(null);
const productionDialog = ref(false);
const formLoading = ref(false);
const productionFormConfig = computed(() => [
  {
    type: "date",
    itemType: "datetime",
    label: "投产时间",
    prop: "produceTime",
    // placeholder: "合同开始时间",
    itemWidth: 100,
    clearable: true,
  },
]);
const productionRules = ref({
  produceTime: [
    { required: true, message: "请选择投产时间", trigger: "change" },
  ],
});
const clickDistributeProduction = (row) => {
  formData.data = {
    id: row.id,
    produceTime: row.produceTimeOne,
  };
  productionDialog.value = true;
};
const submitProduction = () => {
  productionFormDom.value.handleSubmit(() => {
    formLoading.value = true;
    proxy.post("/produceOrder/editProduceTime", formData.data).then((res) => {
      proxy.msgTip("操作成功");
      formLoading.value = false;
      productionDialog.value = false;
      getList();
    });
  });
};

const getDict = () => {
  proxy
    .get("/tenantDept/list", {
      pageNum: 1,
      pageSize: 9999,
      keyword: "",
      tenantId: proxy.useUserStore().user.tenantId,
      type: 0,
    })
    .then((res) => {
      companyData.value = res.data.map((x) => ({
        ...x,
        label: x.deptName,
        value: x.deptId,
      }));
      // treeData.value = proxy.handleTree(res.data, "deptId");
    });
};
getDict();
</script>

<style lang="scss" scoped>
::v-deep(.el-progress__text) {
  font-size: 14px !important;
}
.is-bk {
  background: #0066ff;
}
.left-class {
  border-radius: 6px 0 0 6px;
}
.right-class {
  border-radius: 0px 6px 6px 0px;
}
.all-class {
  border-radius: 6px !important ;
}
:deep(.el-table .cell) {
  padding: 0px !important;
  font-size: 12px;
  line-height: 28px;
}
</style>
