<template>
  <div class="table-box">
    <LjVxeTable
      ref="VxeTableRef"
      row-key="workmanshipid"
      :columns="columns"
      :request-api="getData"
      :data-callback="dataCallback"
      :dwname="dwname"
      :table-props="tableProps"
      :table-events="tableEvents"
      :tool-button="['export', 'refresh', 'setting', 'search', 'location']"
      :auto-load-layout="false"
      pagination
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button-group>
          <el-button @click="handleOpenNewTable">{{ $t("common.add") }}</el-button>
          <el-button @click="fDelete">{{ $t("common.delText") }}</el-button>
          <el-button @click="fAudit(2)">{{ $t("common.enabled") }}</el-button>
          <el-button @click="fAudit(3)">{{ $t("common.disable") }}</el-button>
          <el-button @click="dialogFormVisible = true">批修改</el-button>
        </el-button-group>
      </template>
    </LjVxeTable>
  </div>

  <LjDialog
    ref="LjDrawerRef"
    class="is-selector"
    v-bind="{
      ...drawerDefineProp
    }"
    :style="{ padding: 0 }"
  >
    <template #header>
      <div class="flx-1">
        <span class="text-h5-b">特殊工艺: {{ workmanshipname }}</span>
      </div>
    </template>
    <div class="flx-1 h-full">
      <LjHeaderMenu :data="mainData" :action="orderStatus ? orderEditAction : orderDefaultAction" />
      <Detail class="flx-1" :data="mainData" :status="orderStatus" :enum="enumMap" />
    </div>
  </LjDialog>

  <el-dialog v-model="dialogFormVisible" title="工艺利润率" width="500" draggable append-to-body>
    <el-form :model="formParam" label-width="80px">
      <el-form-item label="工艺利润率">
        <el-input type="number" v-model="formParam.rate" clearable form min="0"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="BatchModifyRate">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="saleprice_workmanshiplist">
import { ref, onMounted, provide } from "vue";
import { getWorkmanShipList } from "@/api/modules/saleprice";
import { ColumnProps } from "@/components/LjVxeTable/interface";
import LjDrawer from "@/components/LjDrawer/index.vue";
import Detail from "./detail.vue";
import { useHooks } from "./hooks/index";
import LjDialog from "@/components/LjDialog/index.vue";
import LjHeaderMenu from "@/components/LjHeaderMenu/index.vue";
import { useI18n } from "vue-i18n";
import { useAuthButtons } from "@/hooks/useAuthButtons";
import { cloneDeep } from "lodash-es";
import { ElMessage, ElMessageBox } from "element-plus";

const dwname = "web_workmanshiplist";
const mainData = ref({});
const tableProps = {
  height: "auto",
  editConfig: { trigger: "click", mode: "cell" }
  // rowClassName: rowClsNameFunc
  // exportConfig: {
  //   filename: t("menu.saletaskmx") + formatToDate(new Date(), "YYYY-MM-DD HH:mm:ss")
  // }
};

const { t } = useI18n();
const { VxeTableRef, LjDetailRef, columns, fDelete, fSave, fAudit } = useHooks(t);
const { CheckPower, CheckOption, buttonNew, buttonDefault } = useAuthButtons(t);

const orderStatus = ref("");
const workmanshipname = ref("");
const enumMap = ref(new Map());
const dialogFormVisible = ref(false);

const formParam = ref({
  rate: 0
});

const orderDefaultAction = [
  buttonDefault({
    label: t("common.cancelText"),
    icon: "iconchevron-left"
  }),
  buttonNew({
    label: t("common.saveText"),
    icon: "iconsave-01",
    clickFunc: item => {
      const save_data = orderStatus.value == "new" ? LjDetailRef.value?.infoParam : LjDetailRef.value?._mainData;

      fSave({ workmanship: save_data }).then(() => {
        LjDrawerRef.value.hide();
      });
    }
  })
];
const orderEditAction = [
  buttonNew({
    label: t("common.saveText"),
    icon: "iconsave-01",
    clickFunc: item => {
      const save_data = LjDetailRef.value._mainData;
      save_data.deptid = Number(save_data.deptid ?? 0);
      save_data.mattresstypeid = Number(save_data.mattresstypeid ?? 0);
      save_data.workmanshipname = save_data.workmanshipname.trim();

      fSave({ workmanship: [save_data] }).then(() => {
        LjDrawerRef.value.hide();
      });
    }
  })
];

/*
 * @description 抽屉默认属性
 */
const drawerDefineProp = {
  draggable: true,
  overflow: true,
  width: "40%"
  // modalClass: "lj-file-dialog"
};

const getData = (params: any) => {
  return getWorkmanShipList(params);
};

const dataCallback = (data: any) => {
  if (data.tableinfo?.columns) {
    data.tableinfo?.columns.map((item: any) => {
      if (item?.enum) {
        enumMap.value.set(item.field, item.enum);
      }
    });
  }
  return {
    list: data.datatable,
    tableinfo: data.tableinfo,
    total: data.totalcnt,
    pageNum: data.pageindex,
    pageSize: data.pagesize
  };
};

const LjDrawerRef = ref();
const handleDBlClickTable = ({ row, rowIndex, $rowIndex, column, columnIndex, $columnIndex, $event }: any) => {
  // 弹窗
  mainData.value = cloneDeep(row);
  workmanshipname.value = row.workmanshipname;
  orderStatus.value = "edit";
  LjDrawerRef.value.show();
};

const handleOpenNewTable = () => {
  mainData.value = {
    workmanshipid: 0,
    workmanshipname: ""
  };
  workmanshipname.value = "";
  orderStatus.value = "new";
  LjDrawerRef.value.show();
};

const BatchModifyRate = () => {
  const checkDate = VxeTableRef?.value.element.getCheckboxRecords();
  if (checkDate.length === 0) {
    ElMessage.error(`请选择要批修改的数据！`);
    return;
  }

  const modifyData = checkDate.map((item: any) => {
    return {
      workmanshipid: item.workmanshipid,
      deptid: item.deptid,
      mattresstypeid: item.mattresstypeid,
      workmanshipname: item.workmanshipname,
      formula: item.formula,
      rate: formParam.value.rate
    };
  });

  fSave({ workmanship: modifyData }).then(() => {
    dialogFormVisible.value = false;
  });
};

// 返回绑定的事件
const tableEvents = {
  "cell-dblclick": handleDBlClickTable
  // "cell-click": handleClickTable
};
</script>
