<!--
  - Author: gn
  -->
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        ref="crud"
        :page="page"
        :data="tableData"
        :permission="permissionList"
        :table-loading="tableLoading"
        :option="tableOption"
        @current-change="currentChange"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-change="searchChange"
        @size-change="sizeChange"
        @row-del="rowDel"
        @row-update="handleUpdate"
        @row-save="handleSave"
      ></avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj
} from "@/api/goodManage/invgoods";
//import { fetchList as fetchList1 } from "@/api/commonApis";
import { fetchGoodsTypeTree } from "@/api/goodManage/invgoodstype";
import { mapGetters } from "vuex";
export default {
  name: "invgoods",
  components: {},
  data() {
    return {
      url: {
        getTypes: "/invtory/invgoodstype/page"
      },
      form: {},
      searchForm: {},
      tableData: [],
      treeGoodsTypeData: [],
      defaultProps: {
        label: "goodsTypeName",
        value: "goodsTypeNo"
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: 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: 150,
        searchIndex: 3,
        submitBtn: true,
        submitText: "确定",
        menuBtn: true,
        menu: true,
        addBtn: true,
        editBtn: true,
        delBtn: true,
        column: [
          {
            type: "input",
            label: "商品ID",
            prop: "goodsId",
            span: 24,
            hide: true,
            display: false
          },
          {
            type: "input",
            overHidden: true,
            label: "商品编号",
            prop: "goodsNo",
            span: 24,
            width: 250,
            // slot: true,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入商品编号！",
                trigger: "blur"
              }
            ]
            //display: false
          },
          {
            type: "input",
            overHidden: true,
            label: "商品名称",
            prop: "goodsName",
            width: 380,
            span: 24,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入商品名称！",
                trigger: "blur"
              }
            ]
          },
          {
            type: "select",
            overHidden: true,
            label: "商品版本",
            prop: "goodsVersion",
            width: 80,
            span: 24,
            dicUrl: "/admin/dict/type/goods_version",
            search: true,
            rules: [
              {
                required: true,
                message: "请选择商品版本",
                trigger: "blur"
              }
            ]
          },
          {
            //overHidden: true,
            label: "商品大类",
            prop: "goodsTypeName",
            span: 24,
            display: false
          },
          {
            type: "select",
            //overHidden: true,
            label: "商品大类",
            prop: "goodsType",
            span: 24,
            //formslot: true,
            //slot: true,
            hide: true,
            dicUrl: "/invtory/invgoodstype/getAllGoodsType", // 动态字典 接口
            props: {
              label: "goodsTypeName",
              value: "goodsTypeNo"
            },
            search: true,
            rules: [
              {
                required: true,
                message: "请选择商品类型！",
                trigger: "blur"
              }
            ]
          },
          {
            type: "input",
            overHidden: true,
            label: "商品单位",
            prop: "unit",
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入单位！",
                trigger: "blur"
              }
            ]
          },
          {
            type: "input",
            overHidden: true,
            label: "商品价格",
            prop: "price",
            span: 12,
            rules: [
              {
                required: true,
                message: "请输入商品价格！",
                trigger: "blur"
              },
               {
                pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/,
                message: '请输入数字，金额输入不能小于0，最多只可保留2位小数',
                trigger: 'blur'
              }
            ]
          },
          /**{
            type: "input",
            overHidden: true,
            label: "图片路径",
            prop: "pic",
            span: 24
          },**/
          {
            type: "input",
            overHidden: true,
            label: "安全值",
            prop: "safeValue",
            span: 24
          },
          {
            type: "textarea",
            overHidden: true,
            label: "备注",
            prop: "remark",
            span: 24,
            display: true
          },
          {
            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
          }
        ]
      }
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.generator_invgoods_add, true),
        delBtn: this.vaildData(this.permissions.generator_invgoods_del, true),
        editBtn: this.vaildData(this.permissions.generator_invgoods_edit, true)
      };
    }
  },
  created() {
    // fetchList1(this.url.getTypes, {}).then(res => {
    //if (res && res.data && res.data.data && res.data.data.records) {
    //this.tableOption.column[4].dicData = res.data.data.records;
    // }
    //});
    //this.getList();
    //this.init();
  },
  methods: {
    init() {
      fetchGoodsTypeTree().then(response => {
        this.treeGoodsTypeData = response.data.data;
      });
    },
    searchChange(form, done) {
      this.searchForm = form;
      this.page.currentPage = 1;
      this.getList(this.page, form);
      done();
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    currentChange(current) {
      this.page.currentPage = current;
    },
    refreshChange() {
      this.getList(this.page);
    },
    getList(page, params) {
      //alert(this.page.currentPage+"====");
      this.tableLoading = true;
      fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: 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.goodsName + "的记录？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delObj(row.goodsId);
        })
        .then(data => {
          this.$message.success("删除成功");
          this.getList(this.page);
        });
    },
    handleUpdate: 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();
        });
    }
  }
};
</script>