<template>
  <!-- 修改仓库信息-->
  <a-modal class="modify-custom commonModal"
           title="修改仓库信息"
           v-model:visible="visible"
           centered
           :maskClosable="false"
           @cancel="cancel"
           width="890px">
    <template #footer>
      <a-button key="submit"
                type="primary"
                :loading="loading"
                @click="cancel">关闭</a-button>
      <a-button key="submit"
                type="primary"
                :loading="loading"
                @click="submit">修改</a-button>
    </template>
    <a-form class="inlineForm"
            ref="formRef"
            :model="formState"
            :rules="rules">
      <a-row :gutter="24">
        <a-col :span="12">
          <a-form-item label="仓库类型"
                       name="warehousetype">
            <a-select class="typeSelect"
                      style="width: 200px"
                      v-model:value="formState.warehousetype"
                      placeholder="请选择仓库类型">
              <a-select-option v-for="item in warehouseType"
                               :key="item.enumitemname"
                               :value="item.enumitemname">
                {{item.enumdicname}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="仓库名称"
                       name="warehousename">
            <a-input class="dialogInput"
                     style="width: 200px"
                     v-model:value="formState.warehousename"
                     placeholder="请输入仓库名称" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="12">
          <a-form-item label="仓库简称"
                       name="warehousecode">
            <a-input class="dialogInput"
                     style="width: 200px"
                     v-model:value="formState.warehousecode"
                     placeholder="请输入仓库简称" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="联系人"
                       name="contactname">
            <a-input class="dialogInput"
                     style="width: 200px"
                     v-model:value="formState.contactname"
                     placeholder="请输入联系人" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-item label="联系电话"
                       name="contactnum">
            <a-input class="dialogInput"
                     style="width: 200px"
                     v-model:value="formState.contactnum"
                     placeholder="请输入联系电话" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-item label="所在地区">
            <a-select class="inlineFormSelect"
                      style="width: 205px"
                      v-model:value="formState.provinceid"
                      @change="provinceChange"
                      placeholder="请选择省">
              <a-select-option v-for="item in provinceList"
                               :key="item.autoid"
                               :value="item.autoid">
                {{item.divisionname}}
              </a-select-option>
            </a-select>
            <a-select class="inlineFormSelect ml9"
                      style="width: 205px"
                      v-model:value="formState.cityid"
                      @change="cityChange"
                      placeholder="请选择市">
              <a-select-option v-for="item in cityList"
                               :key="item.autoid"
                               :value="item.autoid">
                {{item.divisionname}}
              </a-select-option>
            </a-select>
            <a-select class="inlineFormSelect ml9"
                      v-model:value="formState.districtid"
                      style="width: 205px"
                      placeholder="请选择县(区)">
              <a-select-option v-for="item in districtList"
                               :key="item.autoid"
                               :value="item.autoid">
                {{item.divisionname}}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row :gutter="24">
        <a-col :span="24">
          <a-form-item label="详细地址"
                       name="address">
            <a-input class="dialogInput"
                     v-model:value="formState.address"
                     style="width: 635px"
                     placeholder="请输入详细地址" />
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-modal>
</template>

<script lang="ts">
import { defineComponent, PropType, ref, toRaw, watchEffect } from 'vue';
import { closeModal } from '@/common/setup/modal/index';
import { getAddress } from '@/services/go/adress';
import { handleForm, FormState, warehouseApply } from '../setup';
import { AllEnums } from '@/services/go/commonService/interface';
import { WarehouseApplyReq } from '@/services/proto/warehouse/interface';
import { ErmcpWareHouseInfo } from '@/services/go/ermcp/warehouse-info/interface';
import { mergeTwoObj } from '@/utils/objHandle';
import { getWarehouseTypeEnumList } from '@/common/constants/enumsList';
import { validateAction } from '@/common/setup/form';

export default defineComponent({
    name: 'warehouse_info_btn_modify',
    props: {
        selectedRow: {
            type: Object as PropType<ErmcpWareHouseInfo>,
            default: {},
        },
    },
    components: {},
    setup(props, context) {
        const { visible, cancel } = closeModal('warehouse_info_btn_modify');
        const loading = ref<boolean>(false);
        const { cityList, districtList, provinceList, getCityList, getDistrictList } = getAddress();
        const warehouseType = ref<AllEnums[]>(getWarehouseTypeEnumList());
        const { formRef, formState, rules } = handleForm();
        watchEffect(() => {
            if (visible.value) {
                mergeTwoObj(formState, props.selectedRow);
                const { provinceid, cityid } = props.selectedRow;
                provinceid ? getCityList(provinceid) : (formState.provinceid = undefined);
                cityid ? getDistrictList(cityid) : ((formState.cityid = undefined), (formState.districtid = undefined));
            }
        });
        function submit() {
            validateAction<FormState>(formRef, formState).then((param) => {
                const reqParam: WarehouseApplyReq = {
                    type: 2, // int32 类型 1 新增 2 修改
                    warehouseid: props.selectedRow.autoid, // uint64 仓库ID
                    warehousecode: props.selectedRow.warehousecode, // string 仓库代码
                    warehousename: param.warehousename, // string 仓库名称
                    warehousetype: undefined === param.warehousetype ? 0 : (param.warehousetype as number), // int32 仓库类型 - 1 厂库  2 自有库  3 合作库
                    provinceid: undefined === param.provinceid ? 0 : (param.provinceid as number), // uint64 省
                    cityid: undefined === param.cityid ? 0 : (param.cityid as number), // uint64 市
                    districtid: undefined === param.districtid ? 0 : (param.districtid as number), // int32 区
                    address: param.address, // string 详细地址
                    contactname: param.contactname, // string 联系人
                    contactnum: param.contactnum, // string 联系电话
                };
                warehouseApply(reqParam, loading)
                    .then((res) => {
                        cancel();
                        context.emit('refresh');
                    })
                    .catch((err) => {});
            });
        }
        function provinceChange(id: number) {
            getCityList(id);
            formState.cityid = undefined;
            formState.districtid = undefined;
        }
        function cityChange(id: number) {
            getDistrictList(id);
            formState.districtid = undefined;
        }
        return {
            visible,
            cancel,
            submit,
            loading,
            maskClosableFlag: false,
            cityList,
            districtList,
            provinceList,
            provinceChange,
            cityChange,
            formRef,
            formState,
            rules,
            warehouseType,
        };
    },
});
</script>

<style lang="less">
.modify-custom {
}
</style
>;
