import tableForm from "@base/components/table/tableForm.vue";
import editView from "@base/views/bd/setting/form_script/edit.vue";
import { getBdFlag } from "@base/api/user";
import { getJsxBtn, getJsxStatus } from "@base/views/bd/setting/utils/index";
import formVersionButton from "@base/views/bd/setting/formVersion/link.vue";

let modules = {};
modules = {
  name: "form_script:list",
  props: {
    scriptType: {
      type: Number,
      default: 0,
    },
    formCode: {
      type: String,
      default: null,
    },
  },
  components: { tableForm, editView, formVersionButton },
  data() {
    return {
      activeName: "second",
      dataId: 0,
      showEdit: false,
      vxeOption: {},
      formData: {},
      isDev: true,
    };
  },
  mounted() {
    this.getBdEnv();
    this.initTableList();
  },
  methods: {
    searchEvent() {
      this.$refs["table-m1"].commitProxy("reload");
    },
    resetEvent() {
      this.formData = {};
      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);
      }
    },
    getSearchParam() {
      return {
        ...this.formData,
        formCode: this.formCode,
        scriptType: this.scriptType,
      };
    },
    initTableList() {
      let that = this;
      let tableOption = {
        vue: this,
        tableRef: "table-m1",
        tableName: "bd_form_script_list-m" + this.scriptType,
        path: USER_PREFIX + "/formScript/listPage",
        param: () => {
          return this.getSearchParam();
        },
        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: "enabled",
            width: 150,
            slots: {
              default: ({ row }) => {
                if (row.enabled) {
                  return getJsxStatus(null, this.$t1("启用"));
                } else {
                  return getJsxStatus("s-3", this.$t1("禁用"));
                }
              },
            },
          },
          {
            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",
              /* default: ({ row }) => {
                return getJsxBtn({
                  iconName: "el-icon-edit",
                  content: this.$t1("查看"),
                  onclick: () => {
                    this.openEditDialog(row);
                  },
                });
              }, */
            },
          },
        ],
        searchColumns: [
          {
            title: this.$t1("脚本名称"),
            field: "scriptName",
            type: "input",
            common: true,
          },
          {
            title: this.$t1("脚本编码"),
            field: "scriptCode",
            type: "input",
            common: true,
          },
          {
            title: this.$t1("是否启用"),
            field: "enabled",
            type: "select",
            common: true,
            itemOption: [
              { label: this.$t1("启用"), value: true },
              { label: this.$t1("禁用"), value: false },
            ],
          },
          {
            title: this.$t1("脚本说明"),
            field: "scriptRemark",
            type: "input",
            common: true,
          },
          {
            title: this.$t1("脚本"),
            field: "script",
            type: "input",
            common: true,
          },
          {
            title: this.$t1("创建人"),
            field: "createBy",
            type: "input",
            common: false,
          },
          {
            title: this.$t1("更新人"),
            field: "modifyBy",
            type: "input",
            common: false,
          },
          {
            title: this.$t1("操作日志"),
            field: "logContent",
            type: "input",
            common: false,
          },
          {
            title: this.$t1("创建时间"),
            field: "startCreateDate",
            field2: "endCreateDate",
            type: "dateRange",
            common: false,
            widgetType: "datetime",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
          },
          {
            title: this.$t1("更新时间"),
            field: "startModifyDate",
            field2: "endModifyDate",
            type: "dateRange",
            common: false,
            widgetType: "datetime",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
          },
        ],
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then((opts) => {
        this.vxeOption = opts;
      });
    },
    jsonImport() {
      this.$jsonImport({
        saveUrl: USER_PREFIX + "/form_develop/importFormScript",
        showResult: true,
        resultColumns: [
          {
            title: this.$t1("脚本名称"),
            field: "scriptName",
            width: 180,
            fixed: "left",
          },
          {
            title: this.$t1("脚本编码"),
            field: "scriptCode",
            width: 180,
          },
        ],
        callback: () => {
          this.searchEvent();
        },
      });
    },
    jsonExport() {
      this.$jsonExport({
        title: "通用脚本",
        targetRef: "table-m1",
        url: USER_PREFIX + "/form_develop/exportFormScript",
        abcEnabled: true,
      });
    },
    getBdEnv() {
      getBdFlag({
        success: (res) => {
          this.isDev = res.objx === 1;
        },
      });
    },
  },
};
export default modules;
