import { ref, reactive, computed, toRefs } from "vue";
import { SaletaskMx } from "./interface";
import { ColumnProps, SearchProps } from "@/components/LjVxeTable/interface";
import { oa_doc, sys_admsgmx, u_sys_post } from "@/typings/business";

/**
 * @description 表格多选数据操作
 * @param {String} rowKey 当表格可以多选时，所指定的 id
 * */
export const useHomeData = (t: any) => {
  const state = reactive<SaletaskMx.SaletaskMxStateProps>({
    vxeTableRef: null,
    requestData: [],
    selectList: [],
    currentRecord: null,
    reason: ""
  });

  // 表格配置项
  const doc_columns: ColumnProps<oa_doc>[] = [
    { type: "seq", title: "#", fixed: "left", width: 80 },
    {
      title: "抄送",
      field: "ifcopy",
      table: "oa_doc",
      render: scope => {
        let _keys = Object.keys(scope);
        let _data = _keys.includes("row") ? scope.row : _keys.includes("searchParam") ? scope.searchParam : scope;
        switch (Number(_data.ifcopy)) {
          case 1:
            return <el-checkbox class={"el-checkbox__disabled-checked"} checked={true} disabled={true} />;
          default: // 0
            let bool = false;
            return <el-checkbox v-model={bool} disabled={true} />;
        }
      }
    },
    {
      title: "发起人",
      field: "opemp",
      table: "oa_doc"
    },
    {
      title: "标题",
      field: "doctitle",
      table: "oa_doc"
    },
    {
      title: "发起时间",
      field: "opdt",
      table: "oa_doc",
      format: "yyyy-MM-dd HH:mm"
    },
    {
      title: "审批流程",
      field: "flowname",
      table: "oa_flowdef"
    },
    {
      title: "相关单号",
      field: "billcode",
      table: "oa_doc"
    },
    {
      title: "公文编号",
      field: "doccode",
      table: "oa_doc"
    },
    {
      title: "审批限定时间",
      field: "limittime",
      table: "oa_doc_flow",
      format: "yyyy-MM-dd"
    },
    {
      title: "超期",
      field: "ifover",
      table: "oa_doc",
      render: scope => {
        let _keys = Object.keys(scope);
        let _data = _keys.includes("row") ? scope.row : _keys.includes("searchParam") ? scope.searchParam : scope;
        switch (Number(_data.ifover)) {
          case 1:
            return <el-checkbox class={"el-checkbox__disabled-checked"} checked={true} disabled={true} />;
          default: // 0
            let bool = false;
            return <el-checkbox v-model={bool} disabled={true} />;
        }
      }
    },
    {
      title: "超期信息",
      field: "overinfo",
      table: "oa_doc",
      render: scope => {
        let _keys = Object.keys(scope);
        let _data = _keys.includes("row") ? scope.row : _keys.includes("searchParam") ? scope.searchParam : scope;
        let overinfo = "";
        if (_data.ifover === 0) {
          overinfo = "-";
        } else {
          let dayNum = Number(_data.daynum);
          let hourNum = Number(_data.hournum);
          let minNum = Number(_data.minnum);
          if (dayNum > 0) {
            overinfo += dayNum + "天";
          }
          if (hourNum > 0) {
            overinfo += hourNum + "小时";
          }
          if (minNum > 0) {
            overinfo += minNum + "分钟";
          }
        }
        return overinfo;
      }
    },
    {
      title: "催办",
      field: "urgeflag",
      table: "oa_doc_flow",
      render: scope => {
        let _keys = Object.keys(scope);
        let _data = _keys.includes("row") ? scope.row : _keys.includes("searchParam") ? scope.searchParam : scope;
        switch (Number(_data.urgeflag)) {
          case 1:
            return <el-checkbox class={"el-checkbox__disabled-checked"} checked={true} disabled={true} />;
          default: // 0
            let bool = false;
            return <el-checkbox v-model={bool} disabled={true} />;
        }
      }
    },
    {
      title: "最近催办时间",
      field: "urgedate",
      table: "oa_doc_flow",
      format: "yyyy-MM-dd HH:mm:ss"
    },
    {
      title: "催办次数",
      field: "urgenum",
      table: "oa_doc_flow"
    }
  ];

  const doc_unread_columns: ColumnProps<oa_doc>[] = [
    { type: "seq", title: "#", fixed: "left", width: 80 },
    {
      title: "状态",
      field: "docflag",
      table: "oa_doc",
      render: scope => {
        let _keys = Object.keys(scope);
        let _data: any = _keys.includes("row") ? scope.row : _keys.includes("searchParam") ? scope.searchParam : scope;
        switch (_data.docflag) {
          case "0": // 草稿
            return (
              <el-tag type={"info"} plain disable-transitions>
                草稿
              </el-tag>
            );
          case "1": // 处理中
            return (
              <el-tag plain disable-transitions>
                处理中
              </el-tag>
            );
          case "2": //被否决
            return (
              <el-tag type={"danger"} plain disable-transitions>
                被否决
              </el-tag>
            );
          case "11": //完成
            return (
              <el-tag type={"success"} disable-transitions>
                完成
              </el-tag>
            );
          case "12": // 作废
            return (
              <el-tag type={"info"} plain disable-transitions>
                作废
              </el-tag>
            );
          default:
            return _data.status;
        }
      }
    },
    {
      title: "标题",
      field: "doctitle",
      table: "oa_doc"
    },
    {
      title: "备注",
      field: "dscrp",
      table: "oa_doc"
    },
    {
      title: "发起时间",
      field: "opdt",
      table: "oa_doc"
    },
    {
      title: "相关单号",
      field: "billcode",
      table: "oa_doc"
    },
    {
      title: "公文编号",
      field: "doccode",
      table: "oa_doc"
    },
    {
      title: "已读",
      field: "read_flag",
      table: "oa_doc_flow",
      render: scope => {
        let _keys = Object.keys(scope);
        let _data = _keys.includes("row") ? scope.row : _keys.includes("searchParam") ? scope.searchParam : scope;
        switch (Number(_data.read_flag)) {
          case 1:
            return <el-checkbox class={"el-checkbox__disabled-checked"} checked={true} disabled={true} />;
          default: // 0
            let bool = false;
            return <el-checkbox v-model={bool} disabled={true} />;
        }
      }
    }
  ];

  const admsg_columns: ColumnProps<sys_admsgmx>[] = [
    { type: "seq", title: "#", fixed: "left", width: 80 },
    {
      title: "发送人",
      field: "sendemp",
      table: "sys_admsg"
    },
    {
      title: "发送时间",
      field: "senddt",
      table: "sys_admsg",
      format: "yyyy-MM-dd HH:mm:ss"
    },
    {
      title: "消息内容",
      field: "s_msg",
      table: "sys_admsgmx"
    },
    {
      title: "标题",
      field: "s_msgtitle",
      table: "sys_admsgmx"
    },
    {
      title: "相关单号",
      field: "billcode",
      table: "sys_admsgmx"
    },
    {
      title: "已读",
      field: "read_flag",
      table: "sys_admsgmx",
      width: 80,
      render: scope => {
        let _keys = Object.keys(scope);
        let _data = _keys.includes("row") ? scope.row : _keys.includes("searchParam") ? scope.searchParam : scope;
        switch (Number(_data.read_flag)) {
          case 1:
            return <el-checkbox class={"el-checkbox__disabled-checked"} checked={true} disabled={true} />;
          default: // 0
            let bool = false;
            return <el-checkbox v-model={bool} disabled={true} />;
        }
      }
    }
  ];

  return {
    ...toRefs(state),
    doc_columns,
    doc_unread_columns,
    admsg_columns
  };
};
