<template>
  <a-modal
    centered
    :footer="null"
    :maskClosable="false"
    class="allocateBill-print-type-modal"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow=false"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="allocateBill-print-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item style="margin-bottom: 0;" label="调拨单号">{{ itemData&&itemData.allocateNo || '--' }}</a-form-model-item>
        <a-form-model-item style="margin-bottom: 0;" label="调往对象">{{ itemData&&itemData.targetName || '--' }}</a-form-model-item>
        <a-form-model-item style="margin-bottom: 0;" label="发货编号">{{ itemData&&itemData.sendNo || '--' }}</a-form-model-item>
        <a-form-model-item style="margin-bottom: 0;" label="收货客户名称">{{ itemData&&itemData.receiverName || '--' }}</a-form-model-item>
        <a-form-model-item style="margin-bottom: 0;" label="产品分类" prop="id" v-if="nowType=='dbflPrint'||nowType=='dbflExport'">
          <a-select id="allocateBill-print-typeList" v-model="form.id" placeholder="请选择产品分类">
            <a-select-option v-for="item in typeList" :value="item.id" :key="item.id">
              <span v-if="item.id != 'all'">{{ item.productBrandName }} - {{ item.productTypeName3 }}</span>
              <span v-else>全部</span>
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="货位编号" prop="orderBy" v-if="nowType=='dbflPrint'">
          <a-radio-group v-model="form.orderBy" id="allocateBill-print-orderBy">
            <a-radio value="ASC" id="allocateBill-print-orderBy-asc">打印（↑升序）</a-radio>
            <a-radio value="DESC" id="allocateBill-print-orderBy-desc">打印（↓降序）</a-radio>
            <a-radio value="-1" id="allocateBill-print-orderBy-1">不打印</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="产品价格" style="margin-bottom: 0;" prop="priceType" v-if="nowType=='dbPrint'">
          <a-radio-group v-model="form.priceType" id="allocateBill-print-priceType">
            <a-radio value="ALLOCATE_BILL_PRICE" id="allocateBill-print-priceType1" v-if="$hasPermissions('B_transferOut_print_salesPrice')">销售价</a-radio>
            <a-radio value="ALLOCATE_BILL_COST" id="allocateBill-print-priceType2" v-if="$hasPermissions('B_transferOut_print_costPrice')">成本价</a-radio>
            <a-radio value="ALLOCATE_BILL" id="allocateBill-print-priceType3">不打印</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="产品价格" style="margin-bottom: 0;" prop="priceType" v-if="nowType=='dbExport'">
          <a-radio-group v-model="form.priceType" id="allocateBill-print-priceType">
            <a-radio value="ALLOCATE_BILL_PRICE" id="allocateBill-print-priceType1" v-if="$hasPermissions('B_transferOut_detail_export_salesPrice')">销售价</a-radio>
            <a-radio value="ALLOCATE_BILL_COST" id="allocateBill-print-priceType2" v-if="$hasPermissions('B_transferOut_detail_export_costPrice')">成本价</a-radio>
            <a-radio value="ALLOCATE_BILL" id="allocateBill-print-priceType3">不导出</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="备注" v-if="nowType=='dbPrint'">
          <a-checkbox @change="onChangeRemark" :checked="remarkFlag" id="allocateBill-print-remark">
            打印
          </a-checkbox>
        </a-form-model-item>
        <a-form-model-item label="发货说明" v-if="nowType=='dbPrint'||nowType=='dbflPrint'">
          <a-checkbox @change="onChangeExplainInfo" :checked="explainInfoFlag" id="allocateBill-print-explainInfo">
            打印
          </a-checkbox>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="allocateBill-printcancel" @click="handleClose('preview')">取消</a-button>
        <a-button
          v-if="nowType=='dbflPrint'||nowType=='dbPrint'"
          id="allocateBill-print-dbflPrint"
          type="primary"
          class="button-info"
          @click="handleSave('preview')"
          style="margin-left: 15px;">打印预览</a-button>
        <a-button
          v-if="hidePrint&&(nowType=='dbflPrint'||nowType=='dbPrint')"
          type="primary"
          id="allocateBill-print-dbflPrint"
          @click="handleSave('print')"
          style="margin-left: 15px;">确定打印</a-button>
        <a-button
          v-if="nowType=='dbflExport'||nowType=='dbExport'"
          type="primary"
          id="allocateBill-print-export"
          @click="handleSave('export')"
          style="margin-left: 15px;">确定导出</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { allocateBillProductTypeList } from '@/api/allocateBill'
export default {
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemData: {
      type: Object,
      default: () => {
        return null
      }
    },
    nowType: {
      type: String,
      default: ''
    },
    showExplainInfo: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      // 提交参数
      form: {
        id: 'all', // 产品分类
        priceType: 'ALLOCATE_BILL', // 产品价格
        orderBy: '-1', // 排序
        remarkFlag: 1, // 备货打印  1打印 0不打印
        explainInfoFlag: 1 // 发货说明  1打印 0不打印
      },
      //
      remarkFlag: true, // 备货打印
      explainInfoFlag: true, // 发货说明
      // 表单验证规则
      rules: {
        id: [{ required: true, message: '请选择产品分类', trigger: 'change' }],
        priceType: [{ required: true, message: '请选择产品价格', trigger: 'change' }],
        orderBy: [{ required: true, message: '请选择货位编号', trigger: 'change' }]
      },
      // form表单label布局
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      spinning: false,
      typeList: []// 产品分类列表数据
    }
  },
  computed: {
    // 弹窗标题
    modalTit () {
      let title = ''
      if (this.nowType == 'dbPrint') {
        title = '调拨打印'
      } else if (this.nowType == 'dbflPrint') {
        title = '调拨分类打印'
      } else if (this.nowType == 'dbflExport') {
        title = '调拨分类导出'
      } else if (this.nowType == 'dbExport') {
        title = '导出Excel'
      }
      return title
    },
    // 打印状态
    hidePrint () {
      return this.itemData && this.itemData.printState && this.itemData.printState != 'UNABLE_PRINT' && this.itemData.printState != 'CANCEL_PRINT'
    }
  },
  methods: {
    // 打印
    onChangeRemark (e) {
      this.remarkFlag = e.target.checked
      this.form.remarkFlag = e.target.checked ? 1 : 0
    },
    // 发货说明
    onChangeExplainInfo (e) {
      this.explainInfoFlag = e.target.checked
      this.form.explainInfoFlag = e.target.checked ? 1 : 0
    },
    // 确认
    handleSave (type) {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          let authCode = ''
          if (this.nowType == 'dbPrint') {
            authCode = 'B_transferOut_print'
          } else if (this.nowType == 'dbflPrint') {
            authCode = 'B_transferOutType_print'
          } else if (this.nowType == 'dbflExport') {
            authCode = 'B_transferOutType_export'
          } else if (this.nowType == 'dbExport') {
            authCode = 'B_transferOut_detail_export'
          }
          this.$store.state.app.curActionPermission = authCode
          // 普通打印/导出
          const obj = {
            allocateSn: _this.itemData.allocateSn,
            allocateNo: _this.itemData.allocateNo,
            printType: _this.form.priceType,
            isPreview: type == 'preview' ? 1 : 0
          }

          // 分类打印/导出
          if (_this.nowType == 'dbflPrint' || _this.nowType == 'dbflExport') {
            const item = _this.typeList.find(item => item.id == _this.form.id)
            if (item) {
              obj.productBrandSn = item.productBrandSn
              obj.productTypeSn3 = item.productTypeSn3
              obj.printType = 'ALLOCATE_BILL_TYPE'
            }
            // 打印货位编号
            if (_this.nowType == 'dbflPrint') {
              if (_this.form.orderBy != '-1') {
                obj.orderBy = _this.form.orderBy
              }
              obj.explainInfoFlag = this.form.explainInfoFlag
            }
          }
          // 打印
          if (this.nowType == 'dbPrint') {
            obj.remarkFlag = this.form.remarkFlag
            obj.explainInfoFlag = this.form.explainInfoFlag
          }
          // 打印或分类打印
          if (_this.nowType == 'dbPrint' || _this.nowType == 'dbflPrint') {
            _this.$emit('ok', obj)
          } else {
            // 导出
            _this.$emit('export', obj)
          }
          _this.isShow = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 获取该销售单产品二级分类
    getTypeData () {
      this.typeList = [{ id: 'all' }]
      allocateBillProductTypeList({ sn: this.itemData.allocateSn || null }).then(res => {
        if (res.status == 200) {
          if (res.data && res.data.length > 0) {
            this.typeList = [...this.typeList, ...res.data]
          }
        }
      })
    },
    // 关闭
    handleClose () {
      this.$emit('close')
      this.$refs.ruleForm.resetFields()
      this.form = {
        id: 'all', // 产品分类
        priceType: 'ALLOCATE_BILL', // 产品价格
        orderBy: '-1', // 排序
        remarkFlag: 1, // 备货打印  1打印 0不打印
        explainInfoFlag: 1 // 发货说明  1打印 0不打印
      }
      this.remarkFlag = true
      this.explainInfoFlag = true
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.handleClose()
      } else {
        if (this.nowType == 'dbflPrint' || this.nowType == 'dbflExport') {
          this.getTypeData()
        }
      }
    }
  }
}
</script>

<style lang="less">
  .allocateBill-print-type-modal{
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
