<script>
import {
  add,
  bindList,
  removeBind,
  removeAllBind,
  classifyTree,
} from "@/api/material/label";
export default {
  name: "BindDialog",
  data() {
    const initDict = {
      labelType: [
        { label: "类别", value: 1 },
        { label: "标签", value: 2 },
      ],
      parentId: [],
    };
    const initColumns = () => [
      {
        type: "text",
        prop: "materialCode",
        label: "物料编码",
        required: true,
        showOverflowTooltip: true,
      },
      {
        type: "select",
        prop: "materialName",
        label: "物料名称",
        required: true,
        showOverflowTooltip: true,
      },
      {
        type: "text",
        prop: "materialClassifyId",
        label: "物料分类",
        required: false,
        showOverflowTooltip: true,
      },
      {
        type: "text",
        prop: "materialState",
        label: "物料状态",
        required: false,
        showOverflowTooltip: true,
      },
      {
        type: "text",
        prop: "createTime",
        label: "创建时间",
        required: false,
        showOverflowTooltip: true,
      },
      {
        type: "text",
        prop: "bindTime",
        label: "绑定时间",
        required: false,
        showOverflowTooltip: true,
      },
    ];
    const initForm = () => {
      let form = {};
      initColumns().forEach((item) => (form[item.prop] = ""));
      return form;
    };
    return {
      loading: false,
      dialogFormVisible: false,
      autoInnerVisible: false,
      handInnerVisible: false,
      form: initForm(),
      columns: initColumns(),
      tableData: [],
      page: { pageNum: 1, pageSize: 25, total: 0 },
      dict: initDict,
      filterText: "",
      options: [],
      innerForm: {},
    };
  },
  methods: {
    async fetchList(prop) {
      this.loading = true;
      let { pageNum, pageSize } = this.page;
      await bindList({ ...prop, pageNum, pageSize })
        .then((res) => {
          this.tableData = res.rows;
        })
        .finally(() => {
          this.loading = false;
        });
    },

    async fetchItem(prop) {
      // console.log(prop);
      // return;
      this.dialogFormVisible = true;
      this.form = { ...this.form, ...prop };
      await this.fetchList({ labelId: this.form.id });
    },

    async fetchClassifyTree() {
      let data = await classifyTree();
      this.options = data;
    },

    onAutoBindLabel(prop) {
      this.$parent.$children
        .find((el) => el.$vnode.tag.indexOf("AutoBindDialog") > -1)
        .fetchItem(prop);
    },

    onHandBindLabel(prop) {
      this.$parent.$children
        .find((el) => el.$vnode.tag.indexOf("HandBindDialog") > -1)
        .fetchItem(prop);
    },

    async onUnbind(prop) {
      const { materialId: id } = prop;
      await removeBind({ id });
      await this.fetchList({ labelId: this.form.id });
    },

    async onAllUnbind(prop) {
      const { id } = prop;
      await removeAllBind({ id });
      await this.fetchList({ labelId: id });
    },

    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.form.labelType === 1) this.form.parentId = 0;
          add(this.form)
            .then((res) => {
              let { code } = res;
              if (code === 200) {
                this.dialogFormVisible = false;
                this.$message.success("success");
                this.$parent.$children[0].fetchList();
                this.form = {};
              }
            })
            .finally(() => {
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
  created() {},
};
</script>

<template>
  <el-dialog destroy-on-close title="绑定" :visible.sync="dialogFormVisible">
    <el-button :disabled="loading" @click="onAutoBindLabel(form)">
      自动绑定
    </el-button>
    <el-button :disabled="loading" @click="onHandBindLabel(form)">
      手动绑定
    </el-button>
    <el-button :disabled="loading" @click="onAllUnbind(form)">
      全部解绑
    </el-button>
    <el-table
      v-loading="loading"
      lazy
      border
      row-key="id"
      :data="tableData"
      style="margin-top: 16px"
    >
      <el-table-column
        v-for="column in columns"
        :key="column.prop"
        :prop="column.prop"
        :label="column.label"
        :show-overflow-tooltip="column.showOverflowTooltip"
      >
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="225">
        <template slot-scope="scope">
          <el-button @click.native.prevent="onUnbind(scope.row)" size="small">
            解绑
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button :disabled="loading" @click="dialogFormVisible = false"
        >取 消</el-button
      >
      <el-button type="primary" :disabled="loading" @click="onSubmit('addForm')"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<style scoped></style>
