<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" v-model="form" 
      v-model:page="page" ref="crud"
      v-model:search="searchForm"
      @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange"
      @current-change="currentChange" @size-change="sizeChange" @refresh-change="refreshChange" @on-load="onLoad"
      :before-open="beforeOpen">
      <template #menu-left>
        <el-input v-model="checkCode" @keyup.enter.native="changeCode" style="width: 240px; margin-right: 24px" placeholder="请扫描条形码" />
        <el-button type="primary" @click="addView()">新增</el-button>
      </template>
      <template #menu-right> </template>
      <template #menu="scope">
        <el-button type="text"  @click="handeDetails(scope.row)">详情</el-button>
        <el-button type="text" v-if="scope.row.approvalStatus === 0" @click="handeExamine(scope.row)">审批</el-button>
      </template>
      <!-- <template #menu="scope">
        <el-button type="text" size="mini" @click="handle(scope.row.tbId)">处理</el-button>
      </template> -->
    </avue-crud>
    <otherIssuingDailog v-if="openShow" :showDialog="openShow" :checkCode="checkCode" @closeDialog="closeDialog"></otherIssuingDailog>
    <detailsDialog v-if="showDetail" :showDialog="showDetail" :id="checkId" @closeDialog="closeDialog"></detailsDialog>
    <el-dialog width="30%" v-model="approveDialog" title="审批">
            <el-form :model="auditForm" :rules="auditRules" ref="auditForm">
                <el-form-item label="是否审核通过" prop="approvalResult">
                    <el-radio v-model="auditForm.approvalResult" label="1">是</el-radio>
                    <el-radio v-model="auditForm.approvalResult" label="10">否</el-radio>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="approveDialog = false">取 消</el-button>
                    <el-button type="primary" @click="submit">确 定</el-button>
                </span>
            </template>
        </el-dialog>
  </basic-container>
</template>
<script>
import {getList,getDetail,approve} from "@/api/storeManagement/otherIssuing"
import otherIssuingDailog from "./otherIssuingDailog.vue";
import detailsDialog from './detailsDialog.vue';
export default {
  components: {
    otherIssuingDailog,
    detailsDialog,
  },
  data() {
    return {
      openShow: false,
      checkId:'',
      showDetail:false,
      checkCode:"",
      selectionList: [],
      query:{},
      searchForm:{},
      loading:false,
      option: {
        height: "auto",
        calcHeight: 32,
        tip: false,
        // size: "medium",
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: false,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtnText: "修改",
        editBtn:false,
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        viewBtnText: "详情",
        labelWidth: 120,
        menuWidth: 120,
        dialogWidth: 1200,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        index: true,
        searchLabelPosition: "left",
        searchLabelPosition: "left",
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: "center",
        gridBtn: false,
        searchMenuPosition: "right",
        addBtnIcon: " ",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        align: "center",
        column: [
          {
            label: "出库单号",
            prop: "sirCode",
            // bind: "stAccBill.billCode",
            sortable: true,
            filter: true,
            span: 12,
            search: true,
            width: 180,
          },
          {
            label: "出库日期",
            prop: "inOutDate",
            // bind: "stAccBill.billDate",
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            width: 180,
          },
          {
            label: "出库仓库",
            prop: "shName",
            // bind: "stAccBill.coStorehouse.shName",
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            width: 180,
          },
          {
            label: "出库类别",
            prop: "outType",
            type: "select",
            // bind: "stAccBill.billFlagStr",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width:110,
            dicData:[
              /*0:报废出库,1:料头出库,2.超额出库,3复检出库,4领用出库,5库转移,6退货出库,11不合格退库*/
              { label: "报废出库", value: 0 },
              { label: "料头出库", value: 1 },
              { label: "超额出库", value: 2 },
              { label: "复检出库", value: 3 },
              { label: "领用出库", value: 4 },
              { label: "库转移", value: 5 },
              { label: "退货出库", value: 6 },
              { label: "班组提请", value: 8 },
              { label: "不合格退库", value: 11 },

            ]
          },
          {
            label: "出库原因",
            prop: "inOutReason",
            type: "select",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 180,
            dicData:[
              {
                    value: '0',
                    label: "报废出库"
                },
                {
                    value: '1',
                    label: "料头出库"
                },
                {
                    value: '2',
                    label: "超额出库"
                },
                {
                    value: '3',
                    label: "复检出库"
                },
                {
                    value: '4',
                    label: "领用出库"
                },
                {
                    value: '5',
                    label: "库转移"
                },
                {
                    value: '6',
                    label: "退货出库"
                },
                {
                    value: '7',
                    label: "物料出库"
                },
                {
                    value: '8',
                    label: "班组提请"
                },
                {label:'不合格退库',value:'11'}
            ]
          },
          {
            label: "领料人",
            prop: "pickerName",
            search: true,
            sortable: true,
            filter: true,
            span: 12,
            width: 100,

          },
          {
            label: "制单人",
            prop: "createUserName",
            // bind: "stAccBill.createMan.userName",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            width: 100,
          },
          {
            label: "工装编号",
            prop: "toolCode",
            // bind: "pjMold.toolingCode",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            display: false,
            width: 180,
          },
          {
            label: "报废单号",
            prop: "bfCode",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            display: false,
            width: 180,
          },
          {
            label: "关联车间订单号",
            prop: "woCode",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            display: false,
            width: 150,
          },
          {
            label: "审批状态",
            prop: "approvalStatus",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            display: false,
            type: "select",
            width: 180,
            dicData: [
              {
                label: "审批中",
                value: 0,
              },
              {
                label: "审批通过",
                value: 1,
              },
              {
                label: "审批驳回",
                value: 10,
              },
              // {
              //   label: "审批不通过",
              //   value: 3,
              // },
            ],
          },
          {
            label: "审批意见",
            prop: "approvalOpinion",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            display: false,
            width: 180,
          },
          {
            label: "审批人",
            prop: "approverName",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            display: false,
            width: 180,
          },
          {
            label: "审批时间",
            prop: "approvalTime",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            display: false,
            type: "datetime",
            width: 180,
          },
          {
            label: "报废原因",
            prop: "bfType",
            // bind: "scrapReason",
            search: false,
            type:"select",
            sortable: true,
            // /**
            //  * 报废类型：到期维护报废
            //  */
            // public static final Short BF_TYPE_EXPIRED = 1;
            // /**
            //  * 报废类型：归还报废
            //  */
            // public static final Short BF_TYPE_RETURN = 2;
            // /**
            //  * 报废类型：玻璃饼报废
            //  */
            // public static final Short BF_TYPE_GLASS_CAKE = 3;
            dicData:[
              { label: "到期维护报废", value: 1 },
              { label: "未报废", value: 0 },
              { label: "归还报废", value: 2 },
              { label: "玻璃饼报废", value: 3 }
            ],
            filter: true,
            span: 24,
            display: false,
            width: 180,
          },
          {
            label: "提请单号",
            prop: "crCode",
            // bind: "stAccBill.billCode",
            sortable: true,
            filter: true,
            span: 12,
            search: true,
            width: 180,
          },
          {
            label: "备注",
            prop: "memo",
            search: false,
            sortable: true,
            filter: true,
            span: 12,
            display: false,
            width: 180,
          },

        ],
      },
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      approveDialog:false,
      auditForm:{},
      auditRules:{
        approvalResult:[{ required: true, message: '请选择是否通过审核', trigger: 'blur' }] 
      },
    };
  },
  created(){
    if (JSON.stringify(this.$store.state.client.otherOutParams) !== "{}") {
      this.query = this.$store.state.client.otherOutParams;
      this.searchForm = this.$store.state.client.otherOutParams;
    }
  },
  methods: {
    addView() {
      this.openShow = true;
    },
    changeCode(val){
      if(this.checkCode == "") return
      this.openShow = true
    },
    beforeOpen(done, type){
      if(['view'].includes(type)){
        getDetail({id:this.form.id}).then(res =>{
          console.log('res=============',res)
        })
      }else{
        done();
      }
    },
    searchChange(params, done){
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad()
      this.$store.dispatch("changeSetting", {
        title: "otherOutParams",
        form: this.query,
      });
      done()
    },
    searchReset(){
      this.query = {}
      this.onLoad()
    },
    currentChange(currentPage){
      this.page.currentPage = currentPage
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize
    },
    closeDialog(val){
      this.openShow = false
      this.showDetail = false
      if(val){
        this.onLoad()
      }
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => { });
    },
    handeExamine(row){
      this.auditForm = {}
      this.auditForm.id = row.id
      this.approveDialog = true
    },
    handeDetails(row){
      this.showDetail = true
      this.checkId = row.id
    },
    submit(){
      this.$refs.auditForm.validate(valid => {
        if(valid){
            approve({...this.auditForm}).then(res =>{
                if(res.data.code == 200){
                    this.$message.success('审批成功')
                    this.approveDialog = false
                    this.onLoad()
                }
            })
        }
      })
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    onLoad() {
      this.loading = true;
      getList({
        current: this.page.currentPage,
        size: this.page.pageSize,
        ...this.query,
      }).then(res => {
        res.data.data.records.forEach(item => {
          item.approvalStatus = item.approvalStatus === -1 ? '' : item.approvalStatus
        })
        this.data = res.data.data.records
        this.page.total = res.data.data.total
        this.loading = false
      })
      // this.data = [
      //   {
      //     tbId: 'OT2023001',
      //     stAccBill: {
      //       billCode: 'OT2023001',
      //       billDate: '2023-06-15',
      //       coStorehouse: {
      //         shName: '原料仓库'
      //       },
      //       billFlagStr: '其他出库',
      //       pfUserInfo: {
      //         userCode: 'U001',
      //         userName: '张三'
      //       },
      //       createMan: {
      //         userName: '李四'
      //       }
      //     },
      //     outTypStr: '生产领料',
      //     pjMold: {
      //       toolingCode: 'MOLD001'
      //     },
      //     bfCode: 'BF2023001',
      //     orderCode: 'WO2023001',
      //     approvalStatus: 2,
      //     approvalOpinion: '审批通过',
      //     approvalUser: '王五',
      //     approvalTime: '2023-06-15 14:30:00',
      //     scrapReason: '无',
      //     memo: '紧急生产需求'
      //   },
      //   {
      //     tbId: 'OT2023002',
      //     stAccBill: {
      //       billCode: 'OT2023002',
      //       billDate: '2023-06-16',
      //       coStorehouse: {
      //         shName: '成品仓库'
      //       },
      //       billFlagStr: '其他出库',
      //       pfUserInfo: {
      //         userCode: 'U002',
      //         userName: '赵六'
      //       },
      //       createMan: {
      //         userName: '钱七'
      //       }
      //     },
      //     outTypStr: '样品出库',
      //     pjMold: {
      //       toolingCode: 'MOLD002'
      //     },
      //     bfCode: 'BF2023002',
      //     orderCode: 'WO2023002',
      //     approvalStatus: 2,
      //     approvalOpinion: '审批通过',
      //     approvalUser: '孙八',
      //     approvalTime: '2023-06-16 10:15:00',
      //     scrapReason: '无',
      //     memo: '客户样品需求'
      //   }
      // ];
      // this.page.total = this.data.length;
      // this.loading = false;
      // setTimeout(() => {
      //   this.selectionClear();
      // }, 500);
    },
  },
};
</script>
<style lang="scss" scoped></style>
