<template>
  <div>
    <div class="i-layout-page-header">
      <PageHeader :title="$route.meta.title" :content="$route.meta.description" hidden-breadcrumb />
    </div>
    <Card :bordered="false" dis-hover class="ivu-mt">
      <view-table ref="table" view-code="OffTask" @create="$refs.form.open()" @edit="({ row, index }) => $refs.form.open(row)">
        <template #command="{ row }">
          <Button size="small" v-if="row.status == 'Pending'" title="提前关闭" type="error" custom-icon="fa fa-stop-circle" @click="close(row)"></Button>
        </template>
      </view-table>
    </Card>
    <modal-form ref="form" view-code="OffTaskEdit" @on-change="formChange" @on-after-save="$refs.table.loadData()" />
  </div>
</template>

<script>
  import { mixinPage } from 'mooho-base-admin-plus';
  import offTaskApi from '../../api/offTask';

  export default {
    name: 'storage-offTask',
    mixins: [mixinPage],
    components: {},
    data() {
      return {};
    },
    computed: {},
    methods: {
      async close(row) {
        this.confirm('确定关闭当前任务吗？', async () => {
          await offTaskApi.close(row);
          this.$refs.table.loadData();
        });
      },
      formChange(sender, oldValue, newValue, selected) {
        let data = this.$refs.form.data;

        if (sender && sender.code == 'warehouseID') {
          data.locationStorageID == null;
          data.materialID = null;
          data.material = null;
          data.batchNumber = null;
          data.storageStatus = null;
          data.quantity = null;
          data.suggestStoreLocationID = null;
          data.suggestStoreLocation = null;
        }

        if (sender && sender.code == 'locationStorageID') {
          if (selected) {
            data.materialID = selected.materialID;
            data.material = selected.material;
            data.batchNumber = selected.batchNumber;
            data.storageStatus = selected.storageStatus;
            data.quantity = selected.quantity;
            data.suggestStoreLocationID = selected.storeLocationID;
            data.suggestStoreLocation = selected.storeLocation;
          } else {
            data.materialID = null;
            data.material = null;
            data.batchNumber = null;
            data.storageStatus = null;
            data.quantity = null;
            data.suggestStoreLocationID = null;
            data.suggestStoreLocation = null;
          }
        }
      }
    }
  };
</script>
