/*
 *@description:
 *@author: hudw
 *@date: 2022-03-14 10:40:55
*/

<template>
    <a-modal width="70%" v-model="sVisible" v-bind="modalConfig" :confirmLoading="confirmLoading" @ok="handleOk">
      <p-grid-form ref="grid" selfUpdate :onValuesChange="onValuesChange" :span="6" :labelCol="{ span: 9 }" :wrapperCol="{ span: 15 }" :items="items">
      </p-grid-form>
      <p-table
        ref="table"
        dragle
        bordered
        row-key="_row_count"
        :scroll="{ x: true, y: 300 }"
        :columns="columns"
        :data-source="gridDataSource"
        :rowSelectType="false"
        :pagination="false"
      >
      <a slot="operation" :disabled="gridDataSource.length<=1" slot-scope="txt,record" @click.stop="toDel(record)">删除</a>
      </p-table>
    </a-modal>
</template>
<script>
import {  submitSave } from './setConfig.js';
import tableConfig from './tableConfig';
import { setUpdateForm } from './config.js';


export default {
  name: 'updateFormModal',
  props: {

    rowKey: {
      type: null,
      default: 'id',
    },
    setFilterValues: {
      type: Function,
    },
    onValuesChange: {
      type: Function,
    },
    /**0=不显示, 1=新建，2=修改 ，3=查看 */
    view: 0,
    title: '',
    visible: false,
    targetKeys: [],
    currentData: null,

    gridDataSource: [],
    /**DELIVERY_HANDLE_TYPE=待送达, PICKUP_HANDLE_TYPE=待提货 */
    modalTableShow: false,
    options:{},
    btnSave: false, //直接调接口保存提交
  },

  computed: {

    modalConfig() {
      const { title } = this;
      return { title, width: 740 };
    },
  },
  data() {
    return {
      carrierType:undefined,
      columns:tableConfig(this),
      confirmLoading: false,
      items: setUpdateForm(this),
      sVisible: this.visible,

    };
  },
  watch: {
    // currentData(data) {
    //   if (data) {

    //     this.form()?.setFieldsValue(data);
    //   }
    // },
    visible(value) {
      this.sVisible = value;
    },

    sVisible(value) {

      this.confirmLoading = false;
      let _this = this;
      if (value && this.view > 0) {
        setTimeout(() => {
          this.carrierType = undefined;
          this.$refs.grid.form.resetFields();
          if (this.view > 1 && this.currentData) {
            this.form()?.setFieldsValue(this.currentData);
            this.$refs.table?.resize();
          }

        },200);
      }
      this.$emit('update:visible', value);
    },
  },
  mounted() {

  },
  methods: {

    handleCancel() {
      this.previewVisible = false;
    },


    toDel(record) {
      this.$refs.table.delRowItem(record._row_count)

    },

    handleOk() {
      let _this = this;
      let grid = this.$refs.grid;
      this.$refs.grid.form.validateFields((errors, values) => {
        if (!errors) {
          values.transportPlanBillDetailVOS = this.gridDataSource.map(({shipbillDetailId,shipbillId,shipbillNo}) => {
            return {shipbillDetailId,shipbillId,shipbillNo}
          })
          let submit = async () => {

            return submitSave(this,values)
              .then((response) => {
                if (response.success) {
                  _this.$message.success(response.message)
                  return values;
                } else {
                  _this.$exception(response);
                  return null;
                }
              }).finally(() => {
                _this.confirmLoading = false;
                _this.sVisible = false;
              });
          };


          _this.confirmLoading = true;
          return _this.$emit(
            'ok',
            submit
          );
        }
      });
    },

    form() {
      return this.$refs?.grid?.form ?? null;
    },

    // 表格联动
    editPcsChange(value, record) {
      record.volume = parseFloat(this.BigJs(value || 0).times(record.unitVolume || 0));
      record.weight = parseFloat(this.BigJs(value || 0).times(record.unitWeight || 0));
    },
  },
};
</script>
<style scoped>
.ant-upload-select-picture-card i {
  font-size: 32px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}
</style>


