<!--
  - Author: gn
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @search-change="searchChange"
        @refresh-change="refreshChange"
        @size-change="sizeChange"
        @current-change="currentChange"
        @row-save="handleSave"
        @row-click="handleRowClick"
        @row-update="update"
      >
        <template slot="goodsTypeNo" slot-scope="scope">
          <a style="text-decoration: underline; color: #409eff;">{{scope.row.goodsNo}}</a>
        </template>
        <template slot="menu" slot-scope="scope">
          <el-button
            type="text"
            size="small"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,scope.index)"
          >编辑</el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-delete"
            @click="rowDel(scope.row,scope.index)"
          >删除</el-button>
          <el-button
            type="text"
            size="small"
            icon="el-icon-menu"
            @click="handleItem(scope.row,scope.index)"
          >属性项</el-button>
        </template>
      </avue-crud>
    </basic-container>
    <el-dialog :visible.sync="dialogFormVisible" title="属性项管理" width="90%" @close="propItemVisible">
      <avue-crud
        ref="crudItem"
        :page="itemPage"
        :data="tablePropItemData"
        :permission="permissionList"
        v-model="form"
        :before-open="handleBeforeOpen"
        :option="tablePropOption"
        @size-change="itemSizeChange"
        @current-change="itemCurrentChange"
        @row-update="handleItemUpdate"
        @row-save="handleItemSave"
        @row-del="rowItemDel"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj
} from "@/api/goodManage/invgoodstype";
import { mapGetters } from "vuex";
import {
  fetchItemList,
  delItemObj,
  putItemObj,
  addItemObj
} from "@/api/goodManage/invgoodsprop";

export default {
  name: "invgoodstype",
  components: {},
  data() {
    return {
      searchForm: {},
      tableData: [],
      form: {
        goodsTypeNo: undefined
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      itemPage: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      dialogFormVisible: false,
      tablePropItemData: [],
      tableLoading: false,
      tablePropOption: {
        menuPosition: "center",
        border: true,
        index: true,
        indexLabel: "序号",
        stripe: true,
        menuAlign: "center",
        align: "center",
        searchMenuSpan: 6,
        searchSpan: 6,
        searchBtn: true,
        searchShowBtn: true,
        searchIcon: true,
        searchLabelWidth: 100,
        labelWidth: 120,
        menuWidth: 150,
        searchIndex: 3,
        submitBtn: true,
        submitText: "确定",
        menuBtn: true,
        menu: true,
        addBtn: true,
        editBtn: true,
        delBtn: true,
        column: [
          {
            type: "input",
            overHidden: true,
            label: "属性ID",
            prop: "goodsPropId",
            span: 24,
            hide: true,
            display: false
          },
          {
            type: "input",
            overHidden: true,
            label: "属性编号",
            prop: "goodsPropNo",
            span: 24,

            display: false
          },
          {
            type: "input",
            overHidden: true,
            label: "属性名称",
            prop: "goodsPropName",
            span: 24,
            required: true,
            rules: [
              {
                required: true,
                message: "请输入属性名称！"
              }
            ]
          },

          {
            type: "select",
            overHidden: true,
            redonly: true,
            label: "商品类型",
            prop: "goodsTypeNo",
            dicUrl: "/invtory/invgoodstype/getAllGoodsType", // 动态字典 接口
            props: {
              label: "goodsTypeName",
              value: "goodsTypeNo"
            },
            span: 24,
            rules: [
              {
                required: true,
                message: "请选择商品类型！"
              }
            ],
            dicData: [],
            props: {
              label: "goodsTypeName",
              value: "goodsTypeNo"
            }
          },
          {
            type: "number",
            overHidden: true,
            label: "排序",
            prop: "sort",
            span: 24
          },
          {
            type: "textarea",
            overHidden: true,
            label: "备注",
            prop: "remark",
            span: 24
          },
          {
            type: "input",
            overHidden: true,
            label: "创建用户",
            prop: "crteUser",
            span: 24,
            hide: true,
            // search: true,
            editDisplay: false,
            addDisplay: false
          },
          {
            type: "input",
            overHidden: true,
            label: "最后更新用户",
            prop: "upteUser",
            span: 24,
            hide: true,
            // search: true,
            editDisplay: false,
            addDisplay: false
          },
          {
            type: "date",
            overHidden: true,
            label: "创建时间",
            prop: "crteTime",
            span: 24,
            hide: true,
            // search: true,
            // searchRange: true,
            rangeSeparator: "~",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            editDisplay: false,
            addDisplay: false
          },
          {
            type: "date",
            overHidden: true,
            label: "最后更新时间",
            prop: "upteTime",
            span: 24,
            hide: true,
            // search: true,
            // searchRange: true,
            rangeSeparator: "~",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            editDisplay: false,
            addDisplay: false
          }
        ]
      },
      tableOption: {
        menuPosition: "center",
        border: true,
        index: true,
        indexLabel: "序号",
        stripe: true,
        menuAlign: "center",
        align: "center",
        searchMenuSpan: 6,
        searchSpan: 6,
        searchBtn: true,
        searchShowBtn: true,
        searchIcon: true,
        searchLabelWidth: 100,
        labelWidth: 120,
        menuWidth: 200,
        searchIndex: 3,
        submitBtn: true,
        submitText: "确定",
        menuBtn: true,
        menu: true,
        addBtn: true,
        editBtn: false,
        delBtn: false,
        column: [
          {
            type: "input",
            label: "商品类型ID",
            prop: "goodsTypeId",
            span: 24,
            overHidden: true,
            hide: true,
            display: false
          },
          {
            type: "input",
            label: "类型编号",
            prop: "goodsTypeNo",
            span: 24,
            overHidden: true,
            search: true,
            display: false
          },
          {
            type: "input",
            label: "商品类型",
            prop: "goodsTypeName",
            span: 24,
            overHidden: true,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入商品类型！",
                trigger: "blur"
              }
            ]
          },

          {
            type: "input",
            label: "创建用户",
            prop: "crteUser",
            span: 24,
            overHidden: true,
            // hide: true,
            // search: true,
            editDisplay: false,
            addDisplay: false
          },
          {
            type: "input",
            label: "最后更新用户",
            prop: "upteUser",
            span: 24,
            overHidden: true,
            // hide: true,
            // search: true,
            editDisplay: false,
            addDisplay: false
          },
          {
            type: "date",
            label: "创建时间",
            prop: "crteTime",
            span: 24,
            overHidden: true,
            // hide: true,
            // search: true,
            // searchRange: true,
            rangeSeparator: "~",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            editDisplay: false,
            addDisplay: false
          },
          {
            type: "date",
            label: "最后更新时间",
            prop: "upteTime",
            span: 24,
            overHidden: true,
            // hide: true,
            // search: true,
            // searchRange: true,
            rangeSeparator: "~",
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            editDisplay: false,
            addDisplay: false
          },
          {
            type: "textarea",
            label: "备注",
            prop: "remark",
            span: 24,
            overHidden: true
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.generator_invgoodstype_add,
          true
        ),
        delBtn: this.vaildData(
          this.permissions.generator_invgoodstype_del,
          true
        ),
        editBtn: this.vaildData(
          this.permissions.generator_invgoodstype_edit,
          true
        )
      };
    }
  },
  methods: {
    itemSizeChange(pageSize) {
      this.itemPage.pageSize = pageSize;
      this.getPropItemList();
    },
    itemCurrentChange(current) {
      this.itemPage.currentPage = current;
      this.getPropItemList();
    },
    propItemVisible: function() {
      this.dialogFormVisible = false;
      this.itemPage.currentPage = 1;
    },
    handleItem: function(row) {
      this.goodsTypeNo = row.goodsTypeNo;
      this.getPropItemList();
    },
    handleItemSave: function(row, done) {
      addItemObj(row).then(() => {
        this.$message.success("添加成功");
        this.getPropItemList();
        done();
      });
    },
    handleBeforeOpen(done) {
      this.form.goodsTyNo = this.goodsTyNo;
      done();
    },
    handleItemUpdate: function(row, index, done) {
      putItemObj(row).then(() => {
        this.$message.success("修改成功");
        this.getPropItemList();
        done();
      });
    },
    rowItemDel: function(row) {
      this.$confirm('是否确认删除数据为"' + row.label + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delItemObj(row.id);
        })
        .then(() => {
          this.getPropItemList();
          this.$message.success("删除成功");
        })
        .catch(function() {});
    },
    getPropItemList() {
      this.dialogFormVisible = true;
      fetchItemList(
        Object.assign(
          {
            current: this.itemPage.currentPage,
            size: this.itemPage.pageSize
          },
          { goodsTypeNo: this.goodsTypeNo }
        )
      ).then(response => {
        this.tablePropItemData = response.data.data.records;
        this.itemPage.total = response.data.data.total;
      });
    },
    getList(page, params) {
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: this.page.currentPage,
            size: this.page.pageSize
          },
          params,
          this.searchForm
        )
      )
        .then(response => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
    },
    rowDel: function(row, index) {
      this.$confirm(
        "是否确认删除商品类型为" + row.goodsTypeName + "的记录？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delObj(row.goodsTypeId);
        })
        .then(data => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    handleUpdate(row, index) {
      this.$refs.crud.rowEdit(row, index);
    },
    update: function(row, index, done, loading) {
      Object.keys(row).forEach(i => {
        if (!row[i]) {
          delete row[i];
        }
      });
      putObj(row)
        .then(data => {
          this.$message.success("修改成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    handleSave: function(row, done, loading) {
      Object.keys(row).forEach(i => {
        if (!row[i]) {
          delete row[i];
        }
      });
      addObj(row)
        .then(data => {
          this.$message.success("添加成功");
          done();
          this.getList(this.page);
        })
        .catch(() => {
          loading();
        });
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    refreshChange() {
      this.getList(this.page);
    },

    handleRowClick(row) {}
  }
};
</script>