<!--
  - 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-update="handleUpdate"
        @row-save="handleSave"
        @row-del="rowDel"
      ></avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj
} from "@/api/baseManage/invsupplier";
import { mapGetters } from "vuex";
import { checkphone } from "@/util/GnUtils";

export default {
  name: "invsupplier",
  data() {
    return {
      searchForm: {},
      tableData: [],
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20 // 每页显示多少条
      },
      tableLoading: false,
      tableOption: {
        menuPosition: "center",
        border: true,
        index: true,
        indexLabel: "序号",
        stripe: true,
        menuAlign: "center",
        align: "center",
        searchMenuSpan: 8,
        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: "suplierId",
            span: 24,
            hide: true,
            display: false
          },
          {
            type: "input",
            label: "供应商名称",
            prop: "suplierName",
            span: 24,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入供应商名称！",
                trigger: "blur"
              }
            ]
          },
          {
            type: "input",
            label: "联系电话",
            prop: "contactTel",
            span: 24
            /*rules: [
              {
                required: true,
                validator: checkphone,
                trigger: "blur"
              }
            ]*/
          },
          {
            type: "input",
            label: "省",
            prop: "provinceName",
            span: 24,
            display: false
          },
          {
            type: "input",
            label: "市",
            prop: "cityName",
            span: 24,
            display: false
          },
          {
            type: "input",
            label: "县区",
            prop: "districtName",
            span: 24,
            display: false
          },
          {
            type: "select",
            label: "省",
            prop: "province",
            hide: true,
            span: 8,
            width: "100%",
            dicUrl: `/invtory/districts/getProvince`,
            props: {
              label: "name",
              value: "code"
            },
            cascaderItem: ["city", "district"],
            search: true
          },
          {
            type: "select",
            label: "市",
            prop: "city",
            span: 8,
            width: "100%",
            hide: true,
            dicUrl: `/invtory/districts/getChildCity?cityCode={{province}}`,
            props: {
              label: "name",
              value: "code"
            },
            cascaderItem: ["district"],
            search: true
          },
          {
            type: "select",
            label: "县区",
            hide: true,
            prop: "district",
            span: 8,
            width: "100%",
            dicUrl: `/invtory/districts/getChildCity?cityCode={{city}}`,
            props: {
              label: "name",
              value: "code"
            },
            search: true
          },
          {
            type: "input",
            label: "联系地址",
            prop: "contactAddr",
            span: 24,
            rules: [
              {
                required: true,
                message: "请输入联系地址！",
                trigger: "blur"
              }
            ]
          },
          {
            type: "input",
            label: "联系人",
            prop: "linkMan",
            span: 24
          },
          {
            type: "select",
            label: "所属行业",
            prop: "industry",
            dicUrl: "admin/dict/type/industry_type",
            span: 24
          },
          {
            type: "textarea",
            label: "备注",
            prop: "remark",
            span: 24,
            display: true
          },
          {
            type: "input",
            label: "创建用户",
            prop: "crteUser",
            span: 24,
            hide: true,
            // search: true,
            editDisplay: false,
            addDisplay: false
          },
          {
            type: "input",
            label: "最后更新用户",
            prop: "upteUser",
            span: 24,
            hide: true,
            // search: true,
            editDisplay: false,
            addDisplay: false
          },
          {
            type: "date",
            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",
            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_invsupplier_add,
          true
        ),
        delBtn: this.vaildData(
          this.permissions.generator_invsupplier_del,
          true
        ),
        editBtn: this.vaildData(
          this.permissions.generator_invsupplier_edit,
          true
        )
      };
    }
  },
  methods: {
    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.suplierName + " 的记录？",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(function() {
          return delObj(row.suplierId);
        })
        .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();
        });
    },
    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);
    }
  }
};
</script>