<template>
  <div>
    <p v-if="showMessage">{{ message }}</p>
    <Form v-else ref="formModel" :model="formModel" :rules="formRule" label-position="top">
      <FormItem label="选择需要打印的物流公司" prop="logisticsCompany">
        <Select v-model="formModel.logisticsCompany">
          <Option v-for="item in logisticsCompanies" :value="item.ID.val" :key="item.ID.val">{{ item.NAME.val }}</Option>
        </Select>
      </FormItem>
    </Form>
    <businessButton :btn-config="btnConfig" />
  </div>
</template>

<script lang="js">
  import businessButton from 'professionalComponents/businessButton';
  import { common } from '@yunque/service/index';

  export default {
    name: 'PrintByLogistics',
    components: {
      businessButton
    },
    async created() {
      // this.setTitle('选择物流公司');

      let getSignPackagesRsp = await common.listQuery({
        table: 't_omssignpackage',
        range: 5000,
        isdroplistsearch: true,
        fixedcolumns: {
          isprint: ['=0', 'IS NULL']
        },
        fkdrplist: {
          searchcols: ['SHIPPERID']
        }
      });
      const signPackageIds = Array.from(new Set(getSignPackagesRsp.rows.map(a => `"=${a.SHIPPERID.refobjid}"`)))
        .join(',');

      if (signPackageIds) {
        let getLogisticsCompaniesRsp = await common.listQuery({
          table: 'T_V_OMSLOGISTICSCOMPANY',
          range: 5000,
          isdroplistsearch: true,
          fixedcolumns: {
            id: JSON.parse(`[${signPackageIds}]`)
          },
          fkdrplist: {
            searchcols: ['ID', 'NAME']
          }
        });

        this.logisticsCompanies = getLogisticsCompaniesRsp.rows;
      }
    },
    data() {
      return {
        message: '',
        logisticsCompanies: [],
        formModel: {
          logisticsCompany: ''
        },
        formRule: {
          logisticsCompany: [
            { required: true, message: '选择需要打印的物流公司', trigger: 'blur' }
          ]
        },
        btnConfig: {
          typeAll: 'error', // 按钮统一风格样式
          btnsite: 'right', // 按钮位置 (right , center , left)
          buttons: [
            {
              type: '', // 按钮类型
              text: '取消', // 取消 按钮文本
              icon: '', // 按钮图标
              size: '', // 按钮大小
              disabled: false, // 按钮禁用控制
              btnclick: () => {
                this.closeDialog();
              } // 按钮点击事件
            },
            {
              type: '', // 按钮类型
              text: '确定', // 按钮文本
              icon: '', // 按钮图标
              size: '', // 按钮大小
              disabled: false, // 按钮禁用控制
              btnclick: () => {
                this.submit();
              }, // 按钮点击事件
            }
          ]
        }
      };
    },
    computed: {
      showMessage() {
        if (this.message) {
          let btnCancel = this.btnConfig.buttons[0];
          btnCancel.text = '确定';
          this.btnConfig.buttons = [btnCancel];

          return true;
        }
        return false;
      }
    },
    methods: {
      submit() {
        this.$refs.formModel.validate(async (valid) => {
          if (!valid) {
            return;
          }


        });
      },
      setTitle(title) {
        this.$emit('setTitle', title);
      },
      closeDialog() {
        this.$emit('closeActionDialog', false);
      }
    }
  };
</script>
