<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               :header-cell-class-name="headerClass"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.material_delete"
                   @click="handleDelete">批量删除
        </el-button>
      </template>
      <template slot="menuRight">
        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleDetailAdd">新增
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleIn">入库
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleOut">出库
        </el-button>
        <el-button type="primary"
                   size="small"
                   plain
                   @click="handleExport()">导出
        </el-button>
      </template>
      <template #menu="{row,index,size}">
        <el-button @click="handleDetailSearch(row,index)" type="text">查看</el-button>
        <el-button @click="rowDel(row)" type="text">删除</el-button>
      </template>
    </avue-crud>
    <el-dialog title="入库信息"
               append-to-body
               :visible.sync="inBox"
               width="480px"
               @close="handleClose">
      <avue-form :option="optionIn"
                 v-model="inForm"
                 ref="inForm">
        <!--                 :upload-after="uploadAfter">-->
      </avue-form>
      <span slot="footer" class="dialog-footer">
            <el-button @click="inBox = false">取 消</el-button>
            <el-button type="primary"
                       @click="submitIn">确 定</el-button>
          </span>
    </el-dialog>
    <el-dialog title="出库信息"
               append-to-body
               :visible.sync="outBox"
               width="480px"
               @close="handleClose">
      <avue-form :option="optionOut"
                 v-model="outForm"
                 ref="outForm">
        <!--                 :upload-after="uploadAfter">-->
      </avue-form>
      <span slot="footer" class="dialog-footer">
            <el-button @click="outBox = false">取 消</el-button>
            <el-button type="primary"
                       @click="submitOut">确 定</el-button>
          </span>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getList, getDetail, remove} from "@/api/spares/material";
  import {in_add} from "@/api/spares/inbound";
  import {out_add} from "@/api/spares/outbound";
  import {mapGetters} from "vuex";
  import expUtil from "@/util/exportUtil";
  import {getToken} from "@/util/auth";
  import request from "@/router/axios";

  export default {
    data() {
      return {
        form: {},
        query: {},
        search:{},
        loading: true,
        inBox: false,
        outBox: false,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: false,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "物资编号",
              prop: "materialNo",
              search: true,
              rules: [{
                required: true,
                message: "请输入物资编号",
                trigger: "blur"
              }]
            },
            {
              label: "场站",
              prop: "station",
              type: "select",
              search: true,
              dicUrl: "/api/daf-system/dict/dictionary?code=station",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入场站",
                trigger: "blur"
              }]
            },
            {
              label: "物资名称",
              prop: "materialName",
              search: true,
              rules: [{
                required: true,
                message: "请输入物资名称",
                trigger: "blur"
              }]
            },
            {
              label: "物资分类码",
              prop: "materialClassCode",
              hide: true,
              rules: [{
                required: true,
                message: "请输入物资分类码",
                trigger: "blur"
              }]
            },
            {
              label: "规格型号",
              prop: "model",
              search: true,
              rules: [{
                required: true,
                message: "请输入规格型号",
                trigger: "blur"
              }]
            },
            {
              label: "制造商名称",
              prop: "manufacturerName",
              hide: true,
              rules: [{
                required: true,
                message: "请输入制造商名称",
                trigger: "blur"
              }]
            },
            {
              label: "供应商名称",
              prop: "sipplierName",
              hide: true,
              rules: [{
                required: true,
                message: "请输入供应商名称",
                trigger: "blur"
              }]
            },
            {
              label: "所属仓库",
              prop: "warehouse",
              hide: true,
              rules: [{
                required: true,
                message: "请输入所属仓库",
                trigger: "blur"
              }]
            },
            {
              label: "库存数量",
              prop: "inventoryCount",
              search: true,
              rules: [{
                required: true,
                message: "请输入库存数量",
                trigger: "blur"
              }]
            },
            {
              label: "物资描述",
              prop: "materialDescription",
              rules: [{
                required: true,
                message: "请输入物资描述",
                trigger: "blur"
              }]
            },

            {
              label: "审核状态",
              prop: "istatus",
              hide: true,
              dicUrl: "/api/daf-system/dict/dictionary?code=check_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入审核状态",
                trigger: "blur"
              }]
            },
          ]
        },
        inForm: {},
        optionIn: {
          submitBtn: false,
          emptyBtn: false,
          menuAlign: "left",
          column: [
            {
              label: "物资编号",
              prop: "materialNo",
              type:"select",
              span: 22,
              dicUrl: "/api/spares/material/getNoList",
              props: {
                label: "materialNo",
                value: "materialNo",
              },
              event: {
                change: (val) => {
                  if (!val) return
                  if (val) {
                    request({
                      url: '/api/spares/material/getDetailList',
                      method: 'get',
                      params: {
                        materialNo: val
                      }
                    }).then((res) => {
                      this.inForm.materialName = res.data.data.materialName;
                      this.inForm.model = res.data.data.model;
                      // this.inForm.warehouse = res.data.data.warehouseName;
                    })
                  }
                },
              },
              rules: [{
                required: true,
                message: "请输入物资编号",
                trigger: "blur"
              }]
            },
            {
              label: "物资名称",
              prop: "materialName",
              span: 22,
              disabled: true,
            },
            {
              label: "规格型号",
              prop: "model",
              span: 22,
              disabled: true,
            },
            {
              label: "所属仓库",
              prop: "warehouse",
              span: 22,
              disabled: true,

            },
            {
              label: "入库数量",
              prop: "inboundCount",
              span: 22,
              rules: [{
                required: true,
                message: "请输入入库数量",
                trigger: "blur"
              }]
            },
            {
              label: "入库申领人",
              prop: "inboundClaimants",
              span: 22,
              labelWidth:100,
              rules: [{
                required: true,
                message: "请输入入库申领人",
                trigger: "blur"
              }]
            },
            {
              label: "入库经办人",
              prop: "inboundManager",
              span: 22,
              labelWidth: 100,
              rules: [{
                required: true,
                message: "请输入入库经办人",
                trigger: "blur"
              }]
            },
          ]
        },
        outForm: {},
        optionOut: {
          submitBtn: false,
          emptyBtn: false,
          column: [
            {
              label: "物资编号",
              prop: "materialNo",
              span: 22,
              dicUrl: "/api/spares/material/getNoList",
              props: {
                label: "materialNo",
                value: "materialNo",
              },
              event: {
                change: (val) => {
                  if (!val) return
                  if (val) {
                    request({
                      url: '/api/spares/material/getDetailList',
                      method: 'get',
                      params: {
                        materialNo: val
                      }
                    }).then((res) => {
                      this.inForm.materialName = res.data.data.materialName;
                      this.inForm.model = res.data.data.model;
                      // this.inForm.warehouse = res.data.data.warehouseName;
                    })
                  }
                },
              },
              rules: [{
                required: true,
                message: "请输入物资编号",
                trigger: "blur"
              }]
            },
            {
              label: "物资名称",
              prop: "materialName",
              span: 22,
              disabled: true,
            },
            {
              label: "规格型号",
              prop: "model",
              span: 22,
              disabled: true,
            },
            {
              label: "所属仓库",
              prop: "warehouse",
              span: 22,
              disabled: true,

            },
            {
              label: "出库数量",
              prop: "outboundCount",
              span: 22,
              rules: [{
                required: true,
                message: "请输入入库数量",
                trigger: "blur"
              }]
            },
            {
              label: "出库申领人",
              prop: "outboundClaimants",
              span: 22,
              labelWidth: 100,
              rules: [{
                required: true,
                message: "请输入出库申领人",
                trigger: "blur"
              }]
            },
            {
              label: "出库经办人",
              prop: "outboundManager",
              span: 22,
              labelWidth: 100,
              rules: [{
                required: true,
                message: "请输入出库经办人",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
          // this.option.column = this.option.column.filter(v => {
          //     return this.permission['material_col_' + v.prop]
          // })
        return {
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn: false
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      handleDetailAdd() {
        this.$router.push({
          path: "/spares/materialDetail",
          query: {
            frameMode:"add"
          },
        });
      },
      handleIn(){
        this.inBox = true;
        this.$refs.inForm.resetForm();
      },
      submitIn(row, done, loading) {
        this.$refs.inForm.validate((valid, done, msg) => {
          if (valid) {
            done()
            in_add(this.inForm).then(() => {
              this.$refs.inForm.resetForm();
              this.inBox = false;
              this.onLoad(this.page);
              this.$message({
                type: "success",
                message: "操作成功!"
              });
            }, error => {
              window.console.log(error);
              loading();
            });
          } else {
            this.$message({
              type: "warning",
              message: "请输入必要信息!"
            });
          }
        });
      },
      handleOut(){
        this.outBox = true;
        this.$refs.outForm.resetForm();
      },
      submitOut(row, done, loading) {
        this.$refs.outForm.validate((valid, done, msg) => {
          if (valid) {
            done()
            out_add(this.outForm).then(() => {
              this.$refs.outForm.resetForm();
              this.outBox = false;
              this.onLoad(this.page);
              this.$message({
                type: "success",
                message: "操作成功!"
              });
            }, error => {
              window.console.log(error);
              loading();
            });
          } else {
            this.$message({
              type: "warning",
              message: "请输入必要信息!"
            });
          }
        });
      },
      handleExport() {
        this.$confirm("是否导出数据?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          expUtil.excelExport(`/api/spares/material/export?daf-auth=${getToken()}`, this.search, ['station']);
        });
      },
      handleDetailSearch(row) {
        this.$router.push({
          path: "/spares/materialDetail",
          query: {
            frameMode:"view",
            id: row.id,
          },
        });
      },
      headerClass() {
        return 'header-class'
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
.header-class{
  background-color: #3366cc !important;
  color: #FFFFFF !important;
}
</style>
