<!-- 采购操作 -->
<template>
  <div>
      <div class="my-title">{{subTitle}}</div>

      <!-- 表单 -->
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
        <div class="form-item" v-for="(item, index) in dataForm.list" :key="item.purchaseDetailId">
          <el-row>
            <el-col :span="6">
              <el-form-item label="物品名称">
                <span>{{item.materialName}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="规格/型号">
                <span>{{item.specification}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="数量">
                <span>{{item.cnt}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="用途">
                <span>{{item.batchNumber}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row class="my-row">
            <el-col :span="6">
              <el-form-item ref="supplierCheckbox" label="供应商" :prop="'list.' + index + '.supplierId'" :rules="dataRule.supplierId">
                <supplier-component v-model="item.supplierId" :supplier-id.sync="item.supplierId"></supplier-component>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item ref="supplierInput" label="其他供应商" :prop="'list.' + index + '.supplierOther'" :rules="dataRule.supplierOther">
                <el-input v-model="item.supplierOther" placeholder="请输入其他供应商"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="采购依据" :prop="'list.' + index + '.procurementBasis'" :rules="dataRule.procurementBasis">
                <el-select
                  v-model="item.procurementBasis"
                  placeholder="请选择">
                  <el-option
                    v-for="item2 in optionsPB"
                    :key="item2.code"
                    :label="item2.value"
                    :value="item2.code">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row class="my-row">
            <el-col :span="6">
              <el-form-item label="不含税单价" :prop="'list.' + index + '.price'" :rules="dataRule.price">
                <el-input-number v-model="item.price" :step="1" :min="0" :precision="1"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="含税单价" :prop="'list.' + index + '.taxPrice'" :rules="dataRule.taxPrice">
                <el-input-number v-model="item.taxPrice" :step="1" :min="0" :precision="1"></el-input-number>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="含税总价">
                <span>{{ (item.cnt * item.taxPrice).toFixed(1) }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="税率" :prop="'list.' + index + '.taxRate'" :rules="dataRule.taxRate">
                <el-input-number v-model="item.taxRate" :step="1" :precision="1"/>&nbsp;%
              </el-form-item>
            </el-col>
          </el-row>
        </div>
       
      </el-form>
      <span slot="footer" class="dialog-footer">
      <el-button @click="onChose">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-reClick>确定</el-button>
    </span>
  </div>
</template>

<script>
  import SupplierComponent from '../common/supplier-component'
  import { getDictList } from '@/api/dict'
  export default {
    name: 'purchase-operate',
    components: {
      SupplierComponent
    },
    data () {
      let validateSupplier = (rule, value, callback) => {
        if (!this.dataForm.supplierId && !this.dataForm.supplierOther) {
          //callback(new Error('请至少填写一项供应商信息'))
          callback()
        } else {
          // 任意值被填写，清除验证提示
          if (!this.dataForm.supplierId || !this.dataForm.supplierOther) {
            this.$refs.supplierCheckbox.clearValidate()
            this.$refs.supplierInput.clearValidate()
          }
          callback()
        }
      }
      return {
        id: 0,
        dataForm: {
          cnt: 1,
          list:[]
        },
        dataRule: {
          supplierId: [{required: true, validator: validateSupplier, trigger: 'change'}],
          supplierOther: [{required: true, validator: validateSupplier, trigger: 'blur'}],
          procurementBasis: [{ required: true, message: '请选择采购依据', trigger: 'change' }],
          price: [{ required: true, message: '请输入不含税单价', trigger: 'change' }],
          taxPrice: [{ required: true, message: '请输入含税单价', trigger: 'change' }],
          taxRate: [{ required: true, message: '请输入税率', trigger: 'change' }]
        },
        optionsPB: [], // 采购依据（字典）
        subTitle: '' //副标题
      }
    },
    methods: {
      onChose () {
        this.$emit('onChose')
      },
      // 获取采购依据字典
      getPbList () {
        getDictList({type: 'procurement_basis'}).then(({data}) => {
          if (data) {
            this.optionsPB = data
          }
        })
      },
      async init (row) {
        if (!row) return
        this.getPbList()
        // this.dataForm = row

        if(row instanceof Array){
            this.subTitle = '批量采购'
            this.dataForm.list = []
            this.dataForm.list = [...row]
        } else {
          this.subTitle = '采购'
          this.dataForm.list = [row]
        }
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // 检查供应商
            let param = {};
            for(let index=0; index < this.dataForm.list.length; index++){
              let item = this.dataForm.list[index]
              if (!item.supplierId && !item.supplierOther) {
                this.$message.error('请填写供应商')
                return
              }

              item.purComDetailId = item.purchaseDetailId

              param[index] = item
            }

            console.log("param", param)

            this.$http({
              url: this.$http.adornUrl(`/biz-service/purchaseDetail/batch/updatePurchaseDetail`),
              method: 'post',
              data: this.$http.adornData(param)
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.onChose()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>
.form-item {
  margin: 5px 0;
  padding:25px 0 5px;
  background-color: #f1f9f9;
  border-radius: 10px;
}
</style>
