<template>
  <el-dialog
    :title="$t1('暂存数据列表')"
    :append-to-body="true"
    :modal-append-to-body="true"
    :close-on-click-modal="falseValue"
    :visible.sync="showDialog"
    :modal="falseValue"
    custom-class="dialog-style list-dialog dialog-checkbox pd_0"
    width="1200px"
    @close="dialogClose"
    v-el-drag-dialog
    v-el-dialog-center
  >
    <div class="cont" style="height: 450px">
      <vxe-grid
        class="is-pointer"
        ref="table-m1"
        v-bind="vxeOption"
        @resizable-change="$vxeTableUtil.onColumnWitchChange"
        @custom="$vxeTableUtil.customHandle"
        @checkbox-change="addDataTable"
        @checkbox-all="checkAll"
        @cell-dblclick="checkWithSubmit"
      >
        <template #form>
          <div class="clearfix screen-btns">
            <div class="fl">
              <el-button class="button-sty" icon="el-icon-delete" @click="mulitidDelete"
                >{{ $t1("删除") }}
              </el-button>
            </div>
            <div class="fr">
              <vxe-button
                icon="el-icon-brush"
                class="button-sty"
                @click="resetEvent"
                type="text"
                status="primary"
                plain
                >{{ $t1("重置") }}
              </vxe-button>
              <vxe-button
                status="warning"
                icon="el-icon-search"
                class="button-sty"
                @click="searchEvent"
                >{{ $t1("搜索") }}
              </vxe-button>
            </div>
          </div>
          <vxe-form
            class="screen-box"
            title-width="92px"
            title-align="right"
            :data="formData"
            @submit="searchEvent"
            @reset="resetEvent"
          >
            <vxe-form-item :title="$t1('暂存编码') + ':'" field="storageCode">
              <template v-slot>
                <el-input v-model="formData.storageCode" size="small" clearable />
              </template>
            </vxe-form-item>
            <vxe-form-item :title="$t1('描述') + ':'" field="remark">
              <template v-slot>
                <el-input v-model="formData.remark" size="small" clearable />
              </template>
            </vxe-form-item>
            <vxe-button type="submit" @click="searchEvent" v-show="false"></vxe-button>
          </vxe-form>
        </template>
      </vxe-grid>
    </div>
    <label id="labBtn" class="transverse">
      <div class="icon">
        <i class="el-icon-more"></i>
        <i class="el-icon-more"></i>
      </div>
    </label>
    <div class="multipleChoice">
      <el-tooltip
        :enterable="false"
        effect="dark"
        :content="$t1('全部删除')"
        placement="top"
        ><a class="allDel icon-quanbushanchu" @click="clearChecked()"></a
      ></el-tooltip>
      <div class="list">
        <div class="item" v-for="(checkRow, index) in checkRows" :key="index">
          <p>{{ checkRow.storageCode }}</p>
          <a class="el-icon-close" @click="clearTable1Select(index)"></a>
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <span class="fl tips">{{ $t1("注：双击确认选择（单选）") }}</span>
      <el-button type="primary" plain class="button-sty" @click="dialogClose">
        <i class="el-icon-close el-icon"></i>
        {{ $t1("取消") }}
      </el-button>
      <el-button type="primary" @click="dialogPrimary()" class="button-sty">
        <i class="el-icon-check el-icon"></i>
        {{ $t1("确定") }}
      </el-button>
    </span>
  </el-dialog>
</template>

<script>
import { selectDialogMixins } from "../../mixins/selectDialog/index.js";

export default {
  name: "positionDialog",
  props: ["visiable", "multi", "rows", "param", "storageType"],
  mixins: [selectDialogMixins],
  created() {
    this.initSetting();
    this.selectMulti = true;
  },
  mounted() {
    this.initTableM1();
  },
  data() {
    return {
      showDialog: true,
      falseValue: false,
      selectMulti: true,
      formData: {},
      vxeOption: {},
    };
  },
  methods: {
    initTableM1() {
      let that = this;
      let url = USER_PREFIX + "/temp_storage/listPage";
      let tableOption = {
        vue: that,
        tableRef: "table-m1",
        tableName: "user_temp_storage_dialog-m1",
        path: url,
        param: () => {
          return {
            ...this.formData,
            storageType: this.storageType,
          };
        },
        columns: [
          { type: "checkbox", fixed: "left", width: 48, resizable: false },
          {
            field: "remark",
            title: this.$t1("描述"),
            width: 250,
            fixed: "left",
          },
          {
            field: "createDate",
            title: this.$t1("创建时间"),
            width: 150,
          },
          {
            title: this.$t1("暂存编码"),
            field: "storageCode",
            width: 300,
          },
          { title: this.$t1("暂存数据"), field: "data", width: 350, visible: false },
          {
            title: this.$t1("操作"),
            width: 47,
            fixed: "right",
            title: "",
            sortable: false,
            slots: {
              default: ({ row, rowIndex, $table }) => {
                return [
                  <a
                    href="javascript:void(0);"
                    class="a-link"
                    onclick={(event) => {
                      event.stopPropagation();
                      this.deleteRow(row);
                    }}
                  >
                    <el-tooltip
                      enterable={false}
                      effect="dark"
                      content={this.$t1("删除")}
                      placement="top"
                      popper-class="tooltip-skin"
                    >
                      <i class="el-icon-delete" />
                    </el-tooltip>
                  </a>,
                ];
              },
            },
          },
        ],
        config: {
          checkboxConfig: {
            checkStrictly: true,
            showHeader: this.selectMulti,
            trigger: "row",
          },
        },
        callback: (rows) => {
          if (!rows.length) return;
          let checkRows = this.checkRows;
          if (checkRows.length) {
            let checkIds = checkRows.map((item) => item.id);
            let items = rows.filter((item) => checkIds.includes(item.id));
            if (items.length) {
              this.$refs["table-m1"].setCheckboxRow(items, true);
            }
          }
        },
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then((opts) => {
        that.vxeOption = opts;
      });
    },
    deleteRow(row) {
      this.$baseConfirm(this.$t1("您确定要删除当前暂存数据吗？")).then(() => {
        this.deleteHandle([row]);
      });
    },
    mulitidDelete() {
      let checkRows = this.checkRows;
      if (checkRows.length == 0) {
        this.$message({
          message: this.$t1("请选择要删除的暂存数据"),
          type: "error",
        });
        return;
      }
      this.$baseConfirm(this.$t1("您确定要删除选中的暂存数据吗？")).then(() => {
        this.deleteHandle(checkRows);
      });
    },
    deleteHandle(rows) {
      if (!rows || !rows.length) {
        return;
      }
      let data = rows.map((row) => row.id);
      this.$http({
        url: USER_PREFIX + "/temp_storage/delete",
        method: `post`,
        data: data,
        isLoading: true,
        success: (res) => {
          this.$message({
            message: res.content,
            type: "success",
          });
          this.checkRows = this.checkRows.filter((row) => !data.includes(row.id));
          this.searchEvent();
        },
      });
    },
    checkWithSubmit(obj) {
      let $grid1 = this.$refs["table-m1"];
      this.checkRows = [obj.row];
      $grid1.clearCheckboxRow();
      $grid1.setCheckboxRow(obj.row, true);
      setTimeout(() => {
        this.dialogPrimary();
      }, 100);
    },
    dialogPrimary() {
      let rows = this.checkRows;
      if (rows.length > 1) {
        this.$message({
          message: this.$t1("请选择一条暂存数据进行操作"),
          type: "error",
        });
        return;
      }
      if (this.beforeConfirm && this.beforeConfirm(rows, this.dialogClose) === false) {
        return false;
      }
      this.$emit("confirm", rows);
      this.dialogClose();
    },
  },
};
</script>
