<template>
  <a-modal
    centered
    :footer="null"
    :maskClosable="false"
    class="sales-print-type-modal"
    :title="modalTit"
    v-model="isShow"
    @cancel="cancel"
    :width="600">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="sales-print-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="销售单号">{{ itemData&&itemData.salesBillNo || '--' }}</a-form-model-item>
        <a-form-model-item label="客户名称">{{ itemData&&itemData.buyerName || '--' }}</a-form-model-item>
        <a-form-model-item label="产品分类" prop="id" v-if="nowType=='SALES_BILL_TYPE'">
          <a-select id="sales-print-form" 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="dataScope" v-if="nowType=='export'">
          <a-radio-group v-model="form.dataScope">
            <a-radio value="all">全部</a-radio>
            <a-radio value="ENOUGH" :disabled="isDisabled">有库存</a-radio>
            <a-radio value="LESS" :disabled="isDisabled">缺货</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <!-- 销售分类打印 -->
        <a-form-model-item label="产品售价" prop="priceType" v-if="nowType=='SALES_BILL_TYPE'&&$hasPermissions('B_salesTypePrint_salesPrice')">
          <a-radio-group v-model="form.priceType">
            <a-radio value="SALES_BILL_TYPE_COST">打印</a-radio>
            <a-radio value="SALES_BILL_TYPE">不打印</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <!-- 销售打印 -->
        <a-form-model-item label="产品售价" prop="priceType" v-if="nowType=='SALES_BILL'&&$hasPermissions('B_salesPrint_salesPrice')">
          <a-radio-group v-model="form.priceType">
            <a-radio value="SALES_BILL_COST">打印</a-radio>
            <a-radio value="SALES_BILL">不打印</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <!-- 销售导出 -->
        <a-form-model-item label="产品售价" prop="priceType" v-if="nowType=='export'&&$hasPermissions('B_salesDetailExport_salesPrice')">
          <a-radio-group v-model="form.priceType">
            <a-radio :value="form.dataScope == 'ENOUGH' ? 'SALES_BILL_COST_NOT_LACK':'SALES_BILL_COST'">导出</a-radio>
            <a-radio :value="form.dataScope == 'ENOUGH' ? 'SALES_BILL_NOT_LACK':'SALES_BILL'">不导出</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <!-- 销售分类打印 -->
        <a-form-model-item label="货位编号" prop="orderBy" v-if="nowType=='SALES_BILL_TYPE'">
          <a-radio-group v-model="form.orderBy">
            <a-radio value="sbd.STACK_PLACE_CODE ASC">打印（↑升序）</a-radio>
            <a-radio value="sbd.STACK_PLACE_CODE DESC">打印（↓降序）</a-radio>
            <a-radio value="-1">不打印</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <!-- 平均公斤单价 -->
        <a-form-model-item label="平均公斤单价" prop="averagePrice" v-if="nowType=='export'&&$hasPermissions('B_salesDetail_avgprice')">
          <a-radio-group v-model="form.averagePrice">
            <a-radio :value="1">导出</a-radio>
            <a-radio :value="0">不导出</a-radio>
          </a-radio-group>
        </a-form-model-item>
        <!-- 原厂编码 -->
        <a-form-model-item label="原厂编码" prop="origCode" v-if="nowType=='export'">
          <a-radio-group v-model="form.origCode">
            <a-radio :value="1">导出</a-radio>
            <a-radio :value="0">不导出</a-radio>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="sales-print-back" @click="cancel">取消</a-button>
        <a-button type="primary" class="button-info" id="sales-print-save" @click="handleSave()" style="margin-left: 15px;">{{ nowType=='export' ? '导出' : '打印预览' }}</a-button>
        <a-button v-if="nowType!='export'" type="primary" id="sales-print" @click="handleSave('print')" style="margin-left: 10px;">快捷打印</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { salesDetailProductType } from '@/api/salesNew'
export default {
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemData: { // 单据详情信息
      type: Object,
      default: () => {
        return null
      }
    },
    nowType: { // 操作类型，打印、分类打印、导出
      type: String,
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      // 打印、导出选项
      form: {
        id: 'all',
        dataScope: 'all',
        priceType: undefined,
        orderBy: undefined,
        averagePrice: undefined,
        origCode: undefined
      },
      // 选项校验
      rules: {
        id: [{ required: true, message: '请选择产品分类', trigger: 'change' }],
        dataScope: [{ required: true, message: '请选择是否缺货产品', trigger: 'change' }],
        priceType: [{ required: true, message: '请选择产品售价', trigger: 'change' }],
        orderBy: [{ required: true, message: '请选择货位编号', trigger: 'change' }],
        averagePrice: [{ required: true, message: '请选择平均公斤单价', trigger: 'change' }],
        origCode: [{ required: true, message: '请选择原厂编码', trigger: 'change' }]
      },
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 15 }
      },
      spinning: false,
      typeList: [] // 产品分类
    }
  },
  computed: {
    // 弹框标题
    modalTit () {
      let title = ''
      if (this.nowType == 'SALES_BILL') {
        title = '销售打印'
      } else if (this.nowType == 'SALES_BILL_TYPE') {
        title = '销售分类打印'
      } else if (this.nowType == 'export') {
        title = '导出Excel'
      }
      return title
    },
    // 是否禁用
    isDisabled () {
      const states = ['WAIT_SUBMIT', 'WAIT_AUDIT', 'AUDIT_REJECT']
      return this.itemData && states.includes(this.itemData.billStatus)
    }
  },
  methods: {
    cancel () {
      this.$emit('close')
    },
    // 确认
    handleSave (isPrint) {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          // 分类打印
          if (_this.nowType == 'SALES_BILL_TYPE') {
            // 销售分类无权限不打印
            if (!_this.$hasPermissions('B_salesTypePrint_salesPrice')) {
              _this.form.priceType = 'SALES_BILL_TYPE'
            }
            const item = _this.typeList.find(item => item.id == _this.form.id)
            if (item) {
              const obj = {
                salesBillSn: _this.itemData.salesBillSn,
                productBrandSn: item.productBrandSn,
                productTypeSn3: item.productTypeSn3,
                priceType: _this.form.priceType,
                type: isPrint || 'preview'
              }
              // 打印货位编号
              if (_this.form.orderBy != '-1') {
                obj.orderBy = _this.form.orderBy
                obj.priceType = obj.priceType + '_STACK_PLACE'
              }
              _this.$emit('ok', obj)
            }
          } else if (_this.nowType == 'SALES_BILL') { // 销售打印
            // 销售打印无权限不打印
            if (!_this.$hasPermissions('B_salesPrint_salesPrice')) {
              _this.form.priceType = 'SALES_BILL'
            }
            const obj = {
              salesBillSn: _this.itemData.salesBillSn,
              priceType: _this.form.priceType,
              type: isPrint || 'preview'
            }
            _this.$emit('ok', obj)
          } else if (_this.nowType == 'export') { // 销售导出
            // 销售导出无权限不导出
            if (!_this.$hasPermissions('B_salesDetailExport_salesPrice')) {
              _this.form.priceType = _this.form.dataScope == 'ENOUGH' ? 'SALES_BILL_NOT_LACK' : 'SALES_BILL'
            }
            if (_this.form.averagePrice == '1') {
              _this.form.priceType += '_WEIGHT'
            }
            if (_this.form.origCode == '1') {
              _this.form.priceType += '_ORIG_CODE'
            }
            const obj = {
              salesBillSn: _this.itemData.salesBillSn,
              priceType: _this.form.priceType,
              dataScope: _this.form.dataScope == 'all' ? '' : _this.form.dataScope,
              type: isPrint || 'preview'
            }
            _this.$emit('ok', obj)
          }
          _this.isShow = false
        } else {
          return false
        }
      })
    },
    // 获取该销售单产品二级分类
    getTypeData () {
      this.typeList = [{ id: 'all' }]
      salesDetailProductType({ sn: this.itemData.salesBillSn || null }).then(res => {
        if (res.status == 200) {
          if (res.data && res.data.length > 0) {
            this.typeList = [...this.typeList, ...res.data]
          }
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        // 重置表单选项
        this.form = {
          id: 'all',
          dataScope: 'all',
          priceType: undefined,
          orderBy: undefined
        }
        this.$refs.ruleForm.resetFields()
      } else {
        // 如果是分类打印
        if (this.nowType == 'SALES_BILL_TYPE') {
          this.getTypeData()
        }
      }
    }
  }
}
</script>

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