import projectTagView from "@base/components/projectTag/view.vue";
import projectTagAddButton from "@base/components/projectTag/addButton.vue";
import projectTagDeleteButton from "@base/components/projectTag/deleteButton.vue";
import tableForm from "@base/components/table/tableForm.vue";
import MenuKindDialog from "@base/views/bd/setting/menu_kind/dialog.vue";
import editView from "@base/views/bd/setting/form_script/edit1.vue";
import scriptDialog from "@base/views/bd/setting/form_script/dialog.vue";
import projectTagDialog from "@base/views/user/project_tag/dialog.vue";
import { getBdFlag } from "@base/api/user";
import { getJsxStatus, getJsxBtn } from "@base/views/bd/setting/utils/index";
import formVersionButton from "@base/views/bd/setting/formVersion/link.vue";

let modules = {};
modules = {
  name: "form_script:form_list",
  components: {
    projectTagView,
    projectTagAddButton,
    projectTagDeleteButton,
    tableForm,
    MenuKindDialog,
    editView,
    scriptDialog,
    projectTagDialog,
    formVersionButton,
  },
  props: ["formTemplate"],
  data() {
    return {
      activeName: "second",
      dataId: 0,
      showEdit: false,
      vxeOption: {},
      formData: {},
      showScriptDialog: false,
      addProjectTagOption: {
        url: USER_PREFIX + `/formScript/saveTag`,
        tableDatas: () => {
          return this.$refs["table-m1"].getCheckboxRecords(true);
        },
        reqData: (tagDatas, tableDatas) => {
          let sids = tableDatas.map((item) => item.sid);
          let tagCodes = tagDatas.map((item) => item.tagCode);
          let reqData = {
            tagCodes,
            sids,
          };
          return reqData;
        },
        callback: () => {
          this.searchEvent();
        },
      },
      deleteProjectTagOption: {
        url: USER_PREFIX + `/formScript/deleteTag`,
        tableDatas: () => {
          return this.$refs["table-m1"].getCheckboxRecords(true);
        },
        reqData: (tagDatas, tableDatas) => {
          let sids = tableDatas.map((item) => item.sid);
          let tagCodes = tagDatas.map((item) => item.tagCode);
          let reqData = {
            tagCodes,
            sids,
          };
          return reqData;
        },
        callback: () => {
          this.searchEvent();
        },
      },
      showProjectTagDialog3: false,
      checkTags: [],
      isDev: true,
      menuKindAuth: {
        editAuth: 0,
      },
    };
  },
  mounted() {
    this.getBdEnv();
    this.getMenuKindAuth();
    this.initTableList();
  },
  methods: {
    searchEvent() {
      this.$refs["table-m1"].commitProxy("reload");
    },
    resetEvent() {
      this.formData = {};
      this.checkTags = [];
      this.$refs["table-m1"].commitProxy("reload");
    },
    openEditDialog(row) {
      /*this.dataId = !id || typeof id === 'object' ? 0 : id;
      this.activeName = 'first';
      this.$openEditView('showEdit');*/

      let dataId = row?.id || 0;
      if (!dataId) {
        this.dataId = dataId;
        this.activeName = "first";
        this.$openEditView("showEdit");
      } else {
        this.$refs.xTabs.openEditTab(row);
      }
    },
    initTableList() {
      let that = this;
      let tableOption = {
        vue: this,
        tableRef: "table-m1",
        tableName: "bd_form_script_form_list-m1",
        path: USER_PREFIX + "/formScript/listPage",
        param: () => {
          let tagCodes = null;
          if (this.checkTags.length) {
            tagCodes = this.checkTags.map((item) => item.tagCode);
          }
          return {
            formCode: this.formTemplate.formCode,
            menuKindCode: this.formTemplate.menuKindCode,
            ...this.formData,
            tagCodes,
            scriptType: 1,
          };
        },
        columns: [
          { type: "checkbox", width: 48, resizable: false, fixed: "left" },
          {
            title: this.$t1("脚本名称"),
            field: "scriptName",
            width: 180,
            fixed: "left",
          },
          {
            title: this.$t1("脚本编码"),
            field: "scriptCode",
            width: 180,
          },
          {
            title: this.$t1("脚本说明"),
            field: "scriptRemark",
            width: 250,
          },
          {
            title: this.$t1("表单模板编码"),
            field: "formCode",
            width: 250,
          },
          {
            title: this.$t1("项目标签"),
            field: "tag",
            width: 250,
            slots: { default: "tag" },
            params: {
              exportVal: ({ row }) => {
                if (row.formScriptTagDTOs) {
                  return row.formScriptTagDTOs
                    .map((item) => item.tagName)
                    .join(",");
                }
              },
            },
          },
          {
            field: "transactions",
            title: this.$t1("开启事务"),
            width: 150,
            slots: {
              default: ({ row }) => {
                if (row.transactions === 1) {
                  return getJsxStatus(null, this.$t1("是"));
                } else {
                  return getJsxStatus("s-3", this.$t1("否"));
                }
              },
            },
          },
          { field: "serviceName", title: this.$t1("服务名"), width: 150 },
          { field: "version", title: this.$t1("版本号"), width: 150 },
          {
            field: "sid",
            title: this.$t1("唯一标识"),
            width: 280,
          },
          {
            field: "createBy",
            title: this.$t1("创建人"),
            width: 150,
          },
          {
            field: "createDate",
            title: this.$t1("创建时间"),
            width: 150,
          },
          {
            field: "modifyBy",
            title: this.$t1("更新人"),
            width: 150,
          },
          {
            field: "modifyDate",
            title: this.$t1("更新时间"),
            width: 150,
          },
          {
            width: 100,
            fixed: "right",
            title: "",
            sortable: false,
            slots: {
              default: "operation",
            },
          },
        ],
        searchColumns: [
          {
            title: this.$t1("脚本名称"),
            field: "scriptName",
            type: "input",
            common: true,
          },
          {
            title: this.$t1("脚本编码"),
            field: "scriptCode",
            type: "input",
            common: true,
          },
          {
            title: this.$t1("脚本说明"),
            field: "scriptRemark",
            type: "input",
            common: true,
          },
          {
            title: this.$t1("脚本"),
            field: "script",
            type: "input",
            common: true,
          },
          {
            title: this.$t1("项目标签"),
            field: "tag",
            type: "input",
            common: true,
            slot: "tag",
          },
        ],
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then((opts) => {
        this.vxeOption = opts;
      });
    },
    confirmScriptDialog(rows) {
      if (rows.length) {
        let formCode = this.formTemplate.formCode;
        let menuKindCode = this.formTemplate.menuKindCode;
        let data = rows.map((row) => {
          return {
            scriptCode: row.scriptCode,
            scriptName: row.scriptName,
            scriptRemark: row.scriptRemark,
            script: row.script,
            scriptType: 1,
            formCode: formCode,
            menuKindCode: menuKindCode,
          };
        });
        this.$http({
          aes: true,
          url: USER_PREFIX + "/formScript/saves",
          method: `post`,
          data: data,
          isLoading: true,
          success: (res) => {
            this.$message({
              message: res.content,
              type: "success",
              duration: 500,
              onClose: (t) => {
                this.searchEvent();
              },
            });
          },
        });
      }
    },
    openProjectTagDialog3() {
      this.showProjectTagDialog3 = true;
    },
    confirmProjectTagDialog3(rows) {
      this.checkTags = rows;
    },
    getTabNames() {
      return this.checkTags.map((item) => item.tagName).join(",");
    },
    getBdEnv() {
      getBdFlag({
        success: (res) => {
          this.isDev = res.objx === 1;
        },
      });
    },
    getMenuKindAuth() {
      let menuKindCode = this.formTemplate.menuKindCode;
      if (menuKindCode) {
        this.$http({
          aes: true,
          url: USER_PREFIX + "/menu_kind_auth/getAuth",
          method: `post`,
          data: { stringOne: menuKindCode },
          isLoading: true,
          success: (res) => {
            this.menuKindAuth = res.objx || {};
          },
        });
      }
    },
  },
};
export default modules;
