<template>
  <el-dialog :title="$t('批量签收')" :before-close="()=>{$emit('update:showConsBatchSignOff',false)}"
             :visible.sync="showConsBatchSignOff">
    <h1>合共<span style="color: red">{{ consIds.length }}</span> 个包裹</h1>
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item :label="$t('集运仓库')" v-model="form.wareId" prop="wareId" :rules="{
                              required: true, message: '请选择集运仓库', trigger: 'blur'
                              }">
        <template v-for="(item, index) in warehouseList">
          <el-button :type="warehouseList.length === 1 ||form.wareId === item.id ? 'primary' : 'default'"
                     @click="setWarehouseId(item)">
            {{ item.titleZh }}
          </el-button>
        </template>
      </el-form-item>
      <el-row :gutter="10">
        <el-col :span="10">
          <el-form-item label="签收人" prop="signer">
            <el-select
              v-model="form.signer"
              :placeholder="$t('请选择签收人')"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in allSimplList"
                :key="dict.id"
                :label="dict.nickname"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="签收时间" prop="signedTime">
            <el-date-picker clearable v-model="form.signedTime" type="date" value-format="timestamp"
                            placeholder="选择签收时间" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="签收时间" prop="mediaUrl">
        <image-upload v-model="form.mediaUrl"/>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">{{ $t("确认签收") }}</el-button>
      <el-button @click="$emit('update:showConsBatchSignOff',false)">{{ $t("取 消") }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
import imageUpload from "@/components/ImageUpload/index.vue";
import {updateConsBatchSignOff} from '@/api/ecw/cons'
import {DICT_TYPE} from "@/utils/dict";
import {listAllSimpl} from "@/api/system/user";
import Template from "@/views/cms/template/index.vue";
import {getConsWarehouse} from "@/api/ecw/warehouse";

export default {
  name: 'EcwBatchSignOff',
  computed: {
    DICT_TYPE() {
      return DICT_TYPE
    }
  },
  components: {Template, imageUpload},
  props: {
    consIds: {  // 接收数组
      type: Array,
      default: () => []
    },
    showConsBatchSignOff: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      warehouseList: [],
      allSimplList:[],
      rules: {
        status: [
          { required: true, message: "请选择状态", trigger: "blur" }
        ]
      },
    }
  },
  created() {
    listAllSimpl().then((r) => {
      this.allSimplList = r.data
    })
    getConsWarehouse().then((r) => {
      this.warehouseList = r.data;
      if (this.warehouseList.length === 1) {
        this.setWarehouseId(this.warehouseList[0]);
      }
    });
  },
  methods: {
    setWarehouseId(item) {
      this.$set(this.form, 'wareId', item.id)
    },
    submitForm() {
      //二次确认
      this.$modal
        .confirm(this.$t("确认签收" + "?"))
        .then(() => {
          this.form.consIds = this.consIds;
          this.$refs.form.validate(validate => {
            if (validate) {
              updateConsBatchSignOff(this.form).then(response => {
                this.$emit("update:showConsBatchSignOff", false)
                this.$emit("determine")
                this.$modal.msgSuccess("批量签收成功")
              })
            }
          })
        })
        .catch(() => {
          return false;
        });
    }
  }
}
</script>

<style scoped lang="scss">

</style>
