<template>
  <!-- 列表 -->
  <div>
    <AutoResizer :given-height="250">
      <template #default="{height}">
        <el-table
          v-if="columnsList.length > 0"
          v-loading="loading"
          :data="list"
          :height="height"
          border
        >
          <el-table-column
            v-if="columnsList[0].visible"
            :key="Math.random()"
            label="物料编号"
            align="center"
            prop="code"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            v-if="columnsList[1].visible"
            :key="Math.random()"
            label="物料名称"
            align="center"
            prop="name"
            width="150"
          />
          <el-table-column
            v-if="columnsList[2].visible"
            :key="Math.random()"
            label="版本号"
            align="center"
            prop="version"
          />
          <el-table-column
            v-if="columnsList[3].visible"
            :key="Math.random()"
            label="提交人"
            align="center"
            prop="updater"
          />
          <el-table-column
            v-if="columnsList[4].visible"
            :key="Math.random()"
            label="审核状态"
            align="center"
            prop="auditStatus"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_AUDIT_STATUS" :value="scope.row.auditStatus" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[5].visible"
            :key="Math.random()"
            label="启用标志"
            align="center"
            prop="status"
          >
            <template slot-scope="scope">
              <dict-tag :type="DICT_TYPE.WMS_ENABLE_FLAG" :value="scope.row.status" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="columnsList[6].visible"
            :key="Math.random()"
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
            width="220"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleView(scope.row)"
              >查看</el-button>
              <el-button
                v-hasPermi="['wms:material-additional:update']"
                type="text"
                icon="el-icon-check"
                size="mini"
                @click="handleApproval(scope.row)"
              >批核</el-button>
              <el-button
                v-hasPermi="['wms:material-additional-log:query']"
                size="mini"
                type="text"
                icon="el-icon-view"
                @click="handleLogs(scope.row)"
              >审批日志</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </AutoResizer>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 明细 -->
    <StandardApprovalDetails ref="standardApprovalDetails" />
    <!-- 修改检验标准 -->
    <UpdateInspection ref="updateInspection" />
    <!-- 批核选择 -->
    <el-dialog
      title="审核"
      :visible.sync="auditVisible"
      width="30%"
    >
      <el-form
        ref="auditForm"
        :model="auditParams"
        size="small"
        label-width="98px"
        :rules="rules"
      >
        <el-row :gutter="20">
          <el-col>
            <el-form-item label="审核结果" prop="flag">
              <el-select v-model="auditParams.flag" placeholder="请选择审核结果" @change="change">
                <el-option
                  v-for="item in auditSelect"
                  :key="item.code"
                  :label="item.label"
                  :value="item.code"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col>
            <el-form-item v-show="reasonIsShow" label="驳回原因" prop="reason">
              <el-input
                v-model="auditParams.reason"
                type="textarea"
                :rows="2"
                placeholder="请输入驳回的原因"
              />
            </el-form-item>

          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="auditVisible = false">取 消</el-button>
        <el-button :loading="ensureLoading" type="primary" @click="ensureApproval">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 审批日志 -->
    <ApprovalLogs ref="approvalLogs" />
  </div>

</template>

<script>
import { getMaterialAdditionalPage, updateAuditPass, updateAuditUnPass } from '@/api/wms/quality/inspectStandardApproval'
import StandardApprovalDetails from './StandardApprovalDetails.vue'
import UpdateInspection from '../../iqcInspection/components/UpdateInspection.vue'
import ApprovalLogs from './ApprovalLogs.vue'
import { getFormColumnsList } from '@/api/system/saveColumns/index'
import { mapGetters } from 'vuex'
import { isArray } from 'min-dash'

export default {
  name: 'AwaitApproval',
  components: {
    StandardApprovalDetails,
    UpdateInspection,
    ApprovalLogs
  },
  props: {
    'activeName': {
      type: String,
      default: ''
    },
    'queryParamspro': {
      type: Object,
      default: () => {}
    }
  },
  inject: ['paramsQuery'],
  data() {
    return {
      // 遮罩层
      loading: false,
      ensureLoading: false,
      // 导出遮罩层
      exportLoading: false,
      // 批核
      reasonIsShow: false,
      ditVisible: false,
      auditVisible: false,
      // 显示搜索条件
      showSearch: true,
      // 检验选择
      open: false,
      // 总条数
      total: 0,
      // 领料单列表
      list: [],
      //   用户列表
      users: [],
      // 弹出层标题
      title: '',
      //   是否显示查看弹出层
      viewOpen: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        auditStatus: 0,
        processNo: 'SALESOUT'
      },
      auditParams: {},
      excueParams: {},
      //   表单参数
      form: {

      },
      // 审批表单参数
      auditForm: {},
      // 表单校验
      rules: {
        reason: [{ required: false, message: '驳回原因不能为空', trigger: 'blur' }],
        flag: [{ required: true, message: '请选择审核结果', trigger: 'change' }]
      },
      // 批核选择
      auditSelect: [
        {
          label: '驳回',
          code: 0
        },
        {
          label: '通过',
          code: 1
        }
      ],
      // 显示隐藏列参数
      columns: [
        { key: 0, label: '物料编码', visible: true },
        { key: 1, label: '物料名称', visible: true },
        { key: 2, label: '版本号', visible: true },
        { key: 3, label: '提交人', visible: true },
        { key: 4, label: '审核状态', visible: true },
        { key: 5, label: '启用标志', visible: true },
        { key: 6, label: '创建时间', visible: true },
        { key: 7, label: '审核人', visible: true },
        { key: 8, label: '审核时间', visible: true }
      ],
      // 显示隐藏持久化参数
      columnsList: []
    }
  },
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  created() {
    // 获取显隐列列表
    this.getColumnsList()
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      const params = {
        ...this.queryParams,
        ...this.paramsQuery
      }
      getMaterialAdditionalPage(params).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        wmsIncomingReceiptId: undefined,
        orderType: undefined,
        erpStockId: undefined,
        wmsStoreAreaId: undefined,
        wmsStoreId: undefined,
        inQty: undefined,
        funitId: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    // 查看明细
    handleView(row) {
      // const { id } = row
      // this.$refs.standardApprovalDetails.queryParams.wmsMaterialAdditionalIdNew = id
      // this.$refs.standardApprovalDetails.visible = true
      const { code, name, version, id, masterName, processNo } = row
      this.$refs.updateInspection.queryParams.materialNo = code
      this.$refs.updateInspection.queryParams.name = name
      this.$refs.updateInspection.queryParams.wmsMaterialAdditionalId = id
      this.$refs.updateInspection.queryParams.processNo = processNo
      this.$refs.updateInspection.queryObj.version = version
      this.$refs.updateInspection.queryParams.type = 2
      this.$refs.updateInspection.queryObj.name = masterName
      this.$refs.updateInspection.saveDisable = true
      this.$refs.updateInspection.commitDisable = true
      this.$refs.updateInspection.visible = true
      this.$refs.updateInspection.selectDisable = true
      this.$refs.updateInspection.refreshDisable = true
      this.$refs.updateInspection.inspectDisable = true
    },
    change(v) {
      if (v === 0) {
        this.reasonIsShow = true
        this.rules.reason[0].required = true
      } else {
        this.reasonIsShow = false
        this.rules.reason[0].required = false
      }
    },
    ensureApproval() {
      const { id, wmsMaterialItemMasterId, code, name, flag, reason } = this.auditParams
      this.$refs['auditForm'].validate(valid => {
        if (!valid) {
          return
        }
        const params = {
          code,
          name,
          wmsMaterialAdditionalId: id,
          wmsMaterialItemMasterId,
          auditComment: reason
        }
        this.ensureLoading = true
        if (flag === 0) {
          updateAuditUnPass(params).then(res => {
            this.$modal.msgSuccess('审批不通过')
            this.auditVisible = false
            this.getList()
            this.$emit('noPassApproval')
            this.ensureLoading = false
          }).finally(() => {
            this.ensureLoading = false
          })
        } else {
          const passParams = {
            id
          }
          updateAuditPass(passParams).then(res => {
            this.$modal.msgSuccess('审批通过')
            this.auditVisible = false
            this.getList()
            this.$emit('approvalList')
            this.ensureLoading = false
          }).finally(() => {
            this.ensureLoading = false
          })
        }
      })
    },
    // 批核
    handleApproval(row) {
      this.auditParams = row
      this.auditVisible = true
    },
    // 审批日志
    handleLogs(row) {
      const { id } = row
      this.$refs.approvalLogs.queryParams.wmsMaterialAdditionalId = id
      this.$refs.approvalLogs.open = true
    },
    // 获取显示隐藏列
    getColumnsList() {
      const { componentName } = this.$route.meta || null
      const params = {
        userId: this.userId,
        vueForm: componentName
      }
      getFormColumnsList(params).then(res => {
        if (isArray(res.data)) {
          this.columnsList = res.data.length === 0 ? this.columns : res.data
        } else {
          this.columnsList = this.columns
        }
      }).catch((err) => {
        if (err) {
          this.columnsList = this.columns
        }
      }).finally(() => {
        this.$emit('columns', this.columnsList)
      })
    }
  }
}
</script>

    <style>

    </style>

