<template>
  <div>
    <el-card>
      <el-form :model="filterQuery" class="query-form" size="mini" label-width="100px" v-if="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item class="query-form-item" label="单据类型:">
              <el-input v-model="filterQuery.name" placeholder="请输入单据类型" clearable style="width: 90%"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item class="query-form-item" label="出入库类型:">
              <el-select v-model="filterQuery.mainAction" style="width: 90%" placeholder="请选择出入库类型">
                <el-option label="全部" value=""></el-option>
                <el-option label="入库" value="WareHouseIn"></el-option>
                <el-option label="出库" value="WareHouseOut"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item class="query-form-item" label="是否启用:">
              <el-select v-model="filterQuery.enable" style="width: 90%" placeholder="是否启用">
                <el-option label="全部" value=""></el-option>
                <el-option label="已启用" value="1"></el-option>
                <el-option label="未启用" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item class="query-form-item" label="仓库类型:">
              <el-select v-model="filterQuery.actionType" style="width: 90%" placeholder="请选择仓库类型">
                <el-option label="入账库" :value="1"></el-option>
                <el-option label="预验收库" :value="2"></el-option>
                <el-option label="寄售库" :value="3"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>


      <div class="top-right-btn">
        <el-button-group style="width: 600px">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
          <el-button type="primary" icon="el-icon-plus" @click="handleAddClick">新增</el-button>
          <el-button type="primary" icon="el-icon-download" @click="exportJsonFile">导出单据类型</el-button>
          <el-upload
            :action="uploadFileUrl"
            multiple
            :limit="3"
            :headers="headers"
            :show-file-list="false"
            :on-success="handleChange"
            :file-list="fileList"
          >
            <el-button icon="el-icon-upload2" type="primary">导入单据类型</el-button>
          </el-upload>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row>
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column label="单据类型" prop="name"></el-table-column>
        <el-table-column label="出入库类型" prop="mainAction">
          <template slot-scope="scope">
            <span>{{ mainActionMap[scope.row.mainAction] }}</span>
          </template>
        </el-table-column>
        <!--        <el-table-column label="扫码单据类型代码" prop="action" fixed></el-table-column>-->
        <el-table-column label="是否启用" prop="enable">
          <template slot-scope="scope">
            <span>{{ enableMap[scope.row.enable] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="校验业务单" prop="checkEnable">
          <template slot-scope="scope">
            <span>{{ enableMap[scope.row.checkEnable] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="一次审核" prop="thrCheckEnable">
          <template slot-scope="scope">
            <span>{{ enableMap[scope.row.thrCheckEnable] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="二次审核" prop="secCheckEnable">
          <template slot-scope="scope">
            <span>{{ enableMap[scope.row.secCheckEnable] }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click.native.stop="handleModifyClick(scope.row)"
            >编辑
            </el-button
            >
            <el-button
              type="text"
              :disabled="!configParams.typeScan"
              @click.native.stop="deleteDialog(scope.row)"
            >删除
            </el-button
            >
          </template>

        </el-table-column>
      </el-table>

      <el-dialog
        title="新增扫码单据类型"
        :visible.sync="addDialogVisible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="addDialogVisible"
        class="dialog-two"
        width="60%"
        top="5vh"
      >
        <modifyDialog :inputQuery="inputQuery"></modifyDialog>
        <div slot="footer" class="dialog-footer">
          <!--           :disabled="!configParams.typeScan"-->
          <el-button type="primary" size="small" @click="onAddSubmit"

          >提交
          </el-button
          >
          <el-button size="small" @click="cancelDialog"
          >取消
          </el-button
          >
        </div>
      </el-dialog>

      <el-dialog
        title="编辑单据类型"
        :visible.sync="modifyDialogVisible"
        width="70%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="modifyDialogVisible"
        :before-close="cancelDialog"
      >
        <modifyDialog :inputQuery="inputQuery" :modifySubmit="onModifySubmit"></modifyDialog>
        <div style="text-align: center;margin-top: 12px">
          <el-button type="primary" size="small" icon="search" @click="onModifySubmit"
                     :disabled="!configParams.typeScan" :loading="subLoading"
          >提交
          </el-button
          >
          <el-button type="primary" size="small" icon="search" @click="cancelDialog"
          >取消
          </el-button
          >
        </div>
      </el-dialog>

      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getList"
      ></pagination>
    </el-card>
  </div>
</template>
<script>
import {getBusTypeList, deleteBusType, updateBusType, insertBusType, exportFile} from '@/api/basic/busType'
import modifyDialog from './bussinessTypeModify'
import axios from 'axios'
import store from '@/store'
import {findConfig} from '@/api/sync/spsSyncStatus'
import {isBlank} from '@/utils/strUtil'

export default {
  data() {
    return {
      filterQuery: {
        name: null,
        mainAction: null,
        enable: '1',
        page: 1,
        limit: 20
      },
      showSearch: true,
      addDialogVisible: false,
      modifyDialogVisible: false,
      list: [],
      inputQuery: {
        action: '',
        name: '',
        enable: null,
        remark: '',
        mainAction: '',
        devInType: 0,
        thirdSysFk: '',
        id: '',
        checkEnable: false,
        genUnit: false,
        innerOrder: false,
        spUse: null,
        secCheckEnable: null,
        checkUdims: null,
        checkPdaEd: null,
        checkPdaUn: null,
        checkPc: null,
        checkWebNew: null,
        checkWebNewType: null,
        checkChange: null,
        checkCopy: null,
        secCheckUdims: null,
        secCheckPdaEd: null,
        secCheckPdaUn: null,
        secCheckPc: null,
        secCheckWebNew: null,
        secCheckChange: null,
        thirdAction: null,
        secCheckSp: null,
        checkSp: null,
        secCheckCopy: null,
        corpType: null,
        storageCode: null,
        supplementOrderType: null,
        defaultUnit: null,
        useDyCount: 0,
        expireTip: true,
        prefix: null,
        outToSpms: null,
        ullageFill: null,
        scanPreIn: false,
        vailInv: false,
        vailInvBatchNo: false,
        entrutSpms: null,
        codeFillCheck: true,
        defaultInv: null,
        defaultSubInv: null,
        orderVisibleType: null,
        advanceType: null,
        preIn: null,
        originAction: null,
        checkBalance: null,
        secCheckBalance: null,
        preInBack: false,
        spaceOut: 0,
        vailDispatch: false,
        vailGroupBuy: 0,
        inStock: false,
        actionType: 1,
        fillCodeRel: false,
        thrCheckEnable: null,
        thrCheckWebNew: null,
        thrCheckPdaUn: null,
        thrCheckPdaEd: null,
        thrCheckUdims: null,
        thrCheckPc: null,
        thrCheckSp: null,
        thrCheckChange: null,
        thrCheckBalance: null,
        thrCheckCopy: null,
        advancePreIn: false,
        checkVailDate: 0, //校验近效期
        checkExpire: true,   //校验过期
        checkCertExpire: false,//校验证书过期
        editType: 2,
        groupBuySplit: null,
        vailHcType:null,
        checkReview: null,
        secCheckReview: null,
        thrCheckReview: null,
        inPreInBack: 1,
        deptNoInvType: 0,
        vailPrescribe: false,
        inputSick: false,
        vailProductAttributes: null, //校验产品采购类型
      },
      enableMap: {
        true: '是',
        false: '否'
      },
      mainActionMap: {
        WareHouseIn: '入库',
        WareHouseOut: '出库'
      },
      fileList: [],
      total: 0,
      multipleSelection: [],
      uploadFileUrl: null,
      headers: {},
      configParams: {},
      loading: false,
      subLoading: false,

    }
  },

  methods: {
    onReset() {
      this.$router.push({
        path: ''
      })
      this.filterQuery = {
        name: null,
        mainAction: null,
        enable: '1',
        page: 1,
        limit: 20
      }
      this.getList()
    },
    cancelDialog() {
      this.modifyDialogVisible = false
      this.addDialogVisible = false
      this.getList()
    },
    search() {
      this.filterQuery.page = 1
      this.getList()
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    getList() {
      this.loading = true
      getBusTypeList(this.filterQuery)
        .then((response) => {
          if (response.code === 20000) {
            this.list = response.data.list || []
            this.total = response.data.total || 0
          } else {
            this.$message.error(response.message)
          }
          this.loading = false
        })
        .catch(() => {
          this.loading = false
          this.list = []
          this.total = 0
        })
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val
      this.getList()
    },

    onAddSubmit() {
      //默认初始化
      if (this.inputQuery.corpType == null) {
        this.inputQuery.corpType = 2
      }
      if (isBlank(this.inputQuery.name)) {
        this.$message.error('单据类型名称不能为空！')
        return
      }
      if (this.inputQuery.mainAction == '' || this.inputQuery.mainAction == null) {
        this.$message.error('出入库类型不能为空！')
        return
      }
      if (isBlank(this.inputQuery.name)) {
        this.$message.error('单据类型名称不能为空！')
        return
      }
      // if (isBlank(this.inputQuery.actionType)) {
      //   this.$message.error("请选择单据类型！");
      //   return;
      // }
      if (isBlank(this.inputQuery.inStock)) {
        this.$message.error('请选择是否更改库存！')
        return
      }

      if (this.inputQuery.preInBack == 1) {
        if (isBlank(this.inputQuery.backPreinType)) {
          this.$message.error('请选择预验收剩余库存退库方式！')
          return
        }
        if (isBlank(this.inputQuery.preInBackAction)) {
          this.$message.error('请选择预验收退库单据类型！')
          return
        }
      }

      if (isBlank(this.inputQuery.sortNum)) {
        this.$message.error("排序不能为空！");
        return;
      } else {
        if (this.inputQuery.sortNum < 0) {
          this.$message.error("排序不能小于0！");
          return;

        } else if (this.inputQuery.sortNum > 999) {
          this.$message.error("排序不能大于999！");
          return;
        }
      }

      // if (isBlank(this.inputQuery.busType)) {
      //   this.$message.error("请选择特定页面！");
      //   return;
      // }
      insertBusType(this.inputQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.loading = false
            this.cancelDialog()
            this.getList()
          } else {
            this.$message.error(response.message)
          }
        })
        .catch(() => {
          this.loading = false
        });
    },

    onModifySubmit() {

      if (isBlank(this.inputQuery.name)) {
        this.$message.error('单据类型名称不能为空！')
        return
      }
      if (this.inputQuery.mainAction == '' || this.inputQuery.mainAction == null) {
        this.$message.error('出入库类型不能为空！')
        return
      }
      if (isBlank(this.inputQuery.name)) {
        this.$message.error('单据类型名称不能为空！')
        return
      }
      // if (isBlank(this.inputQuery.actionType)) {
      //   this.$message.error("请选择单据类型！");
      //   return;
      // }
      if (isBlank(this.inputQuery.inStock)) {
        this.$message.error('请选择是否更改库存！')
        return
      }
      if (this.inputQuery.sortNum > 999) {
        this.$message.error('排序不能超过999！')
        return
      }
      // if (isBlank(this.inputQuery.busType)) {
      //   this.$message.error("请选择特定页面！");
      //   return;
      // }
      if(!(this.inputQuery.mainAction =='WareHouseIn' && this.inputQuery.actionType == 1)){
        this.inputQuery.devInType = 0;
      }
      this.subLoading = true
      updateBusType(this.inputQuery)
        .then((response) => {
          this.subLoading = false
          if (response.code == 20000) {
            // this.loading = false
            this.cancelDialog()
            this.getList()
          } else {
            this.$message.error(response.message)
          }
        })
        .catch(() => {
          this.subLoading = false
          this.cancelDialog()
        })
    },
    exportJsonFile() {
      exportFile().then((response) => {
        const binaryData = []
        binaryData.push(response)
        let url = window.URL.createObjectURL(
          new Blob(binaryData, {type: 'application/json'})
        )
        this.loading = false
        const eleLink = document.createElement('a')
        // var timestamp = "yyyy-MM-dd_hh:mm";
        eleLink.download = '扫码单据类型导出' + '.json'
        eleLink.style.display = 'none'
        eleLink.href = url
        document.body.appendChild(eleLink)
        eleLink.click()
        document.body.removeChild(eleLink)
      }).catch(() => {
        this.loading = false
      })
    },

    handleAddClick() {
      this.inputQuery = {
        action: '',
        name: '',
        enable: true,
        remark: '',
        mainAction: 'WareHouseIn',
        thirdSysFk: '',
        id: '',
        checkEnable: false,
        genUnit: false,
        innerOrder: false,
        spUse: 0,
        secCheckEnable: null,
        checkUdims: null,
        checkPdaEd: null,
        checkPdaUn: null,
        checkPc: null,
        checkWebNew: null,
        checkWebNewType: null,
        checkChange: null,
        checkCopy: null,
        secCheckUdims: null,
        secCheckPdaEd: null,
        secCheckPdaUn: null,
        secCheckPc: null,
        secCheckWebNew: null,
        secCheckChange: null,
        thirdAction: null,
        secCheckSp: null,
        checkSp: null,
        checkBalance: null,
        secCheckBalance: null,
        secCheckCopy: null,
        corpType: 2,
        storageCode: null,
        supplementOrderType: null,
        defaultUnit: null,
        useDyCount: 0,
        expireTip: true,
        prefix: null,
        outToSpms: null,
        ullageFill: null,
        scanPreIn: false,
        vailInv: false,
        vailInvBatchNo: false,
        entrutSpms: null,
        codeFillCheck: true,
        defaultInv: null,
        defaultSubInv: null,
        orderVisibleType: null,
        advanceType: null,
        preIn: null,
        originAction: null,
        preInBack: false,
        spaceOut: 0,
        vailDispatch: false,
        vailGroupBuy: 0,
        inStock: false,
        actionType: 1,
        fillCodeRel: false,
        thrCheckEnable: null,
        thrCheckWebNew: null,
        thrCheckPdaUn: null,
        thrCheckPdaEd: null,
        thrCheckUdims: null,
        thrCheckPc: null,
        thrCheckSp: null,
        thrCheckChange: null,
        thrCheckBalance: null,
        thrCheckCopy: null,
        checkReview: null,
        secCheckReview: null,
        thrCheckReview: null,
        advancePreIn: false,
        checkVailDate: 0, //校验近效期
        checkExpire: 2,   //校验过期
        checkCertExpire: 0, //校验证书过期
        editType: 2,
        groupBuySplit: null,
        backPreinType: 1,
        sortNum: 999,
        inPreInBack: 1,
        deptNoInvType: 0,
        vailPrescribe: false,
        inputSick: false,
      }
      this.addDialogVisible = true
    },

    handleModifyClick(row) {
      this.inputQuery = row
      this.modifyDialogVisible = true
    },

    deleteDialog(rowId) {
      this.$confirm('此操作将永久删除该扫码类型信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.deleteOrders(rowId)
        })
        .catch(() => {
        })
    },

    deleteOrders(data) {
      this.loading = true
      let tquery = {
        id: data.id + ''
      }
      deleteBusType(tquery)
        .then((response) => {
          this.getList()
          if (response.code == 20000) {
            this.$message({
              type: 'success',
              message: '删除成功！'
            })
          } else {
            this.$message.error(response.message)
          }

        })
        .catch(() => {
        })
    },
    handleChange(response, files, fileList) {
      if (response.code != 20000) {
        this.$message.error(response.message)
      } else {
        this.$message.success(response.data)
        this.getList()
      }
    },
    init() {
      this.uploadFileUrl = process.env.VUE_APP_BASE_API + '/udiwms/bussinessType/file/upload'
      this.headers = {
        ADMINID: store.getters.adminId,
        ADMINTOKEN: store.getters.token
      }
    },
    getSyncConfig() {
      findConfig()
        .then((response) => {
          if (response.code == 20000) {
            this.configParams = response.data
          }
        })
        .catch(() => {
        })
    }
  },
  components: {
    modifyDialog
  },
  mounted() {
  },
  created() {
    this.init()
    this.getSyncConfig()
    this.getList()
  }
}
</script>

<style scoped>
.el-form-item--mini.el-form-item {
  margin-bottom: 9px;
}
</style>
